158
|
1 /*
|
|
2 D0 <---------
|
|
3 D1 lcd rw
|
|
4 D2 <---------
|
|
5 D3 ir out -----
|
|
6 D4 lcd 4
|
|
7 D5 lcd 5
|
|
8 D6 lcd 6
|
|
9 D7 lcd 7
|
|
10 D8 lcd rs
|
|
11 D9 lcd en
|
|
12 D10 lcd backlight
|
|
13 D11 tbd <------------
|
|
14 D12 motion in <--------
|
|
15 D13 debug led <--------
|
|
16
|
|
17 */
|
153
|
18 #include <Arduino.h>
|
|
19 #include <LiquidCrystal.h>
|
157
|
20 #include "DFR_Key.h"
|
|
21 #include "IRremote.h"
|
153
|
22
|
157
|
23 // see http://www.dfrobot.com/image/data/DFR0009/LCDKeypad%20Shield%20V1.0%20SCH.pdf
|
153
|
24 #define I2C_ADDR 0x27 // I2C address of PCF8574A
|
157
|
25 #define BACKLIGHT_PIN 10
|
153
|
26 #define En_pin 9
|
|
27 #define Rw_pin 1
|
|
28 #define Rs_pin 8
|
|
29 #define D4_pin 4
|
|
30 #define D5_pin 5
|
|
31 #define D6_pin 6
|
|
32 #define D7_pin 7
|
|
33
|
157
|
34 LiquidCrystal lcd(Rs_pin, Rw_pin, En_pin, D4_pin, D5_pin, D6_pin, D7_pin, BACKLIGHT_PIN, POSITIVE);
|
|
35 DFR_Key keypad;
|
153
|
36
|
|
37 #define debugLed 13
|
|
38
|
157
|
39 byte backlightStandard = 0;
|
|
40 byte backlightCurrent = 0;
|
|
41 byte backlightKeypressBoost = 30;
|
|
42
|
|
43 int lastKey = -1;
|
|
44 int lastUnsentKeydown = -1;
|
|
45 uint16_t steps = 0;
|
|
46 uint16_t stepDelayPerBrightnessFade = 2048;
|
|
47
|
158
|
48 // uses pin 3
|
157
|
49 IRsend irsend;
|
|
50
|
|
51 void sendnec(byte addr, byte cmd) {
|
|
52 uint32_t w = 0x00ff0000;
|
|
53 w |= (cmd << 8) & 0xff00;
|
|
54 w |= (~cmd) & 0xff;
|
|
55 irsend.sendNEC(w, 32);
|
|
56 delay(100);
|
|
57 }
|
153
|
58
|
|
59 void setup(void) {
|
|
60 pinMode(debugLed, OUTPUT);
|
|
61 Serial.begin(115200);
|
157
|
62 keypad.setRate(10);
|
|
63 lcd.begin(16,2);
|
|
64
|
|
65
|
|
66 for (int i = 0; i < 1; i++) {
|
|
67 digitalWrite(debugLed,1);
|
|
68 /*
|
|
69 44 key remote. command chart: http://blog.allgaiershops.com/2012/05/
|
|
70 Up Down Play Pwr
|
|
71 0x3A 0xBA 0x82 0x02
|
|
72
|
|
73 Red Grn Blu Wht
|
|
74 1A 9A A2 22
|
|
75
|
|
76 2A AA 92 12
|
|
77
|
|
78 0A 8A B2 32
|
|
79
|
|
80 38 B8 78 F8
|
|
81
|
|
82 18 98 58 D8
|
|
83
|
|
84 RUp GUp BUp Quick
|
|
85 28 A8 68 E8
|
|
86
|
|
87 RDn GDn BDn Slow
|
|
88 08 88 48 C8
|
|
89
|
|
90 DIY1 DIY2 DIY3 AUTO
|
|
91 30 B0 70 F0
|
|
92
|
|
93 DIY4 DIY5 DIY6 Flash
|
|
94 10 90 50 D0
|
|
95
|
|
96 JMP3 JMP7 Fade Fade7
|
|
97 20 A0 60 E0
|
|
98
|
|
99 irsend.sendNEC(0xff0002fd, 32);
|
|
100 irsend.sendNEC(0x00ff02fd, 32);
|
|
101 irsend.sendNEC(0x00ff40bf, 32);
|
|
102 irsend.sendNEC(0xff0040bf, 32);
|
|
103 */
|
|
104 delay(100);
|
|
105
|
|
106 // LED618 remote command byte (addr is ff)
|
|
107 // see https://github.com/alistairallan/RgbIrLed/blob/master/RgbIrLed.cpp#L44
|
|
108 #define ON 0xE0
|
|
109 #define OFF 0x60
|
|
110 #define BRIGHTNESS_UP 0xA0
|
|
111 #define BRIGHTNESS_DOWN 0x20
|
|
112 #define FLASH 0xF0
|
|
113 #define STROBE 0xE8
|
|
114 #define FADE 0xD8
|
|
115 #define SMOOTH 0xC8
|
|
116
|
|
117 #define RED 0x90
|
|
118 #define GREEN 0x10
|
|
119 #define BLUE 0x50
|
|
120 #define WHITE 0xD0
|
|
121
|
|
122 #define ORANGE 0xB0
|
|
123 #define YELLOW_DARK 0xA8
|
|
124 #define YELLOW_MEDIUM 0x98
|
|
125 #define YELLOW_LIGHT 0x88
|
|
126
|
|
127 #define GREEN_LIGHT 0x30
|
|
128 #define GREEN_BLUE1 0x28
|
|
129 #define GREEN_BLUE2 0x18
|
|
130 #define GREEN_BLUE3 0x08
|
|
131
|
|
132 #define BLUE_RED 0x70
|
|
133 #define PURPLE_DARK 0x68
|
|
134 #define PURPLE_LIGHT 0x58
|
|
135 #define PINK 0x48
|
|
136 sendnec(0xff, ON);
|
|
137
|
|
138 digitalWrite(debugLed,0);
|
|
139 delay(100);
|
153
|
140 }
|
|
141 }
|
|
142
|
157
|
143
|
153
|
144 void loop() {
|
157
|
145 while (Serial.available() <= 2) {
|
|
146 int localKey = keypad.getKey();
|
|
147 if (localKey != SAMPLE_WAIT) {
|
|
148 if (localKey != lastKey) {
|
|
149 if (lastKey == 0) {
|
|
150 backlightCurrent = min(255, backlightStandard +
|
|
151 backlightKeypressBoost);
|
|
152 lastUnsentKeydown = localKey;
|
|
153 }
|
|
154 lastKey = localKey;
|
|
155 }
|
|
156 }
|
|
157
|
|
158 if (backlightCurrent > backlightStandard) {
|
|
159 steps++;
|
|
160 if (steps % stepDelayPerBrightnessFade == 0) {
|
|
161 backlightCurrent--;
|
|
162 }
|
|
163 } else if (backlightCurrent < backlightStandard) {
|
|
164 backlightCurrent = backlightStandard;
|
|
165 }
|
|
166 lcd.setBacklight(backlightCurrent);
|
|
167 }
|
|
168 byte i = Serial.read();
|
|
169 if (i != 0x60) {
|
|
170 return;
|
|
171 }
|
|
172 i = Serial.read(); // command
|
|
173 if (i == 0) { // get status
|
|
174 Serial.print('{');
|
|
175 if (lastUnsentKeydown != -1) {
|
|
176 Serial.print("\"keyDown\":");
|
|
177 Serial.print(lastUnsentKeydown);
|
|
178 Serial.print(",");
|
|
179 lastUnsentKeydown = -1;
|
|
180 }
|
|
181 Serial.print("\"slider1\":"); Serial.print(analogRead(1));
|
|
182 Serial.print(",\"slider2\":"); Serial.print(analogRead(2));
|
|
183 Serial.print(",\"slider3\":"); Serial.print(analogRead(3));
|
|
184 Serial.print(",\"slider4\":"); Serial.print(analogRead(4));
|
|
185 Serial.print("}\n");
|
|
186 } else if (i == 1) { // write text
|
|
187 while (Serial.available() < 3) NULL;
|
|
188 byte row = Serial.read();
|
|
189 byte col = Serial.read();
|
|
190 byte nchars = Serial.read();
|
|
191 char buf[32];
|
|
192 char *p=buf;
|
|
193 while (nchars--) {
|
|
194 while (Serial.available() < 1) NULL;
|
|
195 *(p++) = Serial.read();
|
|
196 }
|
|
197 *p = 0;
|
|
198 lcd.setCursor(row, col);
|
|
199 lcd.print(buf);
|
|
200 } else if (i == 2) { // set backlight
|
|
201 while (Serial.available() < 1) NULL;
|
|
202 backlightStandard = Serial.read();
|
|
203 } else {
|
|
204 // unknown command
|
|
205 }
|
153
|
206 }
|