From f2683a4b707cd714b7f540ebf6482563df83d51e Mon Sep 17 00:00:00 2001 From: erdgeist Date: Sat, 8 Apr 2017 14:21:36 +0200 Subject: Near complete rewrite. --- arduino/Laserharfe/Laserharfe.ino | 105 +++++++++++++++++++++++++++++--------- 1 file changed, 80 insertions(+), 25 deletions(-) (limited to 'arduino/Laserharfe') 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 @@ -/* - * MIDIUSB_test.ino - * - * Created: 4/6/2015 10:47:08 AM - * Author: gurbrinder grewal - * Modified by Arduino LLC (2015) - */ - #include "MIDIUSB.h" #include #include @@ -15,12 +7,16 @@ #include "engine.h" int led = 13; +int g_reading_config = 0; +int g_writing_config = 0; +ConfigSource g_config_source = source_none; +//Sd2Card card; enum { MODE_STANDALONE = 0, MODE_REPORTPOINTS = 1, - MODE_REPORTPOINTS_PLAY = 2, -} g_mode = MODE_REPORTPOINTS_PLAY; + MODE_REPORTPOINTS_PLAY = 2 +} g_mode = MODE_STANDALONE; uint32_t now() { return millis(); @@ -85,6 +81,7 @@ static void set_cam_register(uint8_t r, uint8_t v) { } static void setup_cam() { + digitalWrite(19, HIGH); // turn the LED on (HIGH is the voltage level) set_cam_register(0x30, 0x01); set_cam_register(0x06, 0x90); set_cam_register(0x08, 0xc0); @@ -99,7 +96,6 @@ void read_config() { Serial.println("initialization failed!"); return; } - File configfile = SD.open("laserhar.cfg", FILE_READ); if (!configfile) { Serial.println("opening config failed"); @@ -107,14 +103,15 @@ void read_config() { } char command[256]; - int command_len; - int skip_command; + int command_len = 0; + int skip_command = 0; while (configfile.available()) { char c = command[command_len++] = configfile.read(); if (c=='\n') { if (!skip_command) { command[command_len] = 0; + // Serial.write(command); config_handle_line(command); } skip_command = 0; @@ -126,9 +123,38 @@ void read_config() { command_len = 0; } } + + configfile.close(); +} + +void write_config() { + SD.remove("laserhar.cfg"); + File configfile = SD.open("laserhar.cfg", FILE_WRITE); + if (!configfile) { + Serial.println("opening config failed"); + return; + } + + char text[256]; + size_t len = config_dumpglobals(text, sizeof(text)); + configfile.write(text, len); + for (int i=0; i< g_string_count; ++i) { + len = config_dumpstring(i, text, sizeof(text)); + configfile.write(text, len); + } + Serial.println("-- DONE"); configfile.close(); } +void flashy_death() { + while(1) { + digitalWrite(led, LOW); // turn the LED on (HIGH is the voltage level) + delay(500); + digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) + delay(500); + } +} + void setup() { pinMode(led, OUTPUT); @@ -139,8 +165,8 @@ void setup() { // Let PWM settle for a bit delay(100); setup_cam(); - Serial.begin(115200); + delay(5000); read_config(); digitalWrite(led, LOW); // turn the LED on (HIGH is the voltage level) } @@ -220,10 +246,10 @@ void handle_midi(char *command) { return; if (command[6] != '\n') return; - midiEventPacket_t p = { m[0], (m[0]<<4) | m[1], (m[2]<<4) | m[3], (m[4]<<4) | m[5] }; MidiUSB.sendMIDI(p); - // MidiUSB.flush(); + MidiUSB.flush(); + Serial.println("- MIDI SENT"); } void midi_playnote(int channel, int note, int octave_offset ) { @@ -246,11 +272,20 @@ void midi_pitchbend( int channel, int pitch ) { MidiUSB.sendMIDI(p); } -void handle_configure(char *command) { +void handle_command(char *command) { + char text[512]; + size_t len; +// Serial.write( text, sprintf(text, "- %s\n", command)); -} + if (g_reading_config ) { + if ( !strcmp(command, "-- DONE")) { + g_reading_config = 0; + return; + } + config_handle_line(command); + return; + } -void handle_command(char *command) { switch (*command) { case 'M': /* Getting MIDI instruction */ handle_midi(command+1); @@ -266,12 +301,32 @@ void handle_command(char *command) { case '3': g_mode = MODE_REPORTPOINTS; break; - default: - break; } break; - case 'C': /* Getting configure command */ - handle_configure(command+1); + case 'C': /* Import a single config line from host */ + config_handle_line(command+1); + break; + case 'E': /* Export config from host, import here */ + config_reset(); + g_reading_config = true; + break; + case 'I': /* Import config at host */ + delay(1000); + len = config_dumpglobals(text, sizeof(text)); + Serial.write(text, len); + for (int i=0; i< g_string_count; ++i) { + len = config_dumpstring(i, text, sizeof(text)); + delay(100); + Serial.write(text, len); + } + delay(100); + Serial.println("-- DONE"); + break; + case 'R': /* Re-read config */ + read_config(); + break; + case 'W': /* Write local config */ + write_config(); break; default: break; @@ -279,7 +334,7 @@ void handle_command(char *command) { } void handle_serial() { - static char command[128]; + static char command[512]; static int command_len; static int skip_command; @@ -323,6 +378,6 @@ void loop() { */ engine_checksilence(millis()); - digitalWrite(led, ++led_state & 1 ? HIGH : LOW); // turn the LED on (HIGH is the voltage level) + digitalWrite(led, ++led_state & 0xff < 0x7f ? HIGH : LOW); // turn the LED on (HIGH is the voltage level) } -- cgit v1.2.3