-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM5_Came_Scanner.ino
125 lines (111 loc) · 2.82 KB
/
M5_Came_Scanner.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
91
92
93
94
95
96
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
123
124
125
#include <M5Stack.h>
#include "Free_Fonts.h"
// Below lines include and use RCSwitch library with 8th protocol for Came gates
// You need to add below protocol definition into RCSwitch.h as 8th protocol by yourself:
// { 320, { 64, 1 }, { 1, 2 }, { 2, 1 }, true }
#define CAME_PROTO 8
#include <RCSwitch.h>
#define ESP32
RCSwitch Radio = RCSwitch();
unsigned long Data[12] = { 0xAAAAAA, 0xBBBBBB, 0xCCCCCC, 0xDDDDDD, 0, 0, 0, 0, 0, 0, 0, 0 };
int CodesCount = 4;
unsigned long LastUpd;
int res;
//===============================================================================
// Check if code is presend in Data array - if yes simply return it's index
// If code was not present in array then beep, add it to array and return
// index of newly added element
int StoreCode(unsigned long code)
{
int i, last;
last = -1;
for (i = 0; i < 12; i++)
{
if (Data[i] == code)
return i;
if (Data[i] == 0)
{
last = i;
break;
}
}
if (last >= 0)
{
Data[last] = code;
M5.Speaker.tone(900, 200);
M5.Lcd.clear(BLUE);
}
return last;
}
//===============================================================================
// Put all 12 elements of Data array on a screen in 2 columns of HEX values
// If 0<=highlight<12 - mark this code with Red color
void ShowCodes(int highlight)
{
int i;
M5.Lcd.setFreeFont(FM18);
for (i=0; i<12; i++)
{
if (i<6)
M5.Lcd.setCursor(0, 30*(i+1));
else
M5.Lcd.setCursor(160, 30*(i-5));
if (i == highlight)
M5.Lcd.setTextColor(TFT_RED);
else
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.print(Data[i], HEX);
}
}
//===============================================================================
// the setup routine runs once when M5Stack starts up
void setup()
{
M5.begin();
M5.Lcd.clear(BLUE);
Radio.enableReceive(2);
ShowCodes(-1);
res = -1;
LastUpd = millis();
}
//===============================================================================
// the loop routine runs over and over again forever
void loop()
{
int i;
unsigned long Code;
if (Radio.available())
{
if (Radio.getReceivedProtocol() == CAME_PROTO)
{
Code = Radio.getReceivedValue();
res = StoreCode(Code);
ShowCodes(res);
LastUpd = millis();
}
Radio.resetAvailable();
}
if ((res >= 0) && ((millis()-LastUpd) > 3000))
{
ShowCodes(-1);
res = -1;
}
// DEBUG
M5.update();
if (M5.BtnA.wasReleased())
{
File file = SD.open("/Codes.txt", FILE_WRITE);
if (file)
{
for (i=0; i<12; i++)
file.println(Data[i], HEX);
file.close();
}
}
if (M5.BtnB.wasReleased())
{
}
if (M5.BtnC.wasReleased())
{
}
}