From d1591c9f58c051ab72dac232fd7ce32869a76030 Mon Sep 17 00:00:00 2001 From: DULAC Philippe Date: Tue, 6 Feb 2024 21:20:46 +0100 Subject: [PATCH] Add files via upload --- Adafruit_LEDBackpack.cpp | 126 +++++++++++++++++++++++++++++---------- Adafruit_LEDBackpack.h | 40 ++++++++++++- 2 files changed, 135 insertions(+), 31 deletions(-) diff --git a/Adafruit_LEDBackpack.cpp b/Adafruit_LEDBackpack.cpp index e155812..f575202 100644 --- a/Adafruit_LEDBackpack.cpp +++ b/Adafruit_LEDBackpack.cpp @@ -43,11 +43,11 @@ #endif #ifndef _swap_int16_t -#define _swap_int16_t(a, b) \ - { \ - int16_t t = a; \ - a = b; \ - b = t; \ +#define _swap_int16_t(a, b) \ + { \ + int16_t t = a; \ + a = b; \ + b = t; \ } ///< 16-bit var swap #endif @@ -153,17 +153,38 @@ static const PROGMEM uint8_t sevensegfonttable[] = { static const PROGMEM uint16_t alphafonttable[] = { - 0b0000000000000001, 0b0000000000000010, 0b0000000000000100, - 0b0000000000001000, 0b0000000000010000, 0b0000000000100000, - 0b0000000001000000, 0b0000000010000000, 0b0000000100000000, - 0b0000001000000000, 0b0000010000000000, 0b0000100000000000, - 0b0001000000000000, 0b0010000000000000, 0b0100000000000000, - 0b1000000000000000, 0b0000000000000000, 0b0000000000000000, - 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, - 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, - 0b0001001011001001, 0b0001010111000000, 0b0001001011111001, - 0b0000000011100011, 0b0000010100110000, 0b0001001011001000, - 0b0011101000000000, 0b0001011100000000, + 0b0000000000000001, + 0b0000000000000010, + 0b0000000000000100, + 0b0000000000001000, + 0b0000000000010000, + 0b0000000000100000, + 0b0000000001000000, + 0b0000000010000000, + 0b0000000100000000, + 0b0000001000000000, + 0b0000010000000000, + 0b0000100000000000, + 0b0001000000000000, + 0b0010000000000000, + 0b0100000000000000, + 0b1000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b0001001011001001, + 0b0001010111000000, + 0b0001001011111001, + 0b0000000011100011, + 0b0000010100110000, + 0b0001001011001000, + 0b0011101000000000, + 0b0001011100000000, 0b0000000000000000, // 0b0000000000000110, // ! 0b0000001000100000, // " @@ -261,7 +282,8 @@ static const PROGMEM uint16_t alphafonttable[] = { 0b0000010100100000, // ~ 0b0011111111111111, -}; + }; + void Adafruit_LEDBackpack::setDisplayState(bool state) { uint8_t buffer; @@ -294,7 +316,7 @@ bool Adafruit_LEDBackpack::begin(uint8_t _addr, TwoWire *theWire) { i2c_dev = new Adafruit_I2CDevice(_addr, theWire); if (!i2c_dev->begin()) return false; - + i2c_addr = _addr; // turn on oscillator uint8_t buffer[1] = {0x21}; i2c_dev->write(buffer, 1); @@ -305,11 +327,8 @@ bool Adafruit_LEDBackpack::begin(uint8_t _addr, TwoWire *theWire) { // when it is turned on. clear(); writeDisplay(); - blinkRate(HT16K33_BLINK_OFF); - setBrightness(15); // max brightness - return true; } @@ -322,7 +341,6 @@ void Adafruit_LEDBackpack::writeDisplay(void) { buffer[1 + 2 * i] = displaybuffer[i] & 0xFF; buffer[2 + 2 * i] = displaybuffer[i] >> 8; } - i2c_dev->write(buffer, 17); } @@ -332,6 +350,55 @@ void Adafruit_LEDBackpack::clear(void) { } } +// Helper button functions, the data is updated every readSwitches() call! + +bool Adafruit_LEDBackpack::isKeyPressed(uint8_t group, uint8_t k) { + if (group > 2 or k > 12) + return false; + return (keys[group] & _BV(k)); +} + +bool Adafruit_LEDBackpack::wasKeyPressed(uint8_t group, uint8_t k) { + if (group > 2 or k > 12) + return false; + return (lastkeys[group] & _BV(k)); +} + +bool Adafruit_LEDBackpack::justPressed(uint8_t group, uint8_t k) { + return (isKeyPressed(group, k) & !wasKeyPressed(group, k)); +} + +bool Adafruit_LEDBackpack::justReleased(uint8_t group, uint8_t k) { + return (!isKeyPressed(group, k) & wasKeyPressed(group, k)); +} + +bool Adafruit_LEDBackpack::readSwitches(void) { + memcpy(lastkeys, keys, sizeof(keys)); + + Wire.beginTransmission((byte)i2c_addr); + Wire.write(0x40); + Wire.endTransmission(); + Wire.requestFrom((byte)i2c_addr, (byte)6); + for (uint8_t i = 0; i < 6; i++) + _keys[i] = Wire.read(); + keys[0] = _keys[0] | (_keys[1] & 0x1F) << 8; + keys[1] = _keys[2] | (_keys[3] & 0x1F) << 8; + keys[2] = _keys[4] | (_keys[5] & 0x1F) << 8; + + for (uint8_t i = 0; i < 3; i++) { + if (lastkeys[i] != keys[i]) { + /* for (uint8_t j = 0; j < 3; j++) { + Serial.print("0x"); + Serial.print(keys[j], HEX); + Serial.print(" "); + } + Serial.println(); */ + return true; + } + } + return false; +} + /******************************* QUAD ALPHANUM OBJECT */ Adafruit_AlphaNum4::Adafruit_AlphaNum4(void) {} @@ -342,15 +409,12 @@ void Adafruit_AlphaNum4::writeDigitRaw(uint8_t n, uint16_t bitmask) { void Adafruit_AlphaNum4::writeDigitAscii(uint8_t n, uint8_t a, bool d) { uint16_t font = pgm_read_word(alphafonttable + a); - displaybuffer[n] = font; - /* Serial.print(a, DEC); Serial.print(" / '"); Serial.write(a); Serial.print("' = 0x"); Serial.println(font, HEX); */ - if (d) displaybuffer[n] |= (1 << 14); } @@ -552,15 +616,18 @@ void Adafruit_BicolorMatrix::drawPixel(int16_t x, int16_t y, uint16_t color) { displaybuffer[y] |= 1 << x; // Turn off red LED. displaybuffer[y] &= ~(1 << (x + 8)); - } else if (color == LED_RED) { + } + else if (color == LED_RED) { // Turn on red LED. displaybuffer[y] |= 1 << (x + 8); // Turn off green LED. displaybuffer[y] &= ~(1 << x); - } else if (color == LED_YELLOW) { + } + else if (color == LED_YELLOW) { // Turn on green and red LED. displaybuffer[y] |= (1 << (x + 8)) | (1 << x); - } else if (color == LED_OFF) { + } + else if (color == LED_OFF) { // Turn off green and red LED. displaybuffer[y] &= ~(1 << x) & ~(1 << (x + 8)); } @@ -643,7 +710,6 @@ void Adafruit_7segment::println(double n, int digits) { void Adafruit_7segment::print(double n, int digits) { printFloat(n, digits); } size_t Adafruit_7segment::write(char c) { - uint8_t r = 0; if (c == '\n') @@ -811,4 +877,4 @@ void Adafruit_7segment::printError(void) { for (uint8_t i = 0; i < SEVENSEG_DIGITS; ++i) { writeDigitRaw(i, (i == 2 ? 0x00 : 0x40)); } -} \ No newline at end of file +} diff --git a/Adafruit_LEDBackpack.h b/Adafruit_LEDBackpack.h index ff42e76..0dbd00e 100644 --- a/Adafruit_LEDBackpack.h +++ b/Adafruit_LEDBackpack.h @@ -89,6 +89,11 @@ See https://learn.adafruit.com/14-segment-alpha-numeric-led-featherwing/usage @brief Class encapsulating the raw HT16K33 controller device. */ class Adafruit_LEDBackpack { +private: + uint8_t i2c_addr; + uint16_t keys[3], lastkeys[3]; + uint8_t _keys[6]; + public: /*! @brief Constructor for HT16K33 devices. @@ -139,6 +144,39 @@ class Adafruit_LEDBackpack { uint16_t displaybuffer[8]; ///< Raw display data + /*! + @brief return position of key. + @param group 0 to 2 of 13 keys + @param k number of key (0 to 12) + */ + bool isKeyPressed(uint8_t group, uint8_t k); + + /*! + @brief return previous position of key. + @param group 0 to 2 of 13 keys + @param k number of key (0 to 12) + */ + bool wasKeyPressed(uint8_t group, uint8_t k); + + /*! + @brief true only one time : return OFF -> ON position of key. + @param group 0 to 2 of 13 keys + @param k number of key (0 to 12) + */ + bool justPressed(uint8_t group, uint8_t k); + + /*! + @brief true only one time : return ON -> OFF position of key. + @param group 0 to 2 of 13 keys + @param k number of key (0 to 12) + */ + bool justReleased(uint8_t group, uint8_t k); + + /*! + @brief read all the keys, and return TRUE if one or more keys change. + */ + bool readSwitches(); + protected: Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface }; @@ -487,4 +525,4 @@ class Adafruit_AlphaNum4 : public Adafruit_LEDBackpack { void writeDigitAscii(uint8_t n, uint8_t ascii, bool dot = false); }; -#endif // Adafruit_LEDBackpack_h \ No newline at end of file +#endif // Adafruit_LEDBackpack_h