123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- int rows[] = {5,4,0,2};
- int cols[] = {14,12,13,15};
- #define FOUR_PIN 1
- void setup() {
- delay(1000);
- Serial.begin(115200);
-
- rowsSet(0);
-
- pinMode(cols[0],INPUT_PULLUP);
- pinMode(cols[1],INPUT_PULLUP);
- pinMode(cols[2],INPUT_PULLUP);
- attachInterrupt(digitalPinToInterrupt(cols[0]), kbd_irq, FALLING);
- attachInterrupt(digitalPinToInterrupt(cols[1]), kbd_irq, FALLING);
- attachInterrupt(digitalPinToInterrupt(cols[2]), kbd_irq, FALLING);
- Serial.println();
- }
- void rowsSet(int rowPattern) {
- for (int r=0; r<4; r++) {
- if (rowPattern & 1)
- pinMode(rows[r],INPUT_PULLUP);
- else {
- pinMode(rows[r],OUTPUT);
- digitalWrite(rows[r],0);
- }
- rowPattern >>= 1;
- }
- }
- const int KEY_NONE = 0;
- const int KEY_DETECTED = 1;
- const int KEY_PROCESSED = 2;
- static int key_state;
- ICACHE_RAM_ATTR void kbd_irq(void) {
-
- if (key_state == KEY_NONE)
- key_state = KEY_DETECTED;
- }
- static char lut4[] = "123A456B789C*0#D";
- static char lut[] = "123456789*0#";
- int char_count;
- void calculator(char key) {
- if (key) {
- static int acc = 0, sum=0;
- if (key>='0' && key<='9') {
- acc = acc*10+(key&0x0f);
- }
- else if (key=='*') {
- sum += acc;
- acc = 0;
- }
- else if (key=='#') {
- sum += acc;
- Serial.println(String("total: ")+String(sum));
- sum = acc = 0;
- }
- }
- }
- #ifdef SEVEN_PIN
- int readColumns() {
-
- return 6 - digitalRead(cols[0]) - 2*digitalRead(cols[1]) - 3*digitalRead(cols[2]);
- }
- const int NO_KEYS = 0;
- int rowPattern [] = {~1, ~2, ~4, ~8};
- void loop() {
-
- int row, col;
- switch(key_state) {
- case KEY_NONE:
- break;
- case KEY_DETECTED:
-
- for (row=0; row<4; row++) {
- rowsSet(rowPattern[row]);
- delayMicroseconds(10);
- col = readColumns();
- if (col)
- break;
- }
- rowsSet(0);
- key_state = KEY_PROCESSED;
-
- if (col) {
- Serial.print(String(lut[3*row+col-1]));
- if (++char_count >= 16)
- char_count=0, Serial.println();
- }
- break;
- case KEY_PROCESSED:
-
- col = readColumns();
- if (col == NO_KEYS)
- key_state = KEY_NONE;
- break;
- }
- }
- #endif
- #ifdef FIVE_PIN
- int readColumns() {
- return digitalRead(cols[0]) + 2*digitalRead(cols[1]) + 4*digitalRead(cols[2]);
- }
- const int NO_KEYS = 07;
- int rowPattern [] = {0b11, 0b10, 0b01, 0b00};
- int fiveKeyLUT[] = {06666, 05555, 03333,
- 07676, 07575, 07373,
- 07766, 07755, 07733,
- 07666, 07555, 07333};
- void loop() {
-
- int row, col, d;
- char key = 0;
- switch(key_state) {
- case KEY_NONE:
- break;
- case KEY_DETECTED:
-
-
- d = 0;
- for (row=0; row<4; row++) {
-
- rowsSet(rowPattern[row]);
- delayMicroseconds(10);
- d = 8*d + readColumns();
-
- }
- rowsSet(0);
- key_state = KEY_PROCESSED;
-
-
- for (int i=0; i<sizeof(fiveKeyLUT)/sizeof(fiveKeyLUT[0]); i++)
- if (d==fiveKeyLUT[i]) {
- key = lut[i];
- break;
- }
-
- if (key) {
- Serial.print(key);
- if (++char_count >= 16)
- char_count=0, Serial.println();
- }
- break;
- case KEY_PROCESSED:
-
- col = readColumns();
- if (col==NO_KEYS)
- key_state = KEY_NONE;
- break;
- }
-
- calculator(key);
- }
- #endif
- #ifdef FOUR_PIN
- const int fourKeyLUT[] = { 0xaa, 0x55, 0x00,
- 0xee, 0xdd, 0xcc,
- 0xfa, 0xf5, 0xf0,
- 0xea, 0xd5, 0xc0
- };
-
- int readColumns() {
- return digitalRead(cols[0]) + 2*digitalRead(cols[1]);
- }
- const int NO_KEYS = 0x3;
- int rowPattern [] = {0b11, 0b10, 0b01, 0b00};
- void loop() {
-
- int row, col, d;
- char key=0;
- switch(key_state) {
- case KEY_NONE:
- break;
- case KEY_DETECTED:
-
- delay(5);
-
- d = 0;
- for (row=0; row<4; row++) {
-
- rowsSet(rowPattern[row]);
- delayMicroseconds(10);
- d = 4*d + readColumns();
-
- }
- rowsSet(0);
- key_state = KEY_PROCESSED;
-
-
- key = 0;
- for (int i=0; i<sizeof(fourKeyLUT)/sizeof(fourKeyLUT[0]); i++)
- if (d==fourKeyLUT[i]) {
- key = lut[i];
- break;
- }
-
- if (key) {
- Serial.print(key);
- if (++char_count >= 16)
- char_count=0, Serial.println();
- }
- break;
- case KEY_PROCESSED:
-
- col = readColumns();
- if (col==NO_KEYS)
- key_state = KEY_NONE;
- break;
- }
- calculator(key);
- }
- #endif
|