/*
replicating ES32A08 board
*/
/* -----74HC595 shift register
ESP32 PIN --- 74HC595 PIN
*/
#define clockPin 27 // SRCLK/SHCP 11
#define latchPin 14 // RCLK / STCP 12
#define dataPin 13 // SER / DS 14
#define LED 15
#define KEY1 18
#define KEY2 19
#define KEY3 21
#define KEY4 23
#define LOAD_165 16
#define CLK_165 17
#define DATA165 5
#define GPIO_INPUT_PIN_SEL ((1ULL<<KEY1) | (1ULL<<KEY2) | (1ULL<<KEY3) | (1ULL<<KEY4))
uint8_t BitsSelection[4] = {0xFE, 0xFD, 0xFB, 0xF7};
uint8_t DisplayCode[4] = {0};
uint8_t Dot = 0x80;
uint8_t BitsSele = 0;
uint8_t location,value;
uint8_t mykey=16;
/*
byte is LSBFIRST, non-inverted
char "2", hex "5b", binary "01011011"
char "A", hex "77", binary "01110111"
*/
// Common cathode 7 segment display
uint8_t SEG8Code[] =
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x58,0x5e,0x79,0x71,0x76,0x74,0x38,0x54,0x37,0x5c,0x73,0x50,0x78,0x3e,0x40,0x00};
// Define the encoding of characters 0-F for the common-anode 7-Segment Display
// uint8_t SEG8Code[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e};
uint16_t Time_Cnt = 0;
uint8_t Out_Value = 0;
uint8_t Out_Cnt = 0;
uint8_t Value = 0;
int Get_KEY_Value()
{
uint8_t Value1,Value2 = 0;
if(digitalRead(KEY1) == 0){Value1 |= 0x01;}
if(digitalRead(KEY2) == 0){Value1 |= 0x02;}
if(digitalRead(KEY3) == 0){Value1 |= 0x04;}
if(digitalRead(KEY4) == 0){Value1 |= 0x08;}
usleep(20);
if(digitalRead(KEY1) == 0){Value2 |= 0x01;}
if(digitalRead(KEY2) == 0){Value2 |= 0x02;}
if(digitalRead(KEY3) == 0){Value2 |= 0x04;}
if(digitalRead(KEY4) == 0){Value2 |= 0x08;}
return(Value1);
}
uint8_t Read_74HC165(void)
{
uint8_t i;
uint8_t Temp = 0;
digitalWrite(LOAD_165, LOW);
digitalRead(LOAD_165);
for(i=0;i<8;i++)
{
Temp <<= 1;
digitalWrite(CLK_165, LOW);
if(digitalRead(DATA165) == 0){Temp |= 0x01;}
digitalWrite(CLK_165, HIGH);
}
return Temp;
}
void Get_Input_Value(void)
{
uint8_t Value1 = 0;
uint8_t Value2 = 0;
Value1 = Read_74HC165();
vTaskDelay(20 / portTICK_PERIOD_MS);
Value2 = Read_74HC165();
if(Value1 == Value2)
{
if(Value1 & 0x01){Value = 1;}
if(Value1 & 0x02){Value = 2;}
if(Value1 & 0x04){Value = 3;}
if(Value1 & 0x08){Value = 4;}
if(Value1 & 0x10){Value = 5;}
if(Value1 & 0x20){Value = 6;}
if(Value1 & 0x40){Value = 7;}
if(Value1 & 0x80){Value = 8;}
//Serial.print((Value1));Serial.print(" : ");Serial.println(Value2);
if(Value1)
{
digitalWrite(LED, LOW);
}
else
{
digitalWrite(LED, HIGH);
}
}
}
void Send_Bytes(uint8_t dat) //Send 1 byte
{
uint8_t i;
for(i=8;i>=1;i--)
{
if(dat & Dot){
digitalWrite(dataPin, 1);
} else {
digitalWrite(dataPin, 0);
} //Sends data bit by bit from high to low.
dat <<= 1;
digitalWrite(clockPin, 0);
digitalWrite(clockPin, 1);
}
}
void Send_74HC595(uint8_t Num, uint8_t Seg, uint8_t out)
{
Send_Bytes(out);
Send_Bytes(Seg);
Send_Bytes(Num);
digitalWrite(latchPin, 0);
digitalWrite(latchPin, 1);
}
void setdigit(uint8_t digit, uint8_t Value)
{
printf("displaycode: %d\tvalue: %d\n",DisplayCode[digit],digit);
DisplayCode[digit] = SEG8Code[Value]; // | Dot;
Send_74HC595(DisplayCode[digit], digit, Out_Value);
//Send_74HC595(DisplayCode[1], BitsSelection[1], Out_Value);
//Send_74HC595(SEG8Code[Value], BitsSelection[digit], Out_Value);
Time_Cnt++;
BitsSele++;
if(BitsSele == 3){BitsSele = 0;}
if(Time_Cnt >= 1000)
{
Time_Cnt = 0;
Out_Value = 0x01 << Out_Cnt;
Out_Cnt++;
if(Out_Cnt >= 8){Out_Cnt = 0;}
}
return;
}
void blank() {
Send_74HC595(0,0,0);
}
void cleardigit(uint8_t digit) {
//digitalWrite(digit, 0);
setdigit(digit, 28);
}
void HexToBin(char hex_number, char* bit_number) {
int max = 128;
for(int i = 7 ; i >-1 ; i--){
bit_number [i] = (hex_number & max ) ? 1 : 0;
max >>=1;
}
}
void setup() {
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(KEY1, INPUT_PULLUP);
pinMode(KEY2, INPUT_PULLUP);
pinMode(KEY3, INPUT_PULLUP);
pinMode(KEY4, INPUT_PULLUP);
pinMode(LOAD_165, OUTPUT);
pinMode(CLK_165, OUTPUT);
pinMode(DATA165, INPUT);
Serial.begin(115200);
}
void loop() {
//for(value=0; value <= 28; value+=1) {
/*
Serial.print("Location: ");
Serial.print(location);
Serial.print(" Value: ");
Serial.print(value);
Serial.print(" Decimal: ");
Serial.print(SEG8Code[value]);
Serial.print(" Binary: ");
setdigit(location,value);
*/
//}
mykey=Get_KEY_Value();
//printf("MyKey: %X\n",mykey);
switch (mykey)
{
case 1:
//cleardigit(1);
setdigit(0,1);
break;
case 2:
//cleardigit(2);
setdigit(1,2);
break;
case 4:
//cleardigit(3);
setdigit(2,4);
break;
case 8:
//cleardigit(4);
setdigit(3,8);
break;
}
blank();
}