Browse Source

Updated Arduino Sketch to use proper State Machine

Thomas Buck 8 years ago
parent
commit
052956c8d1
3 changed files with 84 additions and 48 deletions
  1. 82
    46
      CaseLights.ino
  2. 1
    1
      CaseLights/Info.plist
  3. 1
    1
      CaseLights/Serial.m

+ 82
- 46
CaseLights.ino View File

13
  * The UV command turns the UV lights on or off (s can be 0 or 1).
13
  * The UV command turns the UV lights on or off (s can be 0 or 1).
14
  */
14
  */
15
 
15
 
16
-//#define DEBUG
16
+#define DEBUG
17
+
18
+enum LoopState {
19
+  LOOP_IDLE,
20
+  LOOP_R,
21
+  LOOP_G,
22
+  LOOP_B,
23
+  LOOP_NUM1,
24
+  LOOP_NUM2,
25
+  LOOP_U,
26
+  LOOP_V
27
+};
17
 
28
 
18
 static int redPin = 10;
29
 static int redPin = 10;
19
 static int greenPin = 9;
30
 static int greenPin = 9;
20
 static int bluePin = 11;
31
 static int bluePin = 11;
21
 static int uvPin = 13;
32
 static int uvPin = 13;
33
+static LoopState state = LOOP_IDLE;
34
+static int r = 0, g = 0, b = 0;
22
 
35
 
