summaryrefslogtreecommitdiff
path: root/arduino
diff options
context:
space:
mode:
Diffstat (limited to 'arduino')
-rw-r--r--arduino/Laserharfe/Laserharfe.ino105
1 files changed, 80 insertions, 25 deletions
diff --git a/arduino/Laserharfe/Laserharfe.ino b/arduino/Laserharfe/Laserharfe.ino
index a0e8d07..107dffb 100644
--- a/arduino/Laserharfe/Laserharfe.ino
+++ b/arduino/Laserharfe/Laserharfe.ino
@@ -1,11 +1,3 @@
1/*
2 * MIDIUSB_test.ino
3 *
4 * Created: 4/6/2015 10:47:08 AM
5 * Author: gurbrinder grewal
6 * Modified by Arduino LLC (2015)
7 */
8
9#include "MIDIUSB.h" 1#include "MIDIUSB.h"
10#include <SD.h> 2#include <SD.h>
11#include <Wire.h> 3#include <Wire.h>
@@ -15,12 +7,16 @@
15#include "engine.h" 7#include "engine.h"
16 8
17int led = 13; 9int led = 13;
10int g_reading_config = 0;
11int g_writing_config = 0;
12ConfigSource g_config_source = source_none;
13//Sd2Card card;
18 14
19enum { 15enum {
20 MODE_STANDALONE = 0, 16 MODE_STANDALONE = 0,
21 MODE_REPORTPOINTS = 1, 17 MODE_REPORTPOINTS = 1,
22 MODE_REPORTPOINTS_PLAY = 2, 18 MODE_REPORTPOINTS_PLAY = 2
23} g_mode = MODE_REPORTPOINTS_PLAY; 19} g_mode = MODE_STANDALONE;
24 20
25uint32_t now() { 21uint32_t now() {
26 return millis(); 22 return millis();
@@ -85,6 +81,7 @@ static void set_cam_register(uint8_t r, uint8_t v) {
85} 81}
86 82
87static void setup_cam() { 83static void setup_cam() {
84 digitalWrite(19, HIGH); // turn the LED on (HIGH is the voltage level)
88 set_cam_register(0x30, 0x01); 85 set_cam_register(0x30, 0x01);
89 set_cam_register(0x06, 0x90); 86 set_cam_register(0x06, 0x90);
90 set_cam_register(0x08, 0xc0); 87 set_cam_register(0x08, 0xc0);
@@ -99,7 +96,6 @@ void read_config() {
99 Serial.println("initialization failed!"); 96 Serial.println("initialization failed!");
100 return; 97 return;
101 } 98 }
102
103 File configfile = SD.open("laserhar.cfg", FILE_READ); 99 File configfile = SD.open("laserhar.cfg", FILE_READ);
104 if (!configfile) { 100 if (!configfile) {
105 Serial.println("opening config failed"); 101 Serial.println("opening config failed");
@@ -107,14 +103,15 @@ void read_config() {
107 } 103 }
108 104
109 char command[256]; 105 char command[256];
110 int command_len; 106 int command_len = 0;
111 int skip_command; 107 int skip_command = 0;
112 108
113 while (configfile.available()) { 109 while (configfile.available()) {
114 char c = command[command_len++] = configfile.read(); 110 char c = command[command_len++] = configfile.read();
115 if (c=='\n') { 111 if (c=='\n') {
116 if (!skip_command) { 112 if (!skip_command) {
117 command[command_len] = 0; 113 command[command_len] = 0;
114 // Serial.write(command);
118 config_handle_line(command); 115 config_handle_line(command);
119 } 116 }
120 skip_command = 0; 117 skip_command = 0;
@@ -126,9 +123,38 @@ void read_config() {
126 command_len = 0; 123 command_len = 0;
127 } 124 }
128 } 125 }
126
127 configfile.close();
128}
129
130void write_config() {
131 SD.remove("laserhar.cfg");
132 File configfile = SD.open("laserhar.cfg", FILE_WRITE);
133 if (!configfile) {
134 Serial.println("opening config failed");
135 return;
136 }
137
138 char text[256];
139 size_t len = config_dumpglobals(text, sizeof(text));
140 configfile.write(text, len);
141 for (int i=0; i< g_string_count; ++i) {
142 len = config_dumpstring(i, text, sizeof(text));
143 configfile.write(text, len);
144 }
145 Serial.println("-- DONE");
129 configfile.close(); 146 configfile.close();
130} 147}
131 148
149void flashy_death() {
150 while(1) {
151 digitalWrite(led, LOW); // turn the LED on (HIGH is the voltage level)
152 delay(500);
153 digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
154 delay(500);
155 }
156}
157
132void setup() { 158void setup() {
133 pinMode(led, OUTPUT); 159 pinMode(led, OUTPUT);
134 160
@@ -139,8 +165,8 @@ void setup() {
139 // Let PWM settle for a bit 165 // Let PWM settle for a bit
140 delay(100); 166 delay(100);
141 setup_cam(); 167 setup_cam();
142
143 Serial.begin(115200); 168 Serial.begin(115200);
169 delay(5000);
144 read_config(); 170 read_config();
145 digitalWrite(led, LOW); // turn the LED on (HIGH is the voltage level) 171 digitalWrite(led, LOW); // turn the LED on (HIGH is the voltage level)
146} 172}
@@ -220,10 +246,10 @@ void handle_midi(char *command) {
220 return; 246 return;
221 if (command[6] != '\n') 247 if (command[6] != '\n')
222 return; 248 return;
223
224 midiEventPacket_t p = { m[0], (m[0]<<4) | m[1], (m[2]<<4) | m[3], (m[4]<<4) | m[5] }; 249 midiEventPacket_t p = { m[0], (m[0]<<4) | m[1], (m[2]<<4) | m[3], (m[4]<<4) | m[5] };
225 MidiUSB.sendMIDI(p); 250 MidiUSB.sendMIDI(p);
226 // MidiUSB.flush(); 251 MidiUSB.flush();
252 Serial.println("- MIDI SENT");
227} 253}
228 254
229void midi_playnote(int channel, int note, int octave_offset ) { 255void midi_playnote(int channel, int note, int octave_offset ) {
@@ -246,11 +272,20 @@ void midi_pitchbend( int channel, int pitch ) {
246 MidiUSB.sendMIDI(p); 272 MidiUSB.sendMIDI(p);
247} 273}
248 274
249void handle_configure(char *command) { 275void handle_command(char *command) {
276 char text[512];
277 size_t len;
278// Serial.write( text, sprintf(text, "- %s\n", command));
250 279
251} 280 if (g_reading_config ) {
281 if ( !strcmp(command, "-- DONE")) {
282 g_reading_config = 0;
283 return;
284 }
285 config_handle_line(command);
286 return;
287 }
252 288
253void handle_command(char *command) {
254 switch (*command) { 289 switch (*command) {
255 case 'M': /* Getting MIDI instruction */ 290 case 'M': /* Getting MIDI instruction */
256 handle_midi(command+1); 291 handle_midi(command+1);
@@ -266,12 +301,32 @@ void handle_command(char *command) {
266 case '3': 301 case '3':
267 g_mode = MODE_REPORTPOINTS; 302 g_mode = MODE_REPORTPOINTS;
268 break; 303 break;
269 default:
270 break;
271 } 304 }
272 break; 305 break;
273 case 'C': /* Getting configure command */ 306 case 'C': /* Import a single config line from host */
274 handle_configure(command+1); 307 config_handle_line(command+1);
308 break;
309 case 'E': /* Export config from host, import here */
310 config_reset();
311 g_reading_config = true;
312 break;
313 case 'I': /* Import config at host */
314 delay(1000);
315 len = config_dumpglobals(text, sizeof(text));
316 Serial.write(text, len);
317 for (int i=0; i< g_string_count; ++i) {
318 len = config_dumpstring(i, text, sizeof(text));
319 delay(100);
320 Serial.write(text, len);
321 }
322 delay(100);
323 Serial.println("-- DONE");
324 break;
325 case 'R': /* Re-read config */
326 read_config();
327 break;
328 case 'W': /* Write local config */
329 write_config();
275 break; 330 break;
276 default: 331 default:
277 break; 332 break;
@@ -279,7 +334,7 @@ void handle_command(char *command) {
279} 334}
280 335
281void handle_serial() { 336void handle_serial() {
282 static char command[128]; 337 static char command[512];
283 static int command_len; 338 static int command_len;
284 static int skip_command; 339 static int skip_command;
285 340
@@ -323,6 +378,6 @@ void loop() {
323*/ 378*/
324 379
325 engine_checksilence(millis()); 380 engine_checksilence(millis());
326 digitalWrite(led, ++led_state & 1 ? HIGH : LOW); // turn the LED on (HIGH is the voltage level) 381 digitalWrite(led, ++led_state & 0xff < 0x7f ? HIGH : LOW); // turn the LED on (HIGH is the voltage level)
327} 382}
328 383