annotate service/busyboxArduino/main.ino @ 294:14ac4a210dbc

Copy from from https://github.com/juggernaut/twisted-sse-demo Ignore-this: a252d3c6e023cb83fd10bfbae78e05d9
author drewp@bigasterisk.com
date Fri, 19 Aug 2016 10:53:03 -0700
parents 4c62b6d416a7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
158
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
1 /*
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
2 D0 <---------
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
3 D1 lcd rw
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
4 D2 <---------
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
5 D3 ir out -----
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
6 D4 lcd 4
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
7 D5 lcd 5
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
8 D6 lcd 6
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
9 D7 lcd 7
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
10 D8 lcd rs
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
11 D9 lcd en
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
12 D10 lcd backlight
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
13 D11 tbd <------------
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
14 D12 motion in <--------
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
15 D13 debug led <--------
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
16
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
17 */
153
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
18 #include <Arduino.h>
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
19 #include <LiquidCrystal.h>
157
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
20 #include "DFR_Key.h"
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
21 #include "IRremote.h"
153
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
22
159
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
23 #define motionIn 12
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
24
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
25
157
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
26 // see http://www.dfrobot.com/image/data/DFR0009/LCDKeypad%20Shield%20V1.0%20SCH.pdf
153
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
27 #define I2C_ADDR 0x27 // I2C address of PCF8574A
157
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
28 #define BACKLIGHT_PIN 10
153
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
29 #define En_pin 9
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
30 #define Rw_pin 1
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
31 #define Rs_pin 8
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
32 #define D4_pin 4
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
33 #define D5_pin 5
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
34 #define D6_pin 6
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
35 #define D7_pin 7
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
36
157
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
37 LiquidCrystal lcd(Rs_pin, Rw_pin, En_pin, D4_pin, D5_pin, D6_pin, D7_pin, BACKLIGHT_PIN, POSITIVE);
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
38 DFR_Key keypad;
153
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
39
157
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
40 byte backlightStandard = 0;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
41 byte backlightCurrent = 0;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
42 byte backlightKeypressBoost = 30;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
43
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
44 int lastKey = -1;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
45 int lastUnsentKeydown = -1;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
46 uint16_t steps = 0;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
47 uint16_t stepDelayPerBrightnessFade = 2048;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
48
158
75b1dc5c0bdc notes on pins
drewp@bigasterisk.com
parents: 157
diff changeset
49 // uses pin 3
157
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
50 IRsend irsend;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
51
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
52 void sendnec(byte addr, byte cmd) {
159
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
53 uint32_t w =
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
54 ((uint32_t(addr) << 24) & 0xff000000) |
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
55 (((~uint32_t(addr)) << 16) & 0x00ff0000) |
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
56 ((uint32_t(cmd) << 8) & 0x0000ff00) |
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
57 ((~uint32_t(cmd)) & 0x000000ff);
157
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
58 irsend.sendNEC(w, 32);
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
59 }
153
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
60
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
61 void setup(void) {
159
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
62 pinMode(motionIn, INPUT);
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
63 digitalWrite(motionIn, 1);
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
64
153
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
65 Serial.begin(115200);
157
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
66 keypad.setRate(10);
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
67 lcd.begin(16,2);
153
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
68 }
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
69
157
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
70
153
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
71 void loop() {
157
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
72 while (Serial.available() <= 2) {
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
73 int localKey = keypad.getKey();
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
74 if (localKey != SAMPLE_WAIT) {
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
75 if (localKey != lastKey) {
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
76 if (lastKey == 0) {
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
77 backlightCurrent = min(255, backlightStandard +
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
78 backlightKeypressBoost);
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
79 lastUnsentKeydown = localKey;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
80 }
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
81 lastKey = localKey;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
82 }
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
83 }
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
84
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
85 if (backlightCurrent > backlightStandard) {
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
86 steps++;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
87 if (steps % stepDelayPerBrightnessFade == 0) {
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
88 backlightCurrent--;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
89 }
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
90 } else if (backlightCurrent < backlightStandard) {
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
91 backlightCurrent = backlightStandard;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
92 }
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
93 lcd.setBacklight(backlightCurrent);
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
94 }
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
95 byte i = Serial.read();
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
96 if (i != 0x60) {
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
97 return;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
98 }
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
99 i = Serial.read(); // command
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
100 if (i == 0) { // get status
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
101 Serial.print('{');
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
102 if (lastUnsentKeydown != -1) {
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
103 Serial.print("\"keyDown\":");
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
104 Serial.print(lastUnsentKeydown);
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
105 Serial.print(",");
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
106 lastUnsentKeydown = -1;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
107 }
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
108 Serial.print("\"slider1\":"); Serial.print(analogRead(1));
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
109 Serial.print(",\"slider2\":"); Serial.print(analogRead(2));
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
110 Serial.print(",\"slider3\":"); Serial.print(analogRead(3));
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
111 Serial.print(",\"slider4\":"); Serial.print(analogRead(4));
159
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
112 Serial.print(",\"motion\":"); Serial.print(digitalRead(motionIn));
157
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
113 Serial.print("}\n");
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
114 } else if (i == 1) { // write text
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
115 while (Serial.available() < 3) NULL;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
116 byte row = Serial.read();
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
117 byte col = Serial.read();
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
118 byte nchars = Serial.read();
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
119 char buf[32];
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
120 char *p=buf;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
121 while (nchars--) {
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
122 while (Serial.available() < 1) NULL;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
123 *(p++) = Serial.read();
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
124 }
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
125 *p = 0;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
126 lcd.setCursor(row, col);
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
127 lcd.print(buf);
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
128 } else if (i == 2) { // set backlight
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
129 while (Serial.available() < 1) NULL;
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
130 backlightStandard = Serial.read();
159
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
131 } else if (i == 3) { // IR NEC protocol
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
132 while (Serial.available() < 2) NULL;
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
133 byte addr = Serial.read();
4c62b6d416a7 busybox client can send IR codes
drewp@bigasterisk.com
parents: 158
diff changeset
134 sendnec(addr, Serial.read());
157
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
135 } else {
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
136 // unknown command
28c2db876548 busybox py and arduino
drewp@bigasterisk.com
parents: 153
diff changeset
137 }
153
f8c5ec994768 start buttons+lcd program
drewp@bigasterisk.com
parents:
diff changeset
138 }