espruino / EspruinoDocs

See http://espruino.com for the complete Espruino Documentation - many links in this repository will not work
http://www.espruino.com/
Other
259 stars 284 forks source link

KeyPad return values #691

Closed cwilling closed 1 year ago

cwilling commented 1 year ago

I'm trying to use the Keypad module with a generic esp32 board. From the documentation it looks like both the callback and keypad.read() versions below should return an integer to use as index into string array of characters to print:

require("KeyPad").connect([B2,B3,B4,B5],[B6,B7,B8,B9], function(e) {
  print("123A456B789C*0#D"[e]);
});

or

var keypad = require("KeyPad").connect([B2,B3,B4,B5],[B6,B7,B8,B9]);
print("123A456B789C*0#D"[keypad.read()]);

Here however I see keyboard.read() outputs like this (for a 4x4 keypad):

 00       10      20      30
 04       14      24      34
 08       18      28      38
012      112     212     312

where the first character of each of these seems to represent the column and the remaining characters represent a number such that when added to the column number, gives the index we want e.g. "312" would be column 3 and then adding 3+12 gives an index of 15 into the character array, so printed output would be "D" and "20" would be column 2 and then adding 2+0 gives an index of 2 into the character array, so printed output would be "3".

I can easily add this processing in my app but should I have to? Why isn't output of keypad.read() already doing this and returning an integer like the example suggests?

In my fiddling with this, I was able to add this processing to keypad.js itself to generate the expected output. I could submit a PR for this but there is a second problem, maybe related, namely that the callback example never returns anything. Adding a 'print' to the 'onWatch' function does nothing - it is never called. Before delving deeper, I thought I'd check in here for any insights.

Since there are Kepad Timer and KeyPad Combination Lock examples documented for official boards, the existing implementation must work (or must have worked at some time).

Could the problems I'm seeing be due to some differences in how the official Espruino boards work? I note that the documented examples use GPIO identifiers like B2,B3,B4,B5 etc., which are not recognized in esp32. Is there something special about the 'B' identifiers?

Thanks for any clues.

gfwilliams commented 1 year ago

What firmware version is your ESP32 board on?

The KeyPad module always used to work and I used it reasonably recently myself, but actually I made some changes to the cutting edge releases a few weeks ago that may trip this up...

Looking at https://www.espruino.com/modules/KeyPad.js we iterate using for (var i in rows) {, but I discovered recently that in JS when you do this for an array, i is always a string, not a number so I had to change Espruino to match - and I think that messed this up. I'll put a change in to fix it.

About setWatch - that could be ESP32 specific though. There's nothing special about B pins, that was for STM32 - but I've used keypad recently on NRF52 and it's fine. I guess you might need to delve a bit deeper to see why the watches aren't firing...

PS. If you're going to post on the forum and the GitHub (ideally you wouldn't double-post though) please can you at least try and reference one from the other so I don't end up answering the same question twice?