Mercurial > code > home > repos > homeauto
annotate service/busyboxArduino/main.ino @ 1229:02e4b84821d5
talk to store graph, second button for holding unlocked, etc
Ignore-this: c2ae7d756e743c26e5e01d99772899bd
darcs-hash:a0750d0bbc4dc7c0f65f63f3e7342b35a175141b
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Thu, 04 Apr 2019 02:16:22 -0700 |
parents | 6c31b682a7d7 |
children |
rev | line source |
---|---|
963 | 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 */ | |
958 | 18 #include <Arduino.h> |
19 #include <LiquidCrystal.h> | |
962 | 20 #include "DFR_Key.h" |
21 #include "IRremote.h" | |
958 | 22 |
964
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
23 #define motionIn 12 |
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
24 |
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
25 |
962 | 26 // see http://www.dfrobot.com/image/data/DFR0009/LCDKeypad%20Shield%20V1.0%20SCH.pdf |
958 | 27 #define I2C_ADDR 0x27 // I2C address of PCF8574A |
962 | 28 #define BACKLIGHT_PIN 10 |
958 | 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 | |
962 | 37 LiquidCrystal lcd(Rs_pin, Rw_pin, En_pin, D4_pin, D5_pin, D6_pin, D7_pin, BACKLIGHT_PIN, POSITIVE); |
38 DFR_Key keypad; | |
958 | 39 |
962 | 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 | |
963 | 49 // uses pin 3 |
962 | 50 IRsend irsend; |
51 | |
52 void sendnec(byte addr, byte cmd) { | |
964
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
53 uint32_t w = |
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
54 ((uint32_t(addr) << 24) & 0xff000000) | |
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
55 (((~uint32_t(addr)) << 16) & 0x00ff0000) | |
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
56 ((uint32_t(cmd) << 8) & 0x0000ff00) | |
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
57 ((~uint32_t(cmd)) & 0x000000ff); |
962 | 58 irsend.sendNEC(w, 32); |
59 } | |
958 | 60 |
61 void setup(void) { | |
964
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
62 pinMode(motionIn, INPUT); |
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
63 digitalWrite(motionIn, 1); |
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
64 |
958 | 65 Serial.begin(115200); |
962 | 66 keypad.setRate(10); |
67 lcd.begin(16,2); | |
958 | 68 } |
69 | |
962 | 70 |
958 | 71 void loop() { |
962 | 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)); | |
964
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
112 Serial.print(",\"motion\":"); Serial.print(digitalRead(motionIn)); |
962 | 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(); | |
964
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
131 } else if (i == 3) { // IR NEC protocol |
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
132 while (Serial.available() < 2) NULL; |
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
133 byte addr = Serial.read(); |
6c31b682a7d7
busybox client can send IR codes
drewp <drewp@bigasterisk.com>
parents:
963
diff
changeset
|
134 sendnec(addr, Serial.read()); |
962 | 135 } else { |
136 // unknown command | |
137 } | |
958 | 138 } |