From 17f6d215efdf1aef830f8be00093070557746245 Mon Sep 17 00:00:00 2001 From: Admar Schoonen Date: Sun, 8 Jan 2012 01:39:30 +0100 Subject: [PATCH] Added simple serial get/set time parser --- wordclock/wordclock.ino | 197 +++++++++++++++++++++++++++++++++++----- 1 file changed, 176 insertions(+), 21 deletions(-) diff --git a/wordclock/wordclock.ino b/wordclock/wordclock.ino index d4a6ac7..f415113 100644 --- a/wordclock/wordclock.ino +++ b/wordclock/wordclock.ino @@ -88,11 +88,11 @@ void SWversion(void); #define N_PWM_STEPS 11 // DAY Brightness setting 0 = off, N_PWM_STEPS - 1 = full -#define MAXBRIGHTNESS 10 +#define MAXBRIGHTNESS (N_PWM_STEPS - 1) // start MAXBRIGHTNESS at DAYLIGHTHOUR (7 am) #define DAYLIGHTHOUR 7 // NIGHT Brightness setting 0 = off, N_PWM_STEPS - 1 = full -#define MINBRIGHTNESS 1 +#define MINBRIGHTNESS 3 // start MINBRIGHTNESS at NIGHTLIGHTHOUR (7 pm) #define NIGHTLIGHTHOUR 19 @@ -149,6 +149,8 @@ int current_brightnes=0; char ambient_light_to_brightness[1024]; +String inputBuffer = ""; // input buffer for serial port +volatile boolean inputBufferComplete = false; // define the language to be used for this project: #include LANGUAGE // The language pack @@ -163,7 +165,8 @@ char buf[50]; // time output string for debugging DS1302 rtc(DS1302CEPin, DS1302IOPin, DS1302CLKPin); // ProtoThread structures (one for each thread) -static struct pt blink_thread_pt, lightsensor_thread_pt, buttons_thread_pt, wordclock_thread_pt; +static struct pt blink_thread_pt, lightsensor_thread_pt, buttons_thread_pt, + uart_thread_pt, wordclock_thread_pt; void print_DS1302time() { @@ -171,12 +174,11 @@ void print_DS1302time() Time t = rtc.time(); /* Format the time and date and insert into the temporary buffer */ - snprintf(buf, sizeof(buf), "DS1302 time: %02d:%02d:%02d", + snprintf(buf, sizeof(buf), "D: DS1302 time: %02d:%02d:%02d", t.hr, t.min, t.sec); /* Print the formatted string to serial so we can see the time */ Serial.println(buf); - } @@ -218,10 +220,11 @@ void setup() //Serial.begin(9600); // setup the serial port to 9600 baud Serial.begin(115200); // setup the serial port to 115200 baud + inputBuffer.reserve(256); SWversion(); // Display the version number of the software // test whether the DS1302 is there - Serial.print("Verifying DS1302 "); + Serial.print("D: Verifying DS1302 "); // start by verifying that the chip has a valid signature if (rtc.read_register(0x20) == 0xa5) { // Signature is there - set the present flag and mmove on @@ -289,7 +292,7 @@ void setup() OldHardware=0; if ( digitalRead(FWD_BUTTON_PIN)==0 && digitalRead(REV_BUTTON_PIN)==0) { - Serial.println("Detected Old Hardware"); + Serial.println("D: Detected Old Hardware"); OldHardware=1; // we have old hardware BTNActive = 1; // True = active for old hardware digitalWrite(FWD_BUTTON_PIN,LOW); // Turn off weak pullups @@ -298,7 +301,7 @@ void setup() } else { - Serial.println("Detected New Hardware"); + Serial.println("D: Detected New Hardware"); OldHardware=0; // we have new hardware BTNActive = 0; // True = active for old hardware } @@ -317,7 +320,7 @@ void setup() RESET_TIMER2; sei(); - Serial.println("Finished setting up Timer2 Interrupt"); + Serial.println("D: Finished setting up Timer2 Interrupt"); @@ -338,6 +341,18 @@ void setup() displaytime(); // display the current time } +void serialEvent() +{ + while (Serial.available()) { + // get the new byte + char inChar = (char) Serial.read(); + inputBuffer += inChar; + + // 0x0D == enter + if (inChar == 0x0D) + inputBufferComplete = true; + } +} // Interrupt handler - Arduino runs at 16 Mhz, so we have 1000 Overflows per second... // 1/ ((16000000 / 64) / 256) = 1 / 1000 @@ -410,10 +425,10 @@ void incrementtime(void){ } void SWversion(void) { - Serial.println(); - Serial.println("Wordclock -Arduino v3.0a - reduced brightness version"); - Serial.print(LANGUAGE); Serial.println(" header file used"); - Serial.println("(c)2009, 2010, 2011 Doug Jackson"); + Serial.println("D:"); + Serial.println("D: Wordclock -Arduino v3.0a - reduced brightness version"); + Serial.print("D: "); Serial.print(LANGUAGE); Serial.println(" header file used"); + Serial.println("D: (c)2009, 2010, 2011 Doug Jackson"); } void process_buttons(unsigned char button_status) @@ -448,7 +463,7 @@ void process_buttons(unsigned char button_status) // down again. //if ( digitalRead(FWD_BUTTON_PIN)==BTNActive && digitalRead(REV_BUTTON_PIN)==BTNActive) if (selftestmode) { - Serial.println("Selftest Mode TRUE"); + Serial.println("D: Selftest Mode TRUE"); for(int i=0; i<100; i++) { Display1=255; Display2=255; Display3=255; delay(101-i); @@ -464,7 +479,7 @@ void process_buttons(unsigned char button_status) // the forward button is down // and it has been more than one second since we // last looked - Serial.println("Forward Button Down"); + Serial.println("D: Forward Button Down"); incrementtime(); second++; // Increment the second counter to ensure that the name // flash doesnt happen when setting time @@ -479,7 +494,7 @@ void process_buttons(unsigned char button_status) if (revButtonPressed) { - Serial.println("Backwards Button Down"); + Serial.println("D: Backwards Button Down"); minute--; minute--; second=0; // decrement the minute counter @@ -515,6 +530,145 @@ void process_buttons(unsigned char button_status) buttonPressedMillis = millis(); } +static int uart_thread(struct pt *pt) +{ + char cmd, arg1, arg2, arg3; + int length; + + PT_BEGIN(pt); + + while(1) + { + PT_WAIT_UNTIL(pt, inputBufferComplete == true); + length = inputBuffer.length(); + if (length == 0) + Serial.println("E: received 0 characters"); + else + { + cmd = inputBuffer.charAt(0); + + switch(cmd) + { + case 'H': + // process hello request + Serial.println("ELLO"); + break; + case 'S': + // process set request + if (length < 4) + Serial.println("D: too few arguments"); + else + { + Serial.println("D: set request"); + arg1 = inputBuffer.charAt(1); + arg2 = inputBuffer.charAt(2); + arg3 = inputBuffer.charAt(3); + + switch(arg1) + { + case 'Y': + // set year + break; + case 'M': + // set month + break; + case 'D': + // set day of month + break; + case 'd': + // set day of week + break; + + case 'h': + // set hour (24 hour format) + hour = 10 * (arg2 - 0x30) + + (arg3 - 0x30); + break; + case 'm': + // set minutes + minute = 10 * (arg2 - 0x30) + + (arg3 - 0x30); + break; + case 's': + // set seconds + second = 10 * (arg2 - 0x30) + + (arg3 - 0x30); + break; + default: + break; + } + + // Set the time and date on the chip + Time t(2010, 4, 28, hour, minute, + second, 1); + rtc.time(t); + displaytime(); + } + break; + case 'G': + // process get request + if (length < 2) + Serial.println("D: too few arguments"); + else + { + arg1 = inputBuffer.charAt(1); + + switch(arg1) + { + case 'Y': + // get year + break; + case 'M': + // get month + break; + case 'D': + // get day of month + break; + case 'd': + // get day of week + break; + + case 'h': + // get hour (24 hour format) + Serial.print("g: h: "); + if (hour < 10) + Serial.print(0, DEC); + Serial.println(hour); + break; + case 'm': + // get minutes + Serial.print("g: m: "); + if (minute < 10) + Serial.print(0, DEC); + Serial.println(minute); + break; + case 's': + // get seconds + Serial.print("g: s: "); + if (second < 10) + Serial.print(0, DEC); + Serial.println(second); + break; + default: + break; + } + } + + break; + default: + Serial.print("E: unknown command: 0x"); + Serial.println(cmd, HEX); + break; + } + } + + inputBuffer = ""; + inputBufferComplete = false; + } + + PT_END(pt); +} + static int buttons_thread(struct pt *pt) { PT_BEGIN(pt); @@ -591,12 +745,12 @@ static int lightsensor_thread(struct pt *pt) current_brightnes = ambient_light_to_brightness[lightlevel_avg]; - Serial.print("lightsensor: "); + /*Serial.print("D: lightsensor: "); Serial.print(lightlevel_sample); - Serial.print(" / "); + Serial.print(", avg: "); Serial.print(lightlevel_avg); Serial.print(", brightness: "); - Serial.println(current_brightnes); + Serial.println(current_brightnes);*/ } PT_END(pt); @@ -614,7 +768,7 @@ static int blink_thread(struct pt *pt) PT_WAIT_UNTIL(pt, millisWillOverflow ? (millis() - msTick > 999) : ((millis() < 4294967295 - 998) && (millis() - msTick > 999))); msTick=millis(); - Serial.println(":)"); + Serial.println("D: blink thread is alive"); // Flash the onboard Pin13 Led so we know something is hapening! digitalWrite(13,HIGH); @@ -668,7 +822,7 @@ static int wordclock_thread(struct pt *pt) { incrementtime(); displaytime(); - Serial.print("time: "); + Serial.print("D: time: "); Serial.print(hour, DEC); Serial.print(":"); Serial.print(minute, DEC); @@ -705,5 +859,6 @@ void loop() lightsensor_thread(&lightsensor_thread_pt); #endif blink_thread(&blink_thread_pt); + uart_thread(&uart_thread_pt); }