/ i2ckeyboard / atmegai2ckeyboardV2 / atmegai2ckeyboardV2.ino
atmegai2ckeyboardV2.ino
  1  
  2  #include <Wire.h>
  3  
  4  static byte msg[7];
  5  
  6  // VERI OLD static byte rows[] = {11, 10, 9, 12, 13, 8, 7, 6, 5, 4}; // VERI OLD
  7  // ILI 9341 static byte rows[] = {4, 5, 6, 7, 8, 13, 12, 9, 10, 11}; // Teensy4 ILI 9341 + keyboard
  8  #define REV_DIODE 1
  9  
 10  #ifdef REV_DIODE
 11  // rows are inputs
 12  static byte rows[] = {A3, A2, A1, A0, 3}; //{8,9,10,11};
 13  const int rowCount = sizeof(rows) / sizeof(rows[0]);
 14  // cols are outputs
 15  static byte cols[] = {9, 10, 8, 7, 6, 5, 4, 2, 1, 0 };  // Teensy4 ST7789 + keyboard
 16  const int colCount = sizeof(cols) / sizeof(cols[0]);
 17  #else
 18  // rows are inputs
 19  static byte rows[] = {9, 10, 8, 7, 6, 5, 4, 2, 1, 0 };  // Teensy4 ST7789 + keyboard
 20  const int rowCount = sizeof(rows) / sizeof(rows[0]);
 21  // cols are outputs
 22  static byte cols[] = {A3, A2, A1, A0, 3}; //{8,9,10,11};
 23  const int colCount = sizeof(cols) / sizeof(cols[0]);
 24  #endif 
 25  
 26  #ifdef REV_DIODE
 27  static byte keys[rowCount][colCount];
 28  #else 
 29  static byte keys[colCount][rowCount];
 30  #endif 
 31  
 32  
 33  void setup() {
 34    //Serial.begin(115200);
 35  
 36    Wire.begin(8);                // join i2c bus with address #8
 37    Wire.onRequest(requestEvent); // register event
 38  
 39    // input
 40    for (int x = 0; x < rowCount; x++) {
 41      pinMode(rows[x], INPUT_PULLUP);
 42    }
 43  
 44    // output
 45    for (int x = 0; x < colCount; x++) {
 46      pinMode(cols[x], OUTPUT);
 47      digitalWrite(cols[x], HIGH);
 48    }
 49  }
 50  
 51  void readMatrix() {
 52    // iterate the columns
 53    for (int colIndex = 0; colIndex < colCount; colIndex++) {
 54      // col: set to output to low
 55      byte curCol = cols[colIndex];
 56      //pinMode(curCol, OUTPUT);
 57  
 58  #ifdef REV_DIODE
 59      digitalWrite(curCol, LOW);
 60  #endif
 61      // row: interate through the rows
 62      for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
 63        byte rowCol = rows[rowIndex];            
 64  #ifdef REV_DIODE
 65        keys[rowIndex][colIndex] = digitalRead(rowCol);
 66  #else
 67        digitalWrite(curCol, LOW);
 68        keys[colIndex][rowIndex] = digitalRead(rowCol);
 69        digitalWrite(curCol, HIGH);
 70  #endif    
 71      }
 72  #ifdef REV_DIODE    
 73      digitalWrite(curCol, HIGH);
 74  #endif;
 75    }
 76    byte * pt = &msg[0];
 77    byte v = 0;
 78    v |= (keys[0][0] ? 0x80 : 0);
 79    v |= (keys[1][0] ? 0x40 : 0);
 80    v |= (keys[2][0] ? 0x20 : 0);
 81    v |= (keys[3][0] ? 0x10 : 0);
 82    v |= (keys[0][1] ? 0x08 : 0);
 83    v |= (keys[1][1] ? 0x04 : 0);
 84    v |= (keys[2][1] ? 0x02 : 0);
 85    v |= (keys[3][1] ? 0x01 : 0);
 86    *pt++ = v;
 87    v = 0;
 88    v |= (keys[0][2] ? 0x80 : 0);
 89    v |= (keys[1][2] ? 0x40 : 0);
 90    v |= (keys[2][2] ? 0x20 : 0);
 91    v |= (keys[3][2] ? 0x10 : 0);
 92    v |= (keys[0][3] ? 0x08 : 0);
 93    v |= (keys[1][3] ? 0x04 : 0);
 94    v |= (keys[2][3] ? 0x02 : 0);
 95    v |= (keys[3][3] ? 0x01 : 0);
 96    *pt++ = v;
 97    v = 0;
 98    v |= (keys[0][4] ? 0x80 : 0);
 99    v |= (keys[1][4] ? 0x40 : 0);
100    v |= (keys[2][4] ? 0x20 : 0);
101    v |= (keys[3][4] ? 0x10 : 0);
102    v |= (keys[0][5] ? 0x08 : 0);
103    v |= (keys[1][5] ? 0x04 : 0);
104    v |= (keys[2][5] ? 0x02 : 0);
105    v |= (keys[3][5] ? 0x01 : 0);
106    *pt++ = v;
107    v = 0;
108    v |= (keys[0][6] ? 0x80 : 0);
109    v |= (keys[1][6] ? 0x40 : 0);
110    v |= (keys[2][6] ? 0x20 : 0);
111    v |= (keys[3][6] ? 0x10 : 0);
112    v |= (keys[0][7] ? 0x08 : 0);
113    v |= (keys[1][7] ? 0x04 : 0);
114    v |= (keys[2][7] ? 0x02 : 0);
115    v |= (keys[3][7] ? 0x01 : 0);
116    *pt++ = v;
117    v = 0;
118    v |= (keys[0][8] ? 0x80 : 0);
119    v |= (keys[1][8] ? 0x40 : 0);
120    v |= (keys[2][8] ? 0x20 : 0);
121    v |= (keys[3][8] ? 0x10 : 0);
122    v |= (keys[0][9] ? 0x08 : 0);
123    v |= (keys[1][9] ? 0x04 : 0);
124    v |= (keys[2][9] ? 0x02 : 0);
125    v |= (keys[3][9] ? 0x01 : 0);
126    *pt++ = v;
127    v = 0;
128    v |= (keys[4][0] ? 0x80 : 0);
129    v |= (keys[4][1] ? 0x40 : 0);
130    v |= (keys[4][2] ? 0x20 : 0);
131    v |= (keys[4][3] ? 0x10 : 0);
132    v |= (keys[4][4] ? 0x08 : 0);
133    v |= (keys[4][5] ? 0x04 : 0);
134    v |= (keys[4][6] ? 0x02 : 0);
135    v |= (keys[4][7] ? 0x01 : 0);
136    *pt++ = v;
137    v = 0x3F;
138    v |= (keys[4][8] ? 0x80 : 0);
139    v |= (keys[4][9] ? 0x40 : 0);
140    *pt++ = v;
141  }
142  
143  void printMatrix() {
144    
145      for (int rowIndex=0; rowIndex < rowCount; rowIndex++) {
146          if (rowIndex < 10) Serial.print(F("0"));
147          Serial.print(rowIndex); Serial.print(F(": "));
148          for (int colIndex=0; colIndex < colCount; colIndex++) {
149  #ifdef REV_DIODE
150              Serial.print(keys[rowIndex][colIndex]);
151  #else
152              Serial.print(keys[colIndex][rowIndex]);
153  #endif    
154              if (colIndex < colCount) Serial.print(F(", "));
155          }
156          Serial.println("");
157      }
158      Serial.println("");
159      Serial.println("");
160      Serial.println("");
161    
162    /*
163        Serial.println(msg[0], BIN);
164        Serial.println(msg[1], BIN);
165        Serial.println(msg[2], BIN);
166        Serial.println(msg[3], BIN);
167        Serial.println(msg[4], BIN);
168        Serial.println("");
169        Serial.println("");
170        Serial.println("");
171    */
172  }
173  
174  void loop() {
175    readMatrix();
176    //printMatrix();
177    delay(2);
178  }
179  
180  
181  // function that executes whenever data is requested by master
182  // this function is registered as an event, see setup()
183  void requestEvent() {
184    Wire.write(msg, 7);
185    //Wire.write("hello "); // respond with message of 6 bytes
186  }
187