23
 void setup() {
36
 void setup() {
24
   Serial.begin(115200);
37
   Serial.begin(115200);
38
+  Serial.setTimeout(5000);
25
   
39
   
26
   pinMode(redPin, OUTPUT);
40
   pinMode(redPin, OUTPUT);
27
   pinMode(greenPin, OUTPUT);
41
   pinMode(greenPin, OUTPUT);
40
 
54
 
41
 void loop() {
55
 void loop() {
42
   if (Serial.available() > 0) {
56
   if (Serial.available() > 0) {
43
-    int c = Serial.read();
44
-    if (c == 'R') {
45
-      c = Serial.read();
46
-      if (c == 'G') {
47
-        c = Serial.read();
48
-        if (c == 'B') {
49
-          int r = Serial.parseInt();
50
-          int g = Serial.parseInt();
51
-          int b = Serial.parseInt();
52
-          analogWrite(redPin, r);
53
-          analogWrite(greenPin, g);
54
-          analogWrite(bluePin, b);
57
+    if (state == LOOP_IDLE) {
58
+      int c = Serial.read();
59
+      if ((c == 'R') || (c == 'r')) {
60
+        state = LOOP_R;
61
+      } else if ((c == 'U') || (c == 'u')) {
62
+        state = LOOP_U;
63
+      } else if ((c == '\r') || (c == '\n')) {
55
 #ifdef DEBUG
64
 #ifdef DEBUG
56
-          Serial.print("RGB set ");
57
-          Serial.print(r);
58
-          Serial.print(' ');
59
-          Serial.print(g);
60
-          Serial.print(' ');
61
-          Serial.print(b);
62
-          Serial.println();
65
+        Serial.println("Skipping newline...");
63
 #endif
66
 #endif
64
-        } else {
67
+      } else {
65
 #ifdef DEBUG
68
 #ifdef DEBUG
66
-          Serial.print("Invalid character after G: ");
67
-          Serial.print(c);
68
-          Serial.println();
69
+        Serial.print("Invalid character: ");
70
+        Serial.print(c);
71
+        Serial.println();
69
 #endif
72
 #endif
70
-        }
73
+      }
74
+    } else if (state == LOOP_R) {
75
+      int c = Serial.read();
76
+      if ((c == 'G') || (c == 'g')) {
77
+        state = LOOP_G;
71
       } else {
78
       } else {
79
+        state = LOOP_IDLE;
72
 #ifdef DEBUG
80
 #ifdef DEBUG
73
         Serial.print("Invalid character after R: ");
81
         Serial.print("Invalid character after R: ");
74
         Serial.print(c);
82
         Serial.print(c);
75
         Serial.println();
83
         Serial.println();
76
 #endif
84
 #endif
77
       }
85
       }
78
-    } else if (c == 'U') {
79
-      c = Serial.read();
80
-      if (c == 'V') {
81
-        c = Serial.parseInt();
82
-        if (c == 0) {
83
-          digitalWrite(uvPin, LOW);
84
-#ifdef DEBUG
85
-          Serial.println("UV off");
86
-#endif
87
-        } else if (c == 1) {
88
-          digitalWrite(uvPin, HIGH);
86
+    } else if (state == LOOP_G) {
87
+      int c = Serial.read();
88
+      if ((c == 'B') || (c == 'b')) {
89
+        state = LOOP_B;
90
+      } else {
91
+        state = LOOP_IDLE;
89
 #ifdef DEBUG
92
 #ifdef DEBUG
90
-          Serial.println("UV on");
93
+        Serial.print("Invalid character after G: ");
94
+        Serial.print(c);
95
+        Serial.println();
91
 #endif
96
 #endif
92
-        } else {
97
+      }
98
+    } else if (state == LOOP_B) {
99
+      r = Serial.parseInt();
100
+      state = LOOP_NUM1;
101
+    } else if (state == LOOP_NUM1) {
102
+      g = Serial.parseInt();
103
+      state = LOOP_NUM2;
104
+    } else if (state == LOOP_NUM2) {
105
+      b = Serial.parseInt();
106
+      analogWrite(redPin, r);
107
+      analogWrite(greenPin, g);
108
+      analogWrite(bluePin, b);
93
 #ifdef DEBUG
109
 #ifdef DEBUG
94
-          Serial.print("Invalid character for UV: ");
95
-          Serial.print(c);
96
-          Serial.println();
110
+      Serial.print("RGB set ");
111
+      Serial.print(r);
112
+      Serial.print(' ');
113
+      Serial.print(g);
114
+      Serial.print(' ');
115
+      Serial.print(b);
116
+      Serial.println();
97
 #endif
117
 #endif
98
-        }
118
+      state = LOOP_IDLE;
119
+    } else if (state == LOOP_U) {
120
+      int c = Serial.read();
121
+      if ((c == 'V') || (c == 'v')) {
122
+        state = LOOP_V;
99
       } else {
123
       } else {
124
+        state = LOOP_IDLE;
100
 #ifdef DEBUG
125
 #ifdef DEBUG
101
         Serial.print("Invalid character after U: ");
126
         Serial.print("Invalid character after U: ");
102
         Serial.print(c);
127
         Serial.print(c);
103
         Serial.println();
128
         Serial.println();
104
 #endif
129
 #endif
105
       }
130
       }
106
-    } else if ((c == '\n') || (c == '\r')) {
131
+    } else if (state == LOOP_V) {
132
+      int n = Serial.parseInt();
133
+      if (n == 0) {
134
+        digitalWrite(uvPin, LOW);
107
 #ifdef DEBUG
135
 #ifdef DEBUG
108
-      Serial.println("Skipping new-line or carriage-return...");
136
+        Serial.println("UV off");
109
 #endif
137
 #endif
138
+      } else {
139
+        digitalWrite(uvPin, HIGH);
140
+#ifdef DEBUG
141
+        Serial.println("UV on");
142
+#endif
143
+      }
144
+      state = LOOP_IDLE;
110
     } else {
145
     } else {
146
+      state = LOOP_IDLE;
111
 #ifdef DEBUG
147
 #ifdef DEBUG
112
-      Serial.print("Invalid character: ");
113
-      Serial.print(c);
148
+      Serial.print("Invalid state: ");
149
+      Serial.print(state);
114
       Serial.println();
150
       Serial.println();
115
 #endif
151
 #endif
116
     }
152
     }

+ 1
- 1
CaseLights/Info.plist View File

21
 	<key>CFBundleSignature</key>
21
 	<key>CFBundleSignature</key>
22
 	<string>????</string>
22
 	<string>????</string>
23
 	<key>CFBundleVersion</key>
23
 	<key>CFBundleVersion</key>
24
-	<string>117</string>
24
+	<string>118</string>
25
 	<key>LSApplicationCategoryType</key>
25
 	<key>LSApplicationCategoryType</key>
26
 	<string>public.app-category.utilities</string>
26
 	<string>public.app-category.utilities</string>
27
 	<key>LSMinimumSystemVersion</key>
27
 	<key>LSMinimumSystemVersion</key>

+ 1
- 1
CaseLights/Serial.m View File

251
         // incoming calls, for example, a fax listener.
251
         // incoming calls, for example, a fax listener.
252
         
252
         
253
         deviceFilePathAsCFString = IORegistryEntryCreateCFProperty(modemService,
253
         deviceFilePathAsCFString = IORegistryEntryCreateCFProperty(modemService,
254
-                                                                   CFSTR(kIODialinDeviceKey),
254
+                                                                   CFSTR(kIOCalloutDeviceKey),
255
                                                                    kCFAllocatorDefault,
255
                                                                    kCFAllocatorDefault,
256
                                                                    0);
256
                                                                    0);
257
         if (deviceFilePathAsCFString) {
257
         if (deviceFilePathAsCFString) {

Loading…
Cancel
Save