Coder Social home page Coder Social logo

Comments (2)

zapdogster avatar zapdogster commented on May 25, 2024

Not sure this is the best approach, but I just did a brute force translation. Pls let me know if there is a better way:

// **********************************************************************
// Function to translate button hex code returned to expected 0-7 value
int fnTranslateButtonCode (uint32_t dwButton)
{
int i_ButtonTranslated = dwButton >> 16 ;

switch (i_ButtonTranslated) {
case 0x01:
i_ButtonTranslated = 0 ;
break ;
case 0x04:
i_ButtonTranslated = 1 ;
break ;
case 0x10:
i_ButtonTranslated = 2 ;
break ;
case 0x40:
i_ButtonTranslated = 3 ;
break ;
case 0x02:
i_ButtonTranslated = 4 ;
break ;
case 0x08:
i_ButtonTranslated = 5 ;
break ;
case 0x20:
i_ButtonTranslated = 6 ;
break ;
case 0x80:
i_ButtonTranslated = 7 ;
break ;
}
return i_ButtonTranslated ;
}

from tm16xx.

maxint-rd avatar maxint-rd commented on May 25, 2024

Hello @zapdogster ,

The getButtons() method of the chip-specific class indeed does the work. In your case of the TM1638 LED&KEY module, it is TM1638.cpp that has the implementation. The number of buttons supported depends on the specific chip. While your module has eight buttons, the TM1638 chip supports 8x3 buttons and simultaneous presses. There is another TM1638 module that has a 4x4 keypad. Other chips have different support for buttons, as you can see in this table. To implement a uniform interface the getButtons() method returns a 32-bit value,, representing the state of a maximum of 32 buttons. The LED&KEY module only uses buttons on the K3 pin, which are returned by getButtons() as the third byte of the 32-bit value, giving you buttons 16-23. (The 4x4 button module also returns other values)

The TM16xxButtons_clicks example uses the TM16xx buttons class to report the pressed button. The callback functions receive buttons numbers in the range 0-31, based on their bit position. The easiest way to change the button number to a 0-7 range in for instance fnClick is to simply subtract 16 from the value:

void fnClick(byte nButton)
{
  nButton=nButton-16;       // change values 16-23 to 0-7
  Serial.print(F("Button "));
  Serial.print(nButton);
  Serial.println(F(" click."));
} // click

Edit: as you noticed the button order may seem less logic. The actual order depends on the wiring on the module. Your method of using a switch statement is a good way, but I would manipulate the button number in the callback function instead of the 32-bit value. My reason is that those callback functions add very useful functionality, such as detecting simultaneous presses, long key presses and double clicks. Therefor if needed I would just make a simple translation function to change the order and call that in the callback functions.

from tm16xx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.