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 #83

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 92 additions & 32 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 @@ -259,9 +280,8 @@ static const PROGMEM uint16_t alphafonttable[] = {
0b0001001000000000, // |
0b0010010010001001, // }
0b0000010100100000, // ~
0b0011111111111111,

};
0b0011111111111111 };


void Adafruit_LEDBackpack::setDisplayState(bool state) {
uint8_t buffer;
Expand All @@ -287,16 +307,15 @@ void Adafruit_LEDBackpack::blinkRate(uint8_t b) {
}

Adafruit_LEDBackpack::Adafruit_LEDBackpack(void) {}

bool Adafruit_LEDBackpack::begin(uint8_t _addr, TwoWire *theWire) {
if (i2c_dev)
delete i2c_dev;
i2c_dev = new Adafruit_I2CDevice(_addr, theWire);
if (!i2c_dev->begin())
return false;

i2c_addr = _addr;
// turn on oscillator
uint8_t buffer[1] = {0x21};
uint8_t buffer[1] = { 0x21 };
i2c_dev->write(buffer, 1);

// internal RAM powers up with garbage/random values.
Expand All @@ -305,11 +324,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 +338,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 +347,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 < 0 or group > 2 or k > 12)
return false;
return (keys[group] & _BV(k));
}

bool Adafruit_LEDBackpack::wasKeyPressed(uint8_t group, uint8_t k) {
if (group < 0 or 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 +406,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 @@ -643,7 +704,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 @@ -783,8 +843,8 @@ void Adafruit_7segment::printFloat(double n, uint8_t fracDigits, uint8_t base) {
}

// did toIntFactor shift the decimal off the display?
if (toIntFactor < 1) {
printError();
if (toIntFactor < 1) {
printError();
} else {
// otherwise, display the number
int8_t displayPos = 4;
Expand Down
63 changes: 55 additions & 8 deletions Adafruit_LEDBackpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ See https://learn.adafruit.com/14-segment-alpha-numeric-led-featherwing/usage
/*!
@brief Class encapsulating the raw HT16K33 controller device.
*/
class Adafruit_LEDBackpack {
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,14 +145,48 @@ 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
};

/*!
@brief Class for 24-element bargraph displays.
*/
class Adafruit_24bargraph : public Adafruit_LEDBackpack {
class Adafruit_24bargraph : public Adafruit_LEDBackpack
{
public:
/*!
@brief Constructor for 24-element bargraph displays.
Expand All @@ -164,7 +204,8 @@ class Adafruit_24bargraph : public Adafruit_LEDBackpack {
/*!
@brief Class for 8x16 pixel single-color matrices.
*/
class Adafruit_8x16matrix : public Adafruit_LEDBackpack, public Adafruit_GFX {
class Adafruit_8x16matrix : public Adafruit_LEDBackpack, public Adafruit_GFX
{
public:
/*!
@brief Constructor for 8x16 pixel single-color matrices.
Expand All @@ -186,7 +227,8 @@ class Adafruit_8x16matrix : public Adafruit_LEDBackpack, public Adafruit_GFX {
@brief Class for 8x16 pixel single-color mini matrices.
*/
class Adafruit_8x16minimatrix : public Adafruit_LEDBackpack,
public Adafruit_GFX {
public Adafruit_GFX
{
public:
/*!
@brief Constructor for 8x16 pixel single-color mini matrices.
Expand All @@ -207,7 +249,8 @@ class Adafruit_8x16minimatrix : public Adafruit_LEDBackpack,
/*!
@brief Class for 8x8 pixel single-color matrices.
*/
class Adafruit_8x8matrix : public Adafruit_LEDBackpack, public Adafruit_GFX {
class Adafruit_8x8matrix : public Adafruit_LEDBackpack, public Adafruit_GFX
{
public:
/*!
@brief Constructor for 8x8 pixel single-color matrices.
Expand All @@ -229,7 +272,8 @@ class Adafruit_8x8matrix : public Adafruit_LEDBackpack, public Adafruit_GFX {
@brief Class for bi-color matrices.
*/
class Adafruit_BicolorMatrix : public Adafruit_LEDBackpack,
public Adafruit_GFX {
public Adafruit_GFX
{
public:
/*!
@brief Constructor for 8x8 pixel bi-color matrices.
Expand All @@ -252,7 +296,8 @@ class Adafruit_BicolorMatrix : public Adafruit_LEDBackpack,
/*!
@brief Class for 7-segment numeric displays.
*/
class Adafruit_7segment : public Adafruit_LEDBackpack {
class Adafruit_7segment : public Adafruit_LEDBackpack
{
public:
/*!
@brief Constructor for 7-segment numeric displays.
Expand Down Expand Up @@ -463,7 +508,9 @@ class Adafruit_7segment : public Adafruit_LEDBackpack {
/*!
@brief Class for four-digit alphanumeric displays.
*/
class Adafruit_AlphaNum4 : public Adafruit_LEDBackpack {
class Adafruit_AlphaNum4 : public Adafruit_LEDBackpack
{

public:
/*!
@brief Constructor for four-digit alphanumeric displays.
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
Loading
Loading