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
|
159
|
23 #define motionIn 12
|
|
24
|
|
25
|
157
|
26 // see http://www.dfrobot.com/image/data/DFR0009/LCDKeypad%20Shield%20V1.0%20SCH.pdf
|
153
|
27 #define I2C_ADDR 0x27 // I2C address of PCF8574A
|
157
|
28 #define BACKLIGHT_PIN 10
|
153
|
29 #define En_pin 9
|
|
30 #define Rw_pin 1
|
|
31 #define Rs_pin 8
|
|
32 #define D4_pin 4
|
|
33 #define D5_pin 5
|
|
34 #define D6_pin 6
|
|
35 #define D7_pin 7
|
|
36
|
157
|
37 LiquidCrystal lcd(Rs_pin, Rw_pin, En_pin, D4_pin, D5_pin, D6_pin, D7_pin, BACKLIGHT_PIN, POSITIVE);
|
|
38 DFR_Key keypad;
|
153
|
39
|
157
|
40 byte backlightStandard = 0;
|
|
41 byte backlightCurrent = 0;
|
|
42 byte backlightKeypressBoost = 30;
|
|
43
|
|
44 int lastKey = -1;
|
|
45 int lastUnsentKeydown = -1;
|
|
46 uint16_t steps = 0;
|
|
47 uint16_t stepDelayPerBrightnessFade = 2048;
|
|
48
|
158
|
49 // uses pin 3
|
157
|
50 IRsend irsend;
|
|
51
|
|
52 void sendnec(byte addr, byte cmd) {
|
159
|
53 uint32_t w =
|
|
54 ((uint32_t(addr) << 24) & 0xff000000) |
|
|
55 (((~uint32_t(addr)) << 16) & 0x00ff0000) |
|
|
56 ((uint32_t(cmd) << 8) & 0x0000ff00) |
|
|
57 ((~uint32_t(cmd)) & 0x000000ff);
|
157
|
58 irsend.sendNEC(w, 32);
|
|
59 }
|
153
|
60
|
|
61 void setup(void) {
|
159
|
62 pinMode(motionIn, INPUT);
|
|
63 digitalWrite(motionIn, 1);
|
|
64
|
153
|
65 Serial.begin(115200);
|
157
|
66 keypad.setRate(10);
|
|
67 lcd.begin(16,2);
|
153
|
68 }
|
|
69
|
157
|
70
|
153
|
71 void loop() {
|
157
|
72 while (Serial.available() <= 2) {
|
|
73 int localKey = keypad.getKey();
|
|
74 if (localKey != SAMPLE_WAIT) {
|
|
75 if (localKey != lastKey) {
|
|
76 if (lastKey == 0) {
|
|
77 backlightCurrent = min(255, backlightStandard +
|
|
78 backlightKeypressBoost);
|
|
79 lastUnsentKeydown = localKey;
|
|
80 }
|
|
81 lastKey = localKey;
|
|
82 }
|
|
83 }
|
|
84
|
|
85 if (backlightCurrent > backlightStandard) {
|
|
86 steps++;
|
|
87 if (steps % stepDelayPerBrightnessFade == 0) {
|
|
88 backlightCurrent--;
|
|
89 }
|
|
90 } else if (backlightCurrent < backlightStandard) {
|
|
91 backlightCurrent = backlightStandard;
|
|
92 }
|
|
93 lcd.setBacklight(backlightCurrent);
|
|
94 }
|
|
95 byte i = Serial.read();
|
|
96 if (i != 0x60) {
|
|
97 return;
|
|
98 }
|
|
99 i = Serial.read(); // command
|
|
100 if (i == 0) { // get status
|
|
101 Serial.print('{');
|
|
102 if (lastUnsentKeydown != -1) {
|
|
103 Serial.print("\"keyDown\":");
|
|
104 Serial.print(lastUnsentKeydown);
|
|
105 Serial.print(",");
|
|
106 lastUnsentKeydown = -1;
|
|
107 }
|
|
108 Serial.print("\"slider1\":"); Serial.print(analogRead(1));
|
|
109 Serial.print(",\"slider2\":"); Serial.print(analogRead(2));
|
|
110 Serial.print(",\"slider3\":"); Serial.print(analogRead(3));
|
|
111 Serial.print(",\"slider4\":"); Serial.print(analogRead(4));
|
159
|
112 Serial.print(",\"motion\":"); Serial.print(digitalRead(motionIn));
|
157
|
113 Serial.print("}\n");
|
|
114 } else if (i == 1) { // write text
|
|
115 while (Serial.available() < 3) NULL;
|
|
116 byte row = Serial.read();
|
|
117 byte col = Serial.read();
|
|
118 byte nchars = Serial.read();
|
|
119 char buf[32];
|
|
120 char *p=buf;
|
|
121 while (nchars--) {
|
|
122 while (Serial.available() < 1) NULL;
|
|
123 *(p++) = Serial.read();
|
|
124 }
|
|
125 *p = 0;
|
|
126 lcd.setCursor(row, col);
|
|
127 lcd.print(buf);
|
|
128 } else if (i == 2) { // set backlight
|
|
129 while (Serial.available() < 1) NULL;
|
|
130 backlightStandard = Serial.read();
|
159
|
131 } else if (i == 3) { // IR NEC protocol
|
|
132 while (Serial.available() < 2) NULL;
|
|
133 byte addr = Serial.read();
|
|
134 sendnec(addr, Serial.read());
|
157
|
135 } else {
|
|
136 // unknown command
|
|
137 }
|
153
|
138 }
|