Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
phd0 authored Feb 6, 2024
1 parent 169c213 commit d1591c9
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 31 deletions.
126 changes: 96 additions & 30 deletions Adafruit_LEDBackpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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, // "
Expand Down Expand Up @@ -261,7 +282,8 @@ static const PROGMEM uint16_t alphafonttable[] = {
0b0000010100100000, // ~
0b0011111111111111,

};
};


void Adafruit_LEDBackpack::setDisplayState(bool state) {
uint8_t buffer;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -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);
}

Expand All @@ -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) {}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -811,4 +877,4 @@ void Adafruit_7segment::printError(void) {
for (uint8_t i = 0; i < SEVENSEG_DIGITS; ++i) {
writeDigitRaw(i, (i == 2 ? 0x00 : 0x40));
}
}
}
40 changes: 39 additions & 1 deletion Adafruit_LEDBackpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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
#endif // Adafruit_LEDBackpack_h

0 comments on commit d1591c9

Please sign in to comment.