Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add button inputs keys funcions #84

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
116 changes: 87 additions & 29 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,47 @@ 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])
return true;
return false;
}

/******************************* QUAD ALPHANUM OBJECT */

Adafruit_AlphaNum4::Adafruit_AlphaNum4(void) {}
Expand All @@ -342,15 +401,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 +608,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 +702,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
38 changes: 38 additions & 0 deletions 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
62 changes: 62 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#######################################
# Syntax Coloring Map For Adafruit_LEDBackpack
#######################################
# Class
#######################################

Adafruit_LEDBackpack KEYWORD1
Adafruit_24bargraph KEYWORD1
Adafruit_8x16matrix KEYWORD1
Adafruit_8x16minimatrix KEYWORD1
Adafruit_8x8matrix KEYWORD1
Adafruit_BicolorMatrix KEYWORD1
Adafruit_7segment KEYWORD1
Adafruit_AlphaNum4 KEYWORD1

#######################################
# Methods and Functions
#######################################

begin KEYWORD2
setDisplayState KEYWORD2
setBrightness KEYWORD2
setPixelColor KEYWORD2
blinkRate KEYWORD2
writeDisplay KEYWORD2
clear KEYWORD2
isKeyPressed KEYWORD2
wasKeyPressed KEYWORD2
justPressed KEYWORD2
getPixels KEYWORD2
justReleased KEYWORD2
readSwitches KEYWORD2
writeDigitRaw KEYWORD2
writeDigitAscii KEYWORD2


#######################################
# Constants
#######################################

ALPHANUM_SEG_A LITERAL1
ALPHANUM_SEG_B LITERAL1
ALPHANUM_SEG_C LITERAL1
ALPHANUM_SEG_D LITERAL1
ALPHANUM_SEG_E LITERAL1
ALPHANUM_SEG_F LITERAL1
ALPHANUM_SEG_G1 LITERAL1
ALPHANUM_SEG_G2 LITERAL1
ALPHANUM_SEG_H LITERAL1
ALPHANUM_SEG_J LITERAL1
ALPHANUM_SEG_K LITERAL1
ALPHANUM_SEG_L LITERAL1
ALPHANUM_SEG_M LITERAL1
ALPHANUM_SEG_N LITERAL1
ALPHANUM_SEG_DP LITERAL1

HT16K33_BLINK_CMD LITERAL1
HT16K33_BLINK_DISPLAYON LITERAL1
HT16K33_BLINK_OFF LITERAL1
HT16K33_BLINK_2HZ LITERAL1
HT16K33_BLINK_1HZ LITERAL1
HT16K33_BLINK_HALFHZ LITERAL1
6 changes: 3 additions & 3 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=Adafruit LED Backpack Library
version=1.5.0
version=1.6.0
author=Adafruit
maintainer=Adafruit <info@adafruit.com>
sentence=Adafruit LED Backpack Library for our 8x8 matrix and 7-segment LED backpacks
paragraph=Adafruit LED Backpack Library for our 8x8 matrix and 7-segment LED backpacks
sentence=Adafruit LED Backpack Library for our 8x8 matrix, 7-segment and 14-segments LED backpacks
paragraph=Adafruit LED Backpack Library for our 8x8 matrix, 7-segment and 14-segments LED backpacks
category=Display
depends=Adafruit GFX Library, WaveHC, RTClib, Adafruit GPS Library
url=https://github.com/adafruit/Adafruit_LED_Backpack
Expand Down
Loading