An asynchronous function that does not produce an immediate result inside a while loop

I'm new to nodejs, and what I did was, I connected the LCD panel and 4x4 Membrane matrix keyboard to the Raspberry Pi, and I programmed them using Node.js. What I want to achieve is that whenever a key is pressed, it should be displayed immediately on the LCD panel, and when I press #, it should stop accepting input.

For this, I used the LCD packages https://www.npmjs.com/package/lcd and RPIO https://github.com/jperkin/node-rpio , and since I have to constantly check user input, I put the code for input during the cycle and inside that I wrote the print instruction, and that is where the problem goes. The LCD panel does not display a character when I press a key on the keyboard, but when I press #, the program exits and all the characters are displayed on the LCD panel.

The code I wrote is as follows.

var rpio = require('rpio'); var Lcd = require('lcd'),//This is asynchronous function lcd = new Lcd({ rs: 18, e: 23, data: [24, 17, 27, 22], cols: 8, rows: 2 }); var matrix=[[1,2,3,'A'], [4,5,6,'B'], [7,8,9,'C'], ['*',0,'#','D']] var row=[37,35,33,31]; var col=[29,23,40,38]; for (var i = 0; i < 4; i++) { rpio.open(col[i], rpio.OUTPUT, rpio.HIGH); } for (var i = 0; i < 4; i++) { rpio.open(row[i], rpio.INPUT, rpio.PULL_UP); } var code=""; var comeout=0; lcd.on('ready', function() { lcd.setCursor(0, 0); //start of keypad code while(true){ for (var j = 0; j < 4; j++) { rpio.write(col[j],rpio.LOW); for (var i = 0; i < 4; i++) { if(rpio.read(row[i])==0){ console.log(matrix[i][j]); lcd.print(matrix[i][j]); if(matrix[i][j]=='#'){ comeout=1; break; } while(rpio.read(row[i])==0); } } if(comeout==1) break; else rpio.write(col[j],rpio.HIGH); } if(comeout==1) break; } //end of keypad code }); // If ctrl+c is hit, free resources and exit. process.on('SIGINT', function() { lcd.clear(); lcd.close(); process.exit(); }); 

Any help would be greatly appreciated. Thanks.

+5
source share
1 answer

As discussed with @Thomas above, I would suggest you simulate a while(true) with setImediate calls in which you query the matrix and execute the LCD.print file. This is because LCD.print adds operations to the javascripts event queue. But event queue operations are blocked until the actual operation is completed. It will never end while you are in a while(true) .

So, you must end the active operation and give control of the event loop, which in turn executes the print commands. But at the same time, you must assure that you are again looking at the key matrix until you press the "#" key.

Here is an example:

 lcd.on('ready', function() { lcd.setCursor(0, 0); setImediate( function scanMatrix() { for (var j = 0; j < 4; j++) { rpio.write(col[j],rpio.LOW); for (var i = 0; i < 4; i++) { if(rpio.read(row[i])==0) { console.log(matrix[i][j]); lcd.print(matrix[i][j]); if(matrix[i][j]!='#') { setImmediate(scanMatrix); } while(rpio.read(row[i])==0); } } rpio.write(col[j],rpio.HIGH); } }); }); 

The code is not verified because there is no raspi here. It should give you an idea of ​​how to solve the problem.

Really cool ;-) the solution would be to add a microcontroller (MSP430, ...) to the key matrix. MC scans the matrix and transmits keystrokes to raspi via SPI or I2C ..... OK, ok, ok, don't beat me; -)

0
source

Source: https://habr.com/ru/post/1262486/


All Articles