#include "matrixKeyboard.h"
uint8_t col_Key[] = { 2, 3, 4 }; //Массив столбцов клавиатуры
uint8_t row_Key[] = { 5, 6, 7, 8, 9, 10, 11 }; //Массив строк клавиатуры
#define Adress_RC 0xA0
void setup() {
Serial.begin(9600);
for (byte i = 0; i <= sizeof(col_Key) - 1; i++) pinMode(col_Key[i], OUTPUT);
for (byte i = 0; i <= sizeof(row_Key) - 1; i++) pinMode(row_Key[i], INPUT_PULLUP);
for (byte i = 0; i <= sizeof(col_Key) - 1; i++) digitalWrite(col_Key[i], 1);
pinMode(13, OUTPUT);
bitSet(PORTB, 5);
}
void loop() {
selStage(scanKey());
}
byte scanKey () {
byte rowTempKey = 0;
byte colTempKey = 0;
for (byte i = 0; i <= sizeof(col_Key) - 1; i++) {
pinMode(col_Key[i], OUTPUT);
digitalWrite(col_Key[i], 0);
for (byte j = 0; j <= sizeof(row_Key) - 1; j++) {
if (digitalRead(row_Key[j]) == 0) {
colTempKey = i + 1;
rowTempKey = j + 1;
}
};
digitalWrite(col_Key[i], 1);
pinMode(col_Key[i], INPUT_PULLUP);
};
return ((colTempKey << 4) | rowTempKey);
}
void selStage (byte pressKey) {
static uint32_t Start_1;
static uint32_t Start_2;
static byte oldPressKey;
static byte Stage = 0;
if ((Stage == 0) && (pressKey != 0)) {
Stage = 1;
Start_1 = millis();
oldPressKey = pressKey;
transmitData(pressKey);
}
if (((millis() - Start_1) >= 110) && (pressKey == 0)) {
Stage = 0;
}
if ((Stage == 1) && ((millis() - Start_1) >= 110) && (pressKey == oldPressKey)) {
Start_2 = millis();
Stage = 2;
btnHold();
}
if ((Stage == 2) && ((millis() - Start_2) >= 110)) {
Start_2 = millis();
btnHold();
}
}
void transmitData(byte pressKey) {
switch (pressKey) {
case 0x11: cmdTrans(Adress_RC, 0xA2);
break;
case 0x21: cmdTrans(Adress_RC, 0x62);
break;
case 0x31: cmdTrans(Adress_RC, 0xE2);
break;
case 0x12: cmdTrans(Adress_RC, 0x22);
break;
case 0x22: cmdTrans(Adress_RC, 0x2);
break;
case 0x32: cmdTrans(Adress_RC, 0xC2);
break;
case 0x13: cmdTrans(Adress_RC, 0xE0);
break;
case 0x23: cmdTrans(Adress_RC, 0xA8);
break;
case 0x33: cmdTrans(Adress_RC, 0x90);
break;
case 0x14: cmdTrans(Adress_RC, 0x68);
break;
case 0x24: cmdTrans(Adress_RC, 0x98);
break;
case 0x34: cmdTrans(Adress_RC, 0xB0);
break;
case 0x25: cmdTrans(Adress_RC, 0x18);
break;
case 0x16: cmdTrans(Adress_RC, 0x10);
break;
case 0x26: cmdTrans(Adress_RC, 0x38);
break;
case 0x36: cmdTrans(Adress_RC, 0x5A);
break;
case 0x27: cmdTrans(Adress_RC, 0x4A);
break;
}
}
void cmdTrans (byte Adress, byte IR_Data) {
IR_Trans (9000);
delayMicroseconds(4500);
byteTrans(Adress);
byteTrans(~Adress);
byteTrans(IR_Data);
byteTrans(~IR_Data);
IR_Trans(560);
}
void byteTrans (byte byteT) {
for (uint8_t i = 0; i <= 7; i++) {
if (bitRead(byteT, i) == 1) {
IR_Trans(560);
delayMicroseconds(1690);
}
else {
IR_Trans(560);
delayMicroseconds(560);
}
}
}
void btnHold() {
IR_Trans (9000);
delayMicroseconds(2250);
IR_Trans (560);
}
void IR_Trans (uint16_t timeTrans)
{
bitClear(PORTB, 5);
for (timeTrans; timeTrans > 27; timeTrans = timeTrans - 28 ) {
delayMicroseconds(28);
/*bitSet(PORTB,5);
delayMicroseconds(14);
bitClear(PORTB,5);
delayMicroseconds(14);*/
}
bitSet(PORTB, 5);
}