From 96654ea19b12be4b43230f3ff9f908eebcb416ca Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Wed, 31 Aug 2011 19:45:00 +0100 Subject: Initial commit for the modified libaries used and the ButtonMatrix arduino code. I have just cleaned this up, but not tested it. --- Arduino/ButtonMatrix/ButtonMatrix.pde | 62 ++ Arduino/Libraies/AS1107/AS1107.cpp | 769 +++++++++++++++++++++ Arduino/Libraies/AS1107/AS1107.h | 147 ++++ Arduino/Libraies/AS1107/README | 1 + .../AS1107/examples/LEDMatrix/LEDMatrix.pde | 131 ++++ Arduino/Libraies/AS1107/keywords.txt | 39 ++ .../Keypad/Examples/CustomKeypad/CustomKeypad.pde | 37 + .../Examples/DynamicKeypad/DynamicKeypad.pde | 187 +++++ .../Keypad/Examples/EventKeypad/EventKeypad.pde | 72 ++ .../Keypad/Examples/HelloKeypad/HelloKeypad.pde | 35 + Arduino/Libraies/Keypad/Keypad.cpp | 174 +++++ Arduino/Libraies/Keypad/Keypad.h | 108 +++ Arduino/Libraies/Keypad/README | 1 + Arduino/Libraies/Keypad/keywords.txt | 17 + 14 files changed, 1780 insertions(+) create mode 100755 Arduino/ButtonMatrix/ButtonMatrix.pde create mode 100755 Arduino/Libraies/AS1107/AS1107.cpp create mode 100755 Arduino/Libraies/AS1107/AS1107.h create mode 100644 Arduino/Libraies/AS1107/README create mode 100755 Arduino/Libraies/AS1107/examples/LEDMatrix/LEDMatrix.pde create mode 100755 Arduino/Libraies/AS1107/keywords.txt create mode 100755 Arduino/Libraies/Keypad/Examples/CustomKeypad/CustomKeypad.pde create mode 100755 Arduino/Libraies/Keypad/Examples/DynamicKeypad/DynamicKeypad.pde create mode 100755 Arduino/Libraies/Keypad/Examples/EventKeypad/EventKeypad.pde create mode 100755 Arduino/Libraies/Keypad/Examples/HelloKeypad/HelloKeypad.pde create mode 100755 Arduino/Libraies/Keypad/Keypad.cpp create mode 100755 Arduino/Libraies/Keypad/Keypad.h create mode 100644 Arduino/Libraies/Keypad/README create mode 100755 Arduino/Libraies/Keypad/keywords.txt diff --git a/Arduino/ButtonMatrix/ButtonMatrix.pde b/Arduino/ButtonMatrix/ButtonMatrix.pde new file mode 100755 index 0000000..85a9bfa --- /dev/null +++ b/Arduino/ButtonMatrix/ButtonMatrix.pde @@ -0,0 +1,62 @@ +#include // Needs to be the modified version + +const byte ROWS = 19; +const byte COLS = 8; + +/* Each of the buttons can be refered to by a number + At the moment the byte overflow for the 127+ numbers is dealt with at the + getKey time, but if a unsigned byte could be used here it would remove the need + for the correction? +*/ +char keys[COLS][ROWS] = { +{1, 9, 17, 25, 33, 41, 49, 57, 65, 73, 81, 89, 97, 105, 113, 121, 129, 137, 145}, +{2, 10, 18, 26, 34, 42, 50, 58, 66, 74, 82, 90, 98, 106, 114, 122, 130, 138, 146}, +{3, 11, 19, 27, 35, 43, 51, 59, 67, 75, 83, 91, 99, 107, 115, 123, 131, 139, 147}, +{4, 12, 20, 28, 36, 44, 52, 60, 68, 76, 84, 92, 100, 108, 116, 124, 132, 140, 148}, +{5, 13, 21, 29, 37, 45, 53, 61, 69, 77, 85, 93, 101, 109, 117, 125, 133, 141, 149}, +{6, 14, 22, 30, 38, 46, 54, 62, 70, 78, 86, 94, 102, 110, 118, 126, 134, 142, 150}, +{7, 15, 23, 31, 39, 47, 55, 63, 71, 79, 87, 95, 103, 111, 119, 127, 135, 143, 151}, +{8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152} +}; + +byte colPins[COLS] = {9, 8, 7, 6, 5, 4, 3, 2}; +byte rowPins[ROWS] = {14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42}; + +/* + Becuase of the diodes being fitted the WRONG WAY ROUND, + the columns are rows and the rows are columns, have fun! +*/ +// ROW COL ROWS COLS +Keypad keypad = Keypad( makeKeymap(testKeysTwo), colPins, rowPins, COLS, ROWS); + +void setup(){ + Serial.begin(115200); + Serial.println("Go"); + +} +/* Send numbers back and forth to test the integrity of the serial communication + Computer sends the first byte +*/ +boolean runSerialTest() { + Serial.println("Begining Serial Test"); + delay(10); + byte sByte cByte; + for (int i=0; i<10000; i++) { + sByte = Serial.read(); + if (sByte != cByte && i != 0) return false; + sByte = ((sByte * 10) % 127); + Serial.write(sByte); + cByte = ((sByte * 10) % 127); // The next expected byte + } + return true; +} + +void loop(){ + // Serial.println("GetKeyCall"); + char customKey = customKeypad.getKey(); + int customKeyInt = customKey; + if (customKeyInt < 0) customKeyInt += 256; + if (customKey != NO_KEY){ + Serial.println(customKeyInt); + } +} diff --git a/Arduino/Libraies/AS1107/AS1107.cpp b/Arduino/Libraies/AS1107/AS1107.cpp new file mode 100755 index 0000000..c61819e --- /dev/null +++ b/Arduino/Libraies/AS1107/AS1107.cpp @@ -0,0 +1,769 @@ +/* + AS1107.cpp - Library for interfacing the AS1107 LED Driver Chip + Created by Stephan Elsner, March 29, 2011 + + Scrolltext uses a proportional character font + Tested with Arduino Duemilenove + version 0.82 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include +#include "WProgram.h" +#include "AS1107.h" + +const byte AS1107::_repcharoff = 27; + +// Font Data +PROGMEM prog_uchar _font[] = { + // -------- Space + 0b00000000, + 0b00000000, + 0b00000000, + 0b00000000, + // -------- A + 0b01111110, + 0b10010000, + 0b10010000, + 0b01111110, + // -------- B + 0b01101100, + 0b10010010, + 0b10010010, + 0b11111110, + // -------- C + 0b10000010, + 0b10000010, + 0b01111100, + // -------- D + 0b00111000, + 0b01000100, + 0b10000010, + 0b11111110, + // -------- E + 0b10000010, + 0b10010010, + 0b11111110, + // -------- F + 0b10000000, + 0b10010000, + 0b11111110, + // -------- G + 0b01011100, + 0b10010010, + 0b10000010, + 0b01111100, + // -------- H + 0b11111110, + 0b00010000, + 0b00010000, + 0b11111110, + // -------- I + 0b10000010, + 0b11111110, + 0b10000010, + // -------- J + 0b11111100, + 0b00000010, + 0b00001100, + // -------- K + 0b10000110, + 0b01001000, + 0b00110000, + 0b11111110, + // -------- L + 0b00000010, + 0b00000010, + 0b11111110, + // -------- M + 0b11111110, + 0b01100000, + 0b00111100, + 0b01100000, + 0b11111110, + // -------- N + 0b11111110, + 0b00011000, + 0b01100000, + 0b11111110, + // -------- O + 0b01111100, + 0b10000010, + 0b10000010, + 0b01111100, + // -------- P + 0b01100000, + 0b10010000, + 0b10010000, + 0b11111110, + // -------- Q + 0b01111010, + 0b10000100, + 0b10001010, + 0b01111100, + // -------- R + 0b01100110, + 0b10011000, + 0b10010000, + 0b11111110, + // -------- S + 0b10001100, + 0b10010010, + 0b01100010, + // -------- T + 0b10000000, + 0b11111110, + 0b10000000, + // -------- U + 0b11111100, + 0b00000010, + 0b00000010, + 0b11111100, + // -------- V + 0b11000000, + 0b00111000, + 0b00000110, + 0b00111000, + 0b11000000, + // -------- W + 0b11111110, + 0b00001100, + 0b00111000, + 0b00001100, + 0b11111110, + // -------- X + 0b11000110, + 0b00111000, + 0b00111000, + 0b11000110, + // -------- Y + 0b11100000, + 0b00011110, + 0b11100000, + // -------- Z + 0b11000010, + 0b10110010, + 0b10001110, + // -------- Unknown character + 0b00111000, + 0b00111000, + 0b00111000, + // -------- 0 + 0b01111100, + 0b10100010, + 0b10010010, + 0b01111100, + // -------- 1 + 0b11111110, + 0b01000000, + // -------- 2 + 0b01100010, + 0b10010010, + 0b10001110, + // -------- 3 + 0b01101100, + 0b10010010, + 0b10000010, + // -------- 4 + 0b11111110, + 0b00010000, + 0b11110000, + // -------- 5 + 0b10001100, + 0b10010010, + 0b11110010, + // -------- 6 + 0b01001100, + 0b10010010, + 0b10010010, + 0b01111100, + // -------- 7 + 0b11100000, + 0b10011110, + 0b10000000, + // -------- 8 + 0b01101100, + 0b10010010, + 0b10010010, + 0b01101100, + // -------- 9 + 0b01111100, + 0b10010010, + 0b10010010, + 0b01100100, + // -------- : + 0b00100100, + // -------- ; + 0b00100110, + 0b00000001, + // -------- ! + 0b01100000, + 0b11111010, + 0b01100000, + // -------- Heart + 0b01111000, + 0b11111100, + 0b11111110, + 0b01111111, + 0b11111110, + 0b11111100, + 0b01111000, + // -------- < + 0b01000100, + 0b00101000, + 0b00010000, + // -------- = + 0b00101000, + 0b00101000, + 0b00101000, + 0b00101000, + // -------- > + 0b00010000, + 0b00101000, + 0b01000100, + // -------- ? + 0b01100000, + 0b10011010, + 0b10000000, + // -------- @ + 0b01111100, + 0b10000010, + 0b10111010, + 0b10100010, + 0b01011100, + // -------- ( + 0b10000010, + 0b01111100, + // -------- ) + 0b01111100, + 0b10000010, + // -------- * + 0b00101000, + 0b00010000, + 0b00101000, + // -------- + + 0b00010000, + 0b00010000, + 0b01111100, + 0b00010000, + 0b00010000, + // -------- , + 0b00000110, + 0b00000001, + // -------- - + 0b00010000, + 0b00010000, + 0b00010000, + 0b00010000, + // -------- . + 0b00000010, + // -------- / + 0b11000000, + 0b00111000, + 0b00000110, + // -------- a + 0b00111110, + 0b00100010, + 0b00100010, + 0b00011100, + // -------- b + 0b00011100, + 0b00100010, + 0b00100010, + 0b11111110, + // -------- c + 0b00100010, + 0b00100010, + 0b00011100, + // -------- d + 0b11111110, + 0b00100010, + 0b00100010, + 0b00011100, + // -------- e + 0b00011000, + 0b00101010, + 0b00101010, + 0b00011100, + // -------- f + 0b10010000, + 0b01111110, + 0b00010000, + // -------- g + 0b00111110, + 0b00100101, + 0b00100101, + 0b00011000, + // -------- h + 0b00011110, + 0b00100000, + 0b00100000, + 0b11111110, + // -------- i + 0b00000010, + 0b01011110, + 0b00010010, + // -------- j + 0b01011110, + 0b00000001, + 0b00000001, + // -------- k + 0b00100010, + 0b00010100, + 0b00001000, + 0b11111110, + // -------- l + 0b00000010, + 0b11111100, + // -------- m + 0b00011110, + 0b00100000, + 0b00111110, + 0b00100000, + 0b00111110, + // -------- n + 0b00011110, + 0b00100000, + 0b00100000, + 0b00111110, + // -------- o + 0b00011100, + 0b00100010, + 0b00100010, + 0b00011100, + // -------- p + 0b00011100, + 0b00100010, + 0b00100010, + 0b00111111, + // -------- q + 0b00111111, + 0b00100010, + 0b00100010, + 0b00011100, + // -------- r + 0b00010000, + 0b00100000, + 0b00111110, + // -------- s + 0b00100100, + 0b00101010, + 0b00101010, + 0b00010010, + // -------- t + 0b00100010, + 0b11111100, + 0b00100000, + // -------- u + 0b00111110, + 0b00000010, + 0b00000010, + 0b00111100, + // -------- v + 0b00111000, + 0b00000110, + 0b00111000, + // -------- w + 0b00111110, + 0b00000010, + 0b00011110, + 0b00000010, + 0b00111100, + // -------- x + 0b00110110, + 0b00001000, + 0b00110110, + // -------- y + 0b00111110, + 0b00000101, + 0b00000101, + 0b00111001, + // -------- z + 0b00110010, + 0b00101010, + 0b00100110, + 0b00100010, + 0b11000001 +}; + +// Char width table +PROGMEM prog_uchar _charwidth[] = {4,4,4,3,4,3,3,4,4,3,3,4,3,5,4,4,4,4,4,3,3,4,5,5,4,3,3, + 3,4,2,3,3,3,3,4,3,4,4,1,2,3,7,3,4,3,3,5,2,2,3,5,2,4,1,3, + 4,4,3,4,4,3,4,4,3,3,4,2,5,4,4,4,4,3,4,3,4,3,5,3,4,4,0 }; + +// ASCII Codes of the implemented characters +PROGMEM prog_uchar _charcodes[] = {32,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80, + 81,82,83,84,85,86,87,88,89,90,255,48,49,50,51,52,53, + 54,55,56,57,58,59,33,3,60,61,62,63,64,40,41,42,43,44, + 45,46,47,97,98,99,100,101,102,103,104,105,106,107,108, + 109,110,111,112,113,114,115,116,117,118,119,120,121, + 122,0}; +// ------------------------------------------------------------------------------------ +// Constructor, initialize arduino +AS1107::AS1107(byte cspin, byte clkpin, byte datapin) +{ + _cspin = cspin; + _clkpin = clkpin; + _datapin = datapin; + pinMode(cspin, OUTPUT); + pinMode(clkpin, OUTPUT); + pinMode(datapin, OUTPUT); + digitalWrite(cspin,HIGH); + digitalWrite(clkpin,LOW); + digitalWrite(datapin,LOW); +} + +// ------------------------------------------------------------------------------------ +// Initializes Arduino and AS1107 +// buffer is the initial screen buffer +void AS1107::Init(byte * buffer) +{ + _buffer = buffer; + CalculateCharOffsets(); + // Clear Screen buffer + Clear(); + for (int i=0; i<=HighestCtrlNum; i++) { + WriteRegister(i, Registers::Shutdown, ShutdownModes::Normal); + WriteRegister(i, Registers::DecodeMode, 0x00); + WriteRegister(i, Registers::IntensityControl, 0x00); + WriteRegister(i, Registers::ScanLimit, 0x07); + } +} + +// ------------------------------------------------------------------------------------ +// Set the Screen buffer +void AS1107::SetBuffer(byte * buffer) +{ + _buffer = buffer; +} + +// ------------------------------------------------------------------------------------ +// Writes Data to a Register of the AS1106/AS1107 +// chip: Number of the controller chip (starting at 0 for the left) +// reg: Register of the controller +// data: Data to be written +void AS1107::WriteRegister( byte chip, byte reg, byte data) +{ + if (chip>=0 && chip <= HighestCtrlNum) { + chip = HighestCtrlNum -chip; + digitalWrite(_cspin,LOW); + for (int i=0; i<=HighestCtrlNum; i++) { + if (i == chip) { + shiftOut(_datapin, _clkpin, MSBFIRST, reg); + shiftOut(_datapin, _clkpin, MSBFIRST, data); + } + else { + shiftOut(_datapin, _clkpin, MSBFIRST, Registers::NoOp); + shiftOut(_datapin, _clkpin, MSBFIRST, 0); + } + } + digitalWrite(_cspin,HIGH); + } +} + +// ------------------------------------------------------------------------------------ +// Writes a column of data to the LED-Matrix +// column: column number, starting at 0 +// data : screen data to be written +void AS1107::WriteColumn( byte column, byte data) +{ + // if you use unusual matrix row wiring, you can manipulate data here + byte chip = (column) >>3; + byte reg = (column % 8) +1; + WriteRegister(chip, reg, data); +} + +// ------------------------------------------------------------------------------------ +// Sets the status of a LED in the screen buffer +void AS1107::SetLed(int x, int y, byte value) +{ + if (x<=Maxx && y<=Maxy && x>=0 && y>=0) { + if (value>0) value = 1; + SetLedInternal(x, y, value); + } +} + +// ------------------------------------------------------------------------------------ +// Writes Buffer to Screen +// needs to be called after a graphics operation to see anything +void AS1107::Update() +{ + for (byte i=0; i<=Maxx; i++) { + WriteColumn(i, _buffer[i]); + } +} + +// ------------------------------------------------------------------------------------ +// Clear the Screen Buffer +void AS1107::Clear() +{ + for (byte i=0; i<=Maxx; _buffer[i++]=0); +} + +// ------------------------------------------------------------------------------------ +// Shift the content of the Screen buffer to the left +void AS1107::ShiftLeft() +{ + for (byte i=1; i<=Maxx; i++) { + _buffer[i-1] = _buffer[i]; + } + _buffer[Maxx] = 0; +} +// ------------------------------------------------------------------------------------ +// Shift the content of the Screen buffer to the right +void AS1107::ShiftRight() +{ + for (byte i=Maxx; i>=1; i--) { + _buffer[i] = _buffer[i-1]; + } + _buffer[0] = 0; +} +// ------------------------------------------------------------------------------------ +// Shift the content of the Screen buffer up +void AS1107::ShiftUp() +{ + for (byte i=0; i<=Maxx; i++) { + _buffer[i] = _buffer[i]<<1 & 0xFE; + } +} +// ------------------------------------------------------------------------------------ +// Shift the content of the Screen buffer up +void AS1107::ShiftUp(byte from, byte to) +{ + for (byte i=from; i<=to; i++) { + _buffer[i] = _buffer[i]<<1 & 0xFE; + } +} +// ------------------------------------------------------------------------------------ +// Shift the content of the Screen buffer down +void AS1107::ShiftDown() +{ + for (byte i=0; i<=Maxx; i++) { + _buffer[i] = _buffer[i]>>1 & 0x7F; + } +} +// ------------------------------------------------------------------------------------ +// Shift the content of the Screen buffer down +void AS1107::ShiftDown(byte from, byte to) +{ + for (byte i=from; i<=to; i++) { + _buffer[i] = _buffer[i]>>1 & 0x7F; + } +} +// ------------------------------------------------------------------------------------ +// draws a line from x0 ,y0 to x1,y1 +void AS1107::DrawLine(int x0, int y0, int x1, int y1) +{ + + int dx = abs(x1-x0), sx = x0= dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */ + if (e2 <= dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */ + } +} +// ------------------------------------------------------------------------------------ +// draws a box from x0,x0 to x1,y1 +void AS1107::DrawBox(int x0, int y0, int x1, int y1) +{ + DrawLine(x0,y0,x1,y0); + DrawLine(x1,y0,x1,y1); + DrawLine(x1,y1,x0,y1); + DrawLine(x0,y1,x0,y0); +} +// ------------------------------------------------------------------------------------ +// draws an ellipse +// center xm, ym - radius a,b +void AS1107::DrawEllipse(int xm, int ym, int a, int b) +{ + if (a ==0 && b== 0) { + SetLed(xm,ym,ON); + return; + } + + int dx = 0, dy = b; /* first quadrant from top left to bottom right */ + int a2 = a*a, b2 = b*b; + int err = b2-(2*b-1)*a2, e2; /* error value in the first step */ + + do { + SetLed(xm+dx, ym+dy, ON); /* I. Quadrant */ + SetLed(xm-dx, ym+dy, ON); /* II. Quadrant */ + SetLed(xm-dx, ym-dy, ON); /* III. Quadrant */ + SetLed(xm+dx, ym-dy, ON); /* IV. Quadrant */ + + e2 = 2*err; + if (e2 < (2*dx+1)*b2) { dx++; err += (2*dx+1)*b2; } + if (e2 > -(2*dy-1)*a2) { dy--; err -= (2*dy-1)*a2; } + } while (dy >= 0); + + while (dx++ < a) { /* correction for flat ellipses (b=1) */ + SetLed(xm+dx, ym, ON); + SetLed(xm-dx, ym, ON); + } +} + +// ------------------------------------------------------------------------------------ +// Writes Text at a screen position +void AS1107::DrawText (int x, int y, char *str) +{ + byte count; + char thechar; + while (*str != '\0') { + Serial.println(*str,DEC); + x+=DrawChar(x, y, *str); + str++; + } +} + +// ------------------------------------------------------------------------------------ +// Draws a single character at a screen position +// returns the x-position for the next character +byte AS1107::DrawChar (int x, int y, char thechar) +{ + y+=7; + byte charnum, colct, count=0, colbits; + int fontidx; + charnum = GetInternalCharCode(thechar); + + // get the location of the first column of the font + fontidx = _charoffset[charnum]-1; + + // get the width of the font + colct = pgm_read_byte_near(_charwidth + charnum); + for (byte i=colct; i>0; i--) { + colbits = pgm_read_byte_near(_font + fontidx + i); + for (byte j=0; j<=7; j++) { + if ((colbits >>j) & 1) { + SetLed(x, y-j, 1); + } + } + x++; + } + return colct+1; +} +// ------------------------------------------------------------------------------------ +// Sets the Text for Scrolline +void AS1107::SetScrollText(char *string, int charspacing) +{ + _charspacing = charspacing; + _outputstring = string; + _curchr = string; + _hastext = true; + _colct =-1; +} + +// ------------------------------------------------------------------------------------ +// Scrolls the text one pixel to the left +// returns true if last character has been displayed +boolean AS1107::TextScroll() +{ + boolean lastchar = false; + byte colbits = 0; + if (_colct == (0-_charspacing-1)) { + lastchar = NextCharacter(); + } + if (_colct>=0) { + colbits = pgm_read_byte_near(_font + _fontidx + _colct); + } + ShiftLeft(); + _buffer[Maxx] = colbits; + Update(); + _colct--; + return lastchar; +} + +// ------------------------------------------------------------------------------------ +// PRIVATE (Internal) functions +// ------------------------------------------------------------------------------------ +// Set LED without Parameter check +void AS1107::SetLedInternal(byte x, byte y, byte value) +{ + if (value != 0) { + _buffer[x] |= value << (Maxy-y); + } else { + _buffer[x] &= ~(1 << (Maxy-y)); + } +} + +// ------------------------------------------------------------------------------------ +// fetches the next character of the Scrollline +// returns true if string is at the end +boolean AS1107::NextCharacter() +{ + boolean lastchar = false; + if (_hastext) { + char thechar = *_curchr; + boolean found = false; + byte charnum = 0; + byte i; + // if the string ends, start from the beginning + if (thechar == 0) { + lastchar = true; + _curchr = _outputstring; + thechar = *_curchr; + } + + charnum = GetInternalCharCode(thechar); + + // get the offset of the first column of the character + _fontidx = _charoffset[charnum]; + + // get the width of the font + _colct = pgm_read_byte_near(_charwidth + charnum) -1; + } + else { + _fontidx= _charoffset[_repcharoff]; + _colct = pgm_read_byte_near(_charwidth + _repcharoff); + } + *_curchr++; + return lastchar; +} +// ------------------------------------------------------------------------------------ +// calculates Character offsets from font width table in EEPROM +void AS1107::CalculateCharOffsets() +{ + int off =0; + int idx=0; + int w=0; + int i=0; + + do { + _charoffset[i]=off; + w = pgm_read_byte_near(_charwidth+i); + off += w; + i++; + } while (w != 0); +} + +// ------------------------------------------------------------------------------------ +// gets the character number of the built-in-font from the ASCII table +// returns number of the replacement character for unknown characters +byte AS1107::GetInternalCharCode(byte thechar) +{ + int i=0; + int charnum; + do { + charnum = pgm_read_byte_near(_charcodes + i); + if (charnum == thechar) break; + i++; + } while (charnum != 0 ); + if (charnum == 0) i = _repcharoff; + return i; +} \ No newline at end of file diff --git a/Arduino/Libraies/AS1107/AS1107.h b/Arduino/Libraies/AS1107/AS1107.h new file mode 100755 index 0000000..60ad6ad --- /dev/null +++ b/Arduino/Libraies/AS1107/AS1107.h @@ -0,0 +1,147 @@ +/* + AS1107.h - Library for interfacing the AS1107 LED Driver Chip + Created by Stephan Elsner 2011 + version 0.82 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef AS1107_h +#define AS1107_h + +#include "WProgram.h" +#include + +#define ON 1 +#define OFF 0 + + +// -------------------------------------------------------------------------------------------------------------------------- +class AS1107 +{ + public: + AS1107(byte cspin, byte clkpin, byte datapin); + void WriteRegister2(byte chip, byte reg, byte data); + void WriteRegister(byte chip, byte reg, byte data); + void WriteColumn( byte column, byte data); + void Init(byte *buffer); + void SetBuffer(byte *buffer); + void SetLed(int x, int y, byte value); + void Clear(); + void Update(); + void ShiftLeft(); + void ShiftRight(); + void ShiftUp(); + void ShiftDown(); + void ShiftUp(byte from, byte to); + void ShiftDown(byte from, byte to); + void DrawLine(int x0, int y0, int x1, int y1); + void DrawBox(int x0, int y0, int x1, int y1); + void DrawEllipse(int xm, int ym, int a, int b); + void SetScrollText(char *string, int charspacing); + void DrawText (int x, int y, char *str); + byte DrawChar (int x, int y, char thechar); + + boolean TextScroll(); + + // *************************************** + // Set your module configuration here + // *************************************** + static const byte Maxx=15; // maximum x - Pixels of the Module + static const byte Maxy=7; // maximum y - Pixels of the Module + static const byte HighestCtrlNum=1; // Number of Matrix modules -1 (0 = single module) + + private: + byte _cspin; + byte _clkpin; + byte _datapin; + byte *_buffer; // pointer to current screen buffer + + static const byte _repcharoff; // Offset of the replacement for unknown characers + int _charoffset[82]; // Start position in Array + char *_outputstring; // pointer to start of output string + char *_curchr; // pointer to current char of output String + int _fontidx; // current byte in font to display + int _colct; // to count the columns of character to display + byte _charspacing; // the coulumns of space between characters + boolean _hastext; + + void SetLedInternal(byte x, byte y, byte value); + boolean NextCharacter(); + void CalculateCharOffsets(); + byte GetInternalCharCode(byte thechar); +}; + +// -------------------------------------------------------------------------------------------------------------------------- +// AS1107-Register +class Registers +{ + public: + static const byte NoOp = 0x00; + static const byte Digit0 = 0x01; + static const byte Digit1 = 0x02; + static const byte Digit2 = 0x03; + static const byte Digit3 = 0x04; + static const byte Digit4 = 0x05; + static const byte Digit5 = 0x06; + static const byte Digit6 = 0x07; + static const byte Digit7 = 0x08; + static const byte DecodeMode = 0x09; + static const byte IntensityControl = 0x0A; + static const byte ScanLimit = 0x0B; + static const byte Shutdown = 0x0C; + static const byte Feature = 0x0E; + static const byte DisplayTest = 0x0F; +}; + +// -------------------------------------------------------------------------------------------------------------------------- +// Values to write to the Shutdown Register +class ShutdownModes +{ + public: + static const byte ShutdownResetFeature = 0x00; // Shutdown Mode, Reset Feature Register to Default Settings + static const byte Shutdown = 0x80; // Shutdown Mode, leaving Feature Register unchanged + static const byte NormalResetFeature = 0x01; // Normal Mode, Reset Feature + static const byte Normal = 0x81; // Normal Mode, Feature unchanged +}; + +// Bits in the feature-Register, desired Settings must be OR'ed together +class Features +{ + public: + static const byte ExternalClock = 0x01; // ExternalClock active + static const byte ResetRegisters = 0x02; // Resets all control registers except the Feature Register. + static const byte HexDecoding = 0x04; // 1 = Enable HEX decoding, 0 = Enable Code-B decoding + static const byte SPIEnable = 0x08; // Enables the SPI-compatible interface.(AS1106 only). + static const byte BlinkEnable = 0x10; // Enables blinking. + static const byte BlinkSlow = 0x20; // Sets blink with low frequency + // (with the internal oscillator enabled) + + static const byte BlinkSync = 0x40; // Synchronizes blinking on the rising edge of pin LOAD/CSN. + // The multiplex and blink timing counter is cleared on the + // rising edge of pin LOAD/CSN. By setting this bit in + // multiple AS1106/AS1107 devices, the blink timing can + // be synchronized across all the devices. + + static const byte BlinkStartWithOn = 0x80; // Start Blinking with display enabled phase. + // When bit D4 (blink_en) is set, bit D7 + // determines how blinking starts. + // 0 = Blinking starts with the display turned off. + // 1 = Blinking starts with the display turned on. +}; + +#endif + + \ No newline at end of file diff --git a/Arduino/Libraies/AS1107/README b/Arduino/Libraies/AS1107/README new file mode 100644 index 0000000..3e1dea9 --- /dev/null +++ b/Arduino/Libraies/AS1107/README @@ -0,0 +1 @@ +Changed SetLed so that it can turn LED's off as well as on. diff --git a/Arduino/Libraies/AS1107/examples/LEDMatrix/LEDMatrix.pde b/Arduino/Libraies/AS1107/examples/LEDMatrix/LEDMatrix.pde new file mode 100755 index 0000000..377331d --- /dev/null +++ b/Arduino/Libraies/AS1107/examples/LEDMatrix/LEDMatrix.pde @@ -0,0 +1,131 @@ +// test program for three cascaded AS1107 +// if you got a different configuration you need to +// modify the library itself. + +#include + +// Arduino pins +const byte CsnPin = 2 ; // Chip select (Low active) +const byte ClkPin = 3 ; // Serial Clock +const byte DataPin = 4 ; // Serial Data + +byte buffer [ 24 ] ; // Screen buffer (No. of modules * 8) +AS1107 matrix ( CsnPin, ClkPin, DataPin ) ; + +const byte d = 0 ; +int count= 1 ; +void setup ( ) +{ +matrix. Init ( buffer ) ; +matrix. SetScrollText ( " Three 8x8 LED dot matrix modules driven by three AS1107 LED controllers using just four I/O pins of the Arduino for serial data. That is surprisingly fast, look:" , 1 ) ; +} + +void loop ( ) +{ +if ( matrix. TextScroll ( ) ) { +delay ( 2000 ) ; +for ( int i= 0 ; i<= 7 ; i++ ) { +matrix. ShiftUp ( ) ; +delay ( 50 ) ; +matrix. Update ( ) ; +} +for ( int i= 0 ; i<= 10 ; i++ ) { +CirclePulsate ( ) ; +} +StarsDown ( ) ; +for ( int i= 0 ; i<= 5 ; i++ ) { +Rotator1 ( ) ; +} +StarsRight ( ) ; +for ( int i= 0 ; i<=AS1107:: Maxx ; i++ ) { +matrix. ShiftRight ( ) ; +delay ( 5 ) ; +matrix. Update ( ) ; +} +for ( int i= 1 ; i<= 3 ; i++ ) +{ +bounce ( ) ; +} +} +delay ( 10 ) ; +} + +void CirclePulsate ( ) +{ +for ( int i= 0 ; i<= 15 ; i++ ) { +matrix. Clear ( ) ; +matrix. DrawEllipse ( AS1107:: Maxx / 2 , 4 ,i,i ) ; +matrix. Update ( ) ; +delay ( d ) ; +} +} +void StarsDown ( ) +{ +for ( int i= 0 ; i<= 200 ; i++ ) { +matrix. ShiftDown ( ) ; +matrix. SetLed ( ( byte ) random ( 0 ,AS1107:: Maxx + 1 ) , 0 , 1 ) ; +matrix. Update ( ) ; +delay ( d ) ; +} +} +void StarsRight ( ) +{ +for ( int i= 0 ; i<= 200 ; i++ ) { +matrix. ShiftRight ( ) ; +matrix. SetLed ( 0 , ( byte ) random ( 0 ,AS1107:: Maxy + 1 ) , 1 ) ; +matrix. Update ( ) ; +delay ( d ) ; +} +} +void Rotator1 ( ) +{ +for ( int i= 0 ; i<=AS1107:: Maxx - 1 ; i++ ) { +matrix. Clear ( ) ; +matrix. DrawLine ( i, 0 ,AS1107:: Maxx -i,AS1107:: Maxy ) ; +matrix. Update ( ) ; +delay ( d ) ; +} +for ( int i= 0 ; i<= 6 ; i++ ) { +matrix. Clear ( ) ; +matrix. DrawLine ( AS1107:: Maxx ,i, 0 , 7 -i ) ; +matrix. Update ( ) ; +delay ( d ) ; +} +} + +void bounce ( ) +{ +int rad= 0 ; +float y=random ( 0 ,AS1107:: Maxy + 1 ) ; +float x=random ( 0 ,AS1107:: Maxx + 1 ) ; +float xacc=random ( 0 , 5 ) - 2 ; +float yacc=random ( 0 , 5 ) - 2 ; +float yg= 0 . 08 ; +if ( xacc == 0 ) xacc == 1 ; +if ( yacc == 0 ) yacc == - 1 ; +for ( int i= 1 ; i<= 300 ; i++ ) +{ +x+=xacc; +y+=yacc; +yacc += yg; +if ( x >= ( AS1107:: Maxx -rad ) ) { +xacc = -xacc* 0 . 9 ; +x= AS1107:: Maxx -rad; +} +else if ( x <= rad ) { +xacc = -xacc* 0 . 8 ; +x= rad; +} +if ( y >= ( AS1107:: Maxy -rad ) ) { +yacc = - ( yacc* 0 . 80 ) ; +y= AS1107:: Maxy -rad; +} +else if ( y <= rad ) { +yacc = -yacc* 0 . 8 ; +y= rad; +} +matrix. Clear ( ) ; +matrix. SetLed ( x,y, 1 ) ; +matrix. Update ( ) ; +} +} \ No newline at end of file diff --git a/Arduino/Libraies/AS1107/keywords.txt b/Arduino/Libraies/AS1107/keywords.txt new file mode 100755 index 0000000..06ffc27 --- /dev/null +++ b/Arduino/Libraies/AS1107/keywords.txt @@ -0,0 +1,39 @@ +####################################### +# Syntax Coloring Map For AS1107Graphic +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +AS1107 KEYWORD1 + + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +WriteRegister KEYWORD2 +WriteColumn KEYWORD2 +Init KEYWORD2 +SetLed KEYWORD2 +Clear KEYWORD2 +Update KEYWORD2 +ShiftLeft KEYWORD2 +ShiftRight KEYWORD2 +ShiftUp KEYWORD2 +ShiftDown KEYWORD2 +DrawLine KEYWORD2 +DrawBox KEYWORD2 +DrawEllipse KEYWORD2 +DrawText KEYWORD2 +DrawChar KEYWORD2 +SetScrollText KEYWORD2 +TextScroll KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + +ON LITERAL1 +OFF LITERAL1 \ No newline at end of file diff --git a/Arduino/Libraies/Keypad/Examples/CustomKeypad/CustomKeypad.pde b/Arduino/Libraies/Keypad/Examples/CustomKeypad/CustomKeypad.pde new file mode 100755 index 0000000..408bcdb --- /dev/null +++ b/Arduino/Libraies/Keypad/Examples/CustomKeypad/CustomKeypad.pde @@ -0,0 +1,37 @@ +/* @file CustomKeypad.pde +|| @version 1.0 +|| @author Alexander Brevig +|| @contact alexanderbrevig@gmail.com +|| +|| @description +|| | Demonstrates changing the keypad size and key values. +|| # +*/ +#include + +const byte ROWS = 4; //four rows +const byte COLS = 4; //four columns +//define the cymbols on the buttons of the keypads +char hexaKeys[ROWS][COLS] = { + {'0','1','2','3'}, + {'4','5','6','7'}, + {'8','9','A','B'}, + {'C','D','E','F'} +}; +byte rowPins[ROWS] = {3, 2, 1, 0}; //connect to the row pinouts of the keypad +byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad + +//initialize an instance of class NewKeypad +Keypad cusomKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); + +void setup(){ + Serial.begin(9600); +} + +void loop(){ + char customKey = cusomKeypad.getKey(); + + if (customKey != NO_KEY){ + Serial.println(customKey); + } +} diff --git a/Arduino/Libraies/Keypad/Examples/DynamicKeypad/DynamicKeypad.pde b/Arduino/Libraies/Keypad/Examples/DynamicKeypad/DynamicKeypad.pde new file mode 100755 index 0000000..01f2ffe --- /dev/null +++ b/Arduino/Libraies/Keypad/Examples/DynamicKeypad/DynamicKeypad.pde @@ -0,0 +1,187 @@ +/* @file DynamicKeypad.pde +|| @version 1.0 +|| @author Mark Stanley +|| @contact mstanley@technologist.com +|| +|| @dificulty: Intermediate +|| +|| *** THE KEYPAD REQUIRES PULL-UP RESISTORS ON THE ROW PINS. *** +|| +|| @description +|| | This is a demonstration of keypadEvents. It's used to switch between keymaps +|| | while using only one keypad. The main concepts being demonstrated are: +|| | +|| | Using the keypad events, PRESSED, HOLD and RELEASED to simplify coding. +|| | How to use setHoldTime() and why. +|| | Making more than one thing happen with the same key. +|| | Assigning and changing keymaps on the fly. +|| | +|| | Another useful feature is also included with this demonstration although +|| | it's not really one of the concepts that I wanted to show you. If you look +|| | at the code in the PRESSED event you will see that the first section of that +|| | code is used to scroll through three different letters on each key. For +|| | example, pressing the '2' key will step through the letters 'd', 'e' and 'f'. +|| | +|| | +|| | Using the keypad events, PRESSED, HOLD and RELEASED to simplify coding +|| | Very simply, the PRESSED event occurs imediately upon detecting a pressed +|| | key and will not happen again until after a RELEASED event. When the HOLD +|| | event fires it always falls between PRESSED and RELEASED. However, it will +|| | only occur if a key has been pressed for longer than the setHoldTime() interval. +|| | +|| | How to use setHoldTime() and why +|| | Take a look at keypad.setHoldTime(500) in the code. It is used to set the +|| | time delay between a PRESSED event and the start of a HOLD event. The value +|| | 500 is in milliseconds (mS) and is equivalent to half a second. After pressing +|| | a key for 500mS the HOLD event will fire and any code contained therein will be +|| | executed. This event will stay active for as long as you hold the key except +|| | in the case of bug #1 listed above. +|| | +|| | Making more than one thing happen with the same key. +|| | If you look under the PRESSED event (case PRESSED:) you will see that the '#' +|| | is used to print a new line, Serial.println(). But take a look at the first +|| | half of the HOLD event and you will see the same key being used to switch back +|| | and forth between the letter and number keymaps that were created with alphaKeys[4][5] +|| | and numberKeys[4][5] respectively. +|| | +|| | Assigning and changing keymaps on the fly +|| | You will see that the '#' key has been designated to perform two different functions +|| | depending on how long you hold it down. If you press the '#' key for less than the +|| | setHoldTime() then it will print a new line. However, if you hold if for longer +|| | than that it will switch back and forth between numbers and letters. You can see the +|| | keymap changes in the HOLD event. +|| | +|| | +|| | In addition... +|| | You might notice a couple of things that you won't find in the Arduino language +|| | reference. The first would be #include . This is a standard library from +|| | the C programming language and though I don't normally demonstrate these types of +|| | things from outside the Arduino language reference I felt that its use here was +|| | justified by the simplicity that it brings to this sketch. +|| | That simplicity is provided by the two calls to isalpha(key) and isdigit(key). +|| | The first one is used to decide if the key that was pressed is any letter from a-z +|| | or A-Z and the second one decides if the key is any number from 0-9. The return +|| | value from these two functions is either a zero or some positive number greater +|| | than zero. This makes it very simple to test a key and see if it is a number or +|| | a letter. So when you see the following: +|| | +|| | if (isalpha(key)) // this tests to see if your key was a letter +|| | +|| | And the following may be more familiar to some but it is equivalent: +|| | +|| | if (isalpha(key) != 0) // this tests to see if your key was a letter +|| | +|| | And Finally... +|| | To better understand how the event handler affects your code you will need to remember +|| | that it gets called only when you press, hold or release a key. However, once a key +|| | is pressed or held then the event handler gets called at the full speed of the loop(). +|| | +|| | *** THE KEYPAD REQUIRES PULL-UP RESISTORS ON THE ROW PINS. *** +|| # +*/ +#include +#include + +// Define the keymaps. The blank spot (lower left) is the space character. +char alphaKeys[4][3] = { + { 'a','d','g' }, + { 'j','m','p' }, + { 's','v','y' }, + { ' ','.','#' } +}; + +char numberKeys[4][3] = { + { '1','2','3' }, + { '4','5','6' }, + { '7','8','9' }, + { ' ','0','#' } +}; + +boolean alpha = false; // Start with the numeric keypad. + +char* keypadMap = (alpha == true) ? makeKeymap(alphaKeys) : makeKeymap(numberKeys); + +// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these pins, eg. ROW0 = Arduino pin2. +byte rowPins[] = { 9, 8, 7, 6 }; + +// Connect keypad COL0, COL1 and COL2 to these pins, eg. COL0 = Arduino pin6. +byte colPins[] = { 12, 11, 10 }; + +//create a new Keypad +Keypad keypad = Keypad(keypadMap, rowPins, colPins, sizeof(rowPins), sizeof(colPins)); + +const byte ledPin = 13; // Use the LED on pin 13. + +void setup() { + Serial.begin(9600); + digitalWrite(ledPin, HIGH); // Turns the LED on. + keypad.addEventListener(keypadEvent); // Add an event listener. + keypad.setHoldTime(500); // Default is 1000mS + keypad.setDebounceTime(250); // Default is 50mS +} + +void loop() { + char key = keypad.getKey(); + + if (alpha) { // Flash the LED if we are using the letter keymap. + digitalWrite(ledPin,!digitalRead(ledPin)); + delay(100); + } +} + +// Take care of some special events. +void keypadEvent(KeypadEvent key) { + static char virtKey = NO_KEY; // Stores the last virtual key press. (Alpha keys only) + static char physKey = NO_KEY; // Stores the last physical key press. (Alpha keys only) + static char buildStr[12]; + static byte buildCount; + static byte pressCount; + + switch (keypad.getState()) + { + case PRESSED: + if (isalpha(key)) { // This is a letter key so we're using the letter keymap. + if (physKey != key) { // New key so start with the first of 3 characters. + pressCount = 0; + virtKey = key; + physKey = key; + } + else { // Pressed the same key again... + virtKey++; // so select the next character on that key. + pressCount++; // Tracks how many times we press the same key. + } + if (pressCount > 2) { // Last character reached so cycle back to start. + pressCount = 0; + virtKey = key; + } + Serial.print(virtKey); // Used for testing. + } + if (isdigit(key) || key == ' ' || key == '.') Serial.print(key); + if (key == '#') Serial.println(); + break; + + case HOLD: + if (key == '#') { // Toggle between keymaps. + if (alpha == true) { // We are currently using a keymap with letters + keypad.begin(*numberKeys); // and want to change to numbers. + alpha = false; + } + else { // Or, we are currently using a keymap with numbers + keypad.begin(*alphaKeys); // and want to change to letters. + alpha = true; + } + } + else { // Some key other than '#' was pressed. + buildStr[buildCount++] = (isalpha(key)) ? virtKey : key; + buildStr[buildCount] = '\0'; + Serial.println(); + Serial.println(buildStr); + } + break; + + case RELEASED: + if (buildCount >= sizeof(buildStr)) buildCount = 0; // Our string is full. Start fresh. + break; + + } // end switch-case +} // end keypad events diff --git a/Arduino/Libraies/Keypad/Examples/EventKeypad/EventKeypad.pde b/Arduino/Libraies/Keypad/Examples/EventKeypad/EventKeypad.pde new file mode 100755 index 0000000..db53ff0 --- /dev/null +++ b/Arduino/Libraies/Keypad/Examples/EventKeypad/EventKeypad.pde @@ -0,0 +1,72 @@ +/* @file EventSerialKeypad.pde +|| @version 1.0 +|| @author Alexander Brevig +|| @contact alexanderbrevig@gmail.com +|| +|| @description +|| | Demonstrates using the KeypadEvent. +|| # +*/ +#include + +const byte ROWS = 4; //four rows +const byte COLS = 4; //four columns +char keys[ROWS][COLS] = { + {'1','2','3','A'}, + {'4','5','6','B'}, + {'7','8','9','C'}, + {'#','0','*','D'} +}; +byte rowPins[ROWS] = {2,3,4,5}; //connect to the row pinouts of the keypad +byte colPins[COLS] = {6,7,8,9}; //connect to the column pinouts of the keypad + +Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); +byte ledPin = 13; + +boolean blink = false; + +void setup(){ + Serial.begin(9600); + pinMode(ledPin, OUTPUT); // sets the digital pin as output + digitalWrite(ledPin, HIGH); // sets the LED on + keypad.addEventListener(keypadEvent); //add an event listener for this keypad +} + +void loop(){ + char key = keypad.getKey(); + + if (key != NO_KEY) { + Serial.println(key); + } + if (blink){ + digitalWrite(ledPin,!digitalRead(ledPin)); + delay(100); + } +} + +//take care of some special events +void keypadEvent(KeypadEvent key){ + switch (keypad.getState()){ + case PRESSED: + switch (key){ + case '#': digitalWrite(ledPin,!digitalRead(ledPin)); break; + case '*': + digitalWrite(ledPin,!digitalRead(ledPin)); + break; + } + break; + case RELEASED: + switch (key){ + case '*': + digitalWrite(ledPin,!digitalRead(ledPin)); + blink = false; + break; + } + break; + case HOLD: + switch (key){ + case '*': blink = true; break; + } + break; + } +} diff --git a/Arduino/Libraies/Keypad/Examples/HelloKeypad/HelloKeypad.pde b/Arduino/Libraies/Keypad/Examples/HelloKeypad/HelloKeypad.pde new file mode 100755 index 0000000..c001965 --- /dev/null +++ b/Arduino/Libraies/Keypad/Examples/HelloKeypad/HelloKeypad.pde @@ -0,0 +1,35 @@ +/* @file HelloKeypad.pde +|| @version 1.0 +|| @author Alexander Brevig +|| @contact alexanderbrevig@gmail.com +|| +|| @description +|| | Demonstrates the simplest use of the matrix Keypad library. +|| # +*/ +#include + +const byte ROWS = 4; //four rows +const byte COLS = 3; //three columns +char keys[ROWS][COLS] = { + {'1','2','3'}, + {'4','5','6'}, + {'7','8','9'}, + {'#','0','*'} +}; +byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad +byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad + +Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); + +void setup(){ + Serial.begin(9600); +} + +void loop(){ + char key = keypad.getKey(); + + if (key != NO_KEY){ + Serial.println(key); + } +} diff --git a/Arduino/Libraies/Keypad/Keypad.cpp b/Arduino/Libraies/Keypad/Keypad.cpp new file mode 100755 index 0000000..3c7ffc6 --- /dev/null +++ b/Arduino/Libraies/Keypad/Keypad.cpp @@ -0,0 +1,174 @@ +/* +|| +|| @file Keypad.h +|| @version 1.8 +|| @author Mark Stanley, Alexander Brevig +|| @contact mstanley@technologist.com, alexanderbrevig@gmail.com +|| +|| @description +|| | This library provides a simple interface for using matrix +|| | keypads. It supports the use of multiple keypads with the +|| | same or different sets of keys. It also supports user +|| | selectable pins and definable keymaps. +|| # +|| +|| @license +|| | This library is free software; you can redistribute it and/or +|| | modify it under the terms of the GNU Lesser General Public +|| | License as published by the Free Software Foundation; version +|| | 2.1 of the License. +|| | +|| | This library is distributed in the hope that it will be useful, +|| | but WITHOUT ANY WARRANTY; without even the implied warranty of +|| | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +|| | Lesser General Public License for more details. +|| | +|| | You should have received a copy of the GNU Lesser General Public +|| | License along with this library; if not, write to the Free Software +|| | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +|| # +|| +*/ + +#include "Keypad.h" + +// <> Allows custom keymap. pin configuration and keypad size +Keypad::Keypad(char *userKeymap, byte *row, byte *col, byte rows, byte cols) +{ + rowPins = row; + columnPins = col; + + //Serial.println("Override"); + size.rows = rows; + //size.rows = 8; + size.columns = cols; + //size.columns = 16; + + //Serial.print("Constructor Col "); + //int colSize = size.columns; + //Serial.print(colSize); + //int rowSize = size.rows; + //Serial.print(" Row "); + //Serial.println(rowSize); + + begin(userKeymap); + + lastUpdate = 0; + debounceTime = 50; + holdTime = 1000; + keypadEventListener = 0; + currentKey = NO_KEY; + state = IDLE; + + initializePins(); +} + +// Let the user define a keymap - assume the same row- / columncount as defined in constructor +void Keypad::begin( char *userKeymap){ + keymap = userKeymap; +} + +// Returns the keykode of the pressed key, or NO_KEY if no key is pressed +char Keypad::getKey(){ + //Serial.print("GetKey Col "); + //int colSize = size.columns; + //Serial.print(colSize); + //int rowSize = size.rows; + //Serial.print(" Row "); + //Serial.println(rowSize); + char key = NO_KEY; // Assume that no key is pressed, this is the default return for getKey() + for (byte c=0; c= holdTime) && digitalRead(rowPins[r]) == LOW){ + transitionTo(HOLD); + } + // Button release + if (((millis()-lastUpdate) >= debounceTime) && digitalRead(rowPins[r]) == HIGH){ + transitionTo(RELEASED); + currentKey = NO_KEY; + } + } + // Button pressed event. The user pressed a button. + else if (((millis()-lastUpdate) >= debounceTime) && digitalRead(rowPins[r]) == LOW){ + digitalWrite(columnPins[c],HIGH); // De-activate the current column. + key = keymap[c+(r*size.columns)]; + lastUpdate = millis(); + goto EVALUATE_KEY; // Save resources and do not attempt to parse to keys at a time + } + } + digitalWrite(columnPins[c],HIGH); // De-activate the current column. + } + + EVALUATE_KEY: + if (key != NO_KEY && key != currentKey){ + currentKey = key; + transitionTo(PRESSED); + return currentKey; + } + else{ + return NO_KEY; + } +} + + +KeypadState Keypad::getState(){ + return state; +} + +void Keypad::setDebounceTime(unsigned int debounce){ + debounceTime = debounce; +} +void Keypad::setHoldTime(unsigned int hold){ + holdTime = hold; +} + +void Keypad::addEventListener(void (*listener)(char)){ + keypadEventListener = listener; +} + +//private +void Keypad::transitionTo(KeypadState newState){ + if (state!=newState){ + state = newState; + if (keypadEventListener!=NULL){ + keypadEventListener(currentKey); + } + } +} + +void Keypad::initializePins(){ + for (byte r=0; r + +#define makeKeymap(x) ((char*)x) + +typedef char KeypadEvent; + +typedef enum { + IDLE=0, + PRESSED, + RELEASED, + HOLD +} KeypadState; + +// : 8 refers to the variable occupying 8 bits of memory +typedef struct { + byte rows : 8; + byte columns : 8; +} KeypadSize; + +const char NO_KEY = '\0'; +#define KEY_RELEASED NO_KEY + +class Keypad { +public: + /* + Keypad(); + Keypad(byte row[], byte col[], byte rows, byte cols); + */ + Keypad(char *userKeymap, byte *row, byte *col, byte rows, byte cols); + + //void begin(); //DEPRECATED! + void begin(char *userKeymap); + char getKey(); + KeypadState getState(); + void setDebounceTime(unsigned int debounce); + void setHoldTime(unsigned int hold); + void addEventListener(void (*listener)(char)); + +private: + void transitionTo(KeypadState newState); + void initializePins(); + + char *keymap; + byte *rowPins; + byte *columnPins; + KeypadSize size; + KeypadState state; + char currentKey; + unsigned long lastUpdate; + unsigned int debounceTime; + unsigned int holdTime; + void (*keypadEventListener)(char); +}; + +#endif + +/* +|| @changelog +|| | 1.8 2009-07-08 - Alexander Brevig : No longer uses arrays +|| | 1.7 2009-06-18 - Alexander Brevig : This library is a Finite State Machine +|| | every time a state changes the keypadEventListener will trigger, if set +|| | 1.7 2009-06-18 - Alexander Brevig : Added setDebounceTime +|| | setHoldTime specifies the amount of microseconds before a HOLD state triggers +|| | 1.7 2009-06-18 - Alexander Brevig : Added transitionTo +|| | 1.6 2009-06-15 - Alexander Brevig : Added getState() and state variable +|| | 1.5 2009-05-19 - Alexander Brevig : Added setHoldTime() +|| | 1.4 2009-05-15 - Alexander Brevig : Added addEventListener +|| | 1.3 2009-05-12 - Alexander Brevig : Added lastUdate, in order to do simple debouncing +|| | 1.2 2009-05-09 - Alexander Brevig : Changed getKey() +|| | 1.1 2009-04-28 - Alexander Brevig : Modified API, and made variables private +|| | 1.0 2007-XX-XX - Mark Stanley : Initial Release +|| # +*/ diff --git a/Arduino/Libraies/Keypad/README b/Arduino/Libraies/Keypad/README new file mode 100644 index 0000000..504a22a --- /dev/null +++ b/Arduino/Libraies/Keypad/README @@ -0,0 +1 @@ +Modified the bit fields in the KeypadSize structure to allow for a big enough matrix diff --git a/Arduino/Libraies/Keypad/keywords.txt b/Arduino/Libraies/Keypad/keywords.txt new file mode 100755 index 0000000..2ae39b8 --- /dev/null +++ b/Arduino/Libraies/Keypad/keywords.txt @@ -0,0 +1,17 @@ +Keypad KEYWORD1 +KeypadEvent KEYWORD1 + +begin KEYWORD2 +getKey KEYWORD2 +getState KEYWORD2 +setHoldTime KEYWORD2 +addEventListener KEYWORD2 + +# this is a macro that converts 2d arrays to pointers +makeKeymap KEYWORD2 + +# this is a way to standardize the client code, help enhance the use of the name keypadEvent +keypadEvent KEYWORD2 + +NO_KEY LITERAL1 +KEY_RELEASED LITERAL1 -- cgit v1.2.3