Fixes strange crash (?) + printdebug level support

This commit is contained in:
Admar Schoonen 2012-01-08 17:39:07 +01:00
parent c5dc50fcdd
commit ff98f561af
1 changed files with 79 additions and 24 deletions

View File

@ -18,6 +18,7 @@ void SWversion(void);
// available choices - English, Danish, French, German
//#define LANGUAGE "English.h"
#define LANGUAGE "Dutch.h"
#define PRINT_DEBUG_LEVEL 1
/**************************************************************************
@ -133,6 +134,9 @@ int LEDStrobePin=4; // Arduino Pin#6 - 4094 pin 1 Strobe
// delay in ms before repeating key a second time
#define BUTTON_DELAY_SHORT 200
// length of buffer where we receive data over uart
#define INPUTBUFFER_LENGTH 16
// 1302 RTC Constants
int DS1302IOPin=10;
@ -145,7 +149,7 @@ int LED2PIN=18; // Arduino analog 4
int LED3PIN=17; // Arduino analog 3
int LED4PIN=16; // Arduino analog 2
int current_brightnes=0;
int current_brightness=0;
char ambient_light_to_brightness[1024];
@ -170,6 +174,7 @@ static struct pt blink_thread_pt, lightsensor_thread_pt, buttons_thread_pt,
void print_DS1302time()
{
#if (PRINT_DEBUG_LEVEL <= 0)
/* Get the current time and date from the chip */
Time t = rtc.time();
@ -179,6 +184,7 @@ void print_DS1302time()
/* Print the formatted string to serial so we can see the time */
Serial.println(buf);
#endif
}
@ -216,20 +222,24 @@ void setup()
// could be corrupt and should be ignored --> discard this sample now
analogRead(LIGHTSENSOR_INPUTPIN);
current_brightnes=MAXBRIGHTNESS;
current_brightness=MAXBRIGHTNESS;
//Serial.begin(9600); // setup the serial port to 9600 baud
Serial.begin(115200); // setup the serial port to 115200 baud
inputBuffer.reserve(256);
inputBuffer.reserve(INPUTBUFFER_LENGTH);
SWversion(); // Display the version number of the software
// test whether the DS1302 is there
#if (PRINT_DEBUG_LEVEL <= 0)
Serial.print("D: Verifying DS1302 ");
#endif
// 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
DS1302Present=1;
#if (PRINT_DEBUG_LEVEL <= 0)
Serial.println("present - Valid Signature");
#endif
rtc.write_protect(false);
rtc.halt(false);
}
@ -249,10 +259,16 @@ void setup()
rtc.time(t);
// set the DS1302 present flag
DS1302Present=1;
#if (PRINT_DEBUG_LEVEL <= 0)
Serial.println("present - new chip initialised.");
#endif
}
else
{
#if (PRINT_DEBUG_LEVEL <= 0)
Serial.println("absent");
#endif
}
}
// compute ambient light level to brightness level mapping
@ -292,7 +308,9 @@ void setup()
OldHardware=0;
if ( digitalRead(FWD_BUTTON_PIN)==0 && digitalRead(REV_BUTTON_PIN)==0)
{
#if (PRINT_DEBUG_LEVEL <= 0)
Serial.println("D: Detected Old Hardware");
#endif
OldHardware=1; // we have old hardware
BTNActive = 1; // True = active for old hardware
digitalWrite(FWD_BUTTON_PIN,LOW); // Turn off weak pullups
@ -301,7 +319,9 @@ void setup()
}
else
{
#if (PRINT_DEBUG_LEVEL <= 0)
Serial.println("D: Detected New Hardware");
#endif
OldHardware=0; // we have new hardware
BTNActive = 0; // True = active for old hardware
}
@ -320,7 +340,9 @@ void setup()
RESET_TIMER2;
sei();
Serial.println("D: Finished setting up Timer2 Interrupt");
#if (PRINT_DEBUG_LEVEL <= 0)
Serial.println("D: Finished setting up Timer2 Interrupt");
#endif
@ -338,7 +360,7 @@ void setup()
hour=t.hr;
}
displaytime(); // display the current time
displaytime(); // display the current time
}
void serialEvent()
@ -346,11 +368,19 @@ void serialEvent()
while (Serial.available()) {
// get the new byte
char inChar = (char) Serial.read();
inputBuffer += inChar;
// 0x0D == enter
if (inChar == 0x0D)
inputBuffer += inChar;
// 0x0A == line feet, 0x0D == carriage return
if ((inChar == 0x0A) || (inChar == 0x0D))
inputBufferComplete = true;
else
{
if (inputBuffer.length() >= INPUTBUFFER_LENGTH)
{
Serial.print("E: input buffer overflow; resetting buffer");
inputBuffer = "";
}
}
}
}
@ -359,7 +389,8 @@ void serialEvent()
ISR(TIMER2_OVF_vect) {
RESET_TIMER2;
if (timercount-- <= current_brightnes){
if (timercount-- <= current_brightness)
{
// Now we write the actual values to the hardware
shiftOut(LEDDataPin, LEDClockPin, MSBFIRST, Display3);
shiftOut(LEDDataPin, LEDClockPin, MSBFIRST, Display2);
@ -373,7 +404,7 @@ ISR(TIMER2_OVF_vect) {
digitalWrite(LED4PIN,Led4);
}
else
{
{
shiftOut(LEDDataPin, LEDClockPin, MSBFIRST, 0);
shiftOut(LEDDataPin, LEDClockPin, MSBFIRST, 0);
shiftOut(LEDDataPin, LEDClockPin, MSBFIRST, 0);
@ -385,9 +416,9 @@ ISR(TIMER2_OVF_vect) {
digitalWrite(LED3PIN,0);
digitalWrite(LED4PIN,0);
}
if (timercount==0)
timercount=N_PWM_STEPS-1;
}
if (timercount == 0)
timercount = N_PWM_STEPS - 1;
}
@ -424,11 +455,14 @@ void incrementtime(void){
}
void SWversion(void) {
void SWversion(void)
{
#if (PRINT_DEBUG_LEVEL <= 0)
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");
#endif
}
void process_buttons(unsigned char button_status)
@ -462,8 +496,11 @@ void process_buttons(unsigned char button_status)
// if so - start a self test till both buttons are held
// down again.
//if ( digitalRead(FWD_BUTTON_PIN)==BTNActive && digitalRead(REV_BUTTON_PIN)==BTNActive)
if (selftestmode) {
if (selftestmode)
{
#if (PRINT_DEBUG_LEVEL <= 0)
Serial.println("D: Selftest Mode TRUE");
#endif
for(int i=0; i<100; i++)
{
Display1=255; Display2=255; Display3=255; delay(101-i);
@ -479,7 +516,9 @@ void process_buttons(unsigned char button_status)
// the forward button is down
// and it has been more than one second since we
// last looked
#if (PRINT_DEBUG_LEVEL <= 0)
Serial.println("D: Forward Button Down");
#endif
incrementtime();
second++; // Increment the second counter to ensure that the name
// flash doesnt happen when setting time
@ -494,7 +533,9 @@ void process_buttons(unsigned char button_status)
if (revButtonPressed)
{
#if (PRINT_DEBUG_LEVEL <= 0)
Serial.println("D: Backwards Button Down");
#endif
minute--;
minute--;
second=0; // decrement the minute counter
@ -542,7 +583,9 @@ static int uart_thread(struct pt *pt)
PT_WAIT_UNTIL(pt, inputBufferComplete == true);
length = inputBuffer.length();
if (length == 0)
{
Serial.println("E: received 0 characters");
}
else
{
cmd = inputBuffer.charAt(0);
@ -556,10 +599,12 @@ static int uart_thread(struct pt *pt)
case 'S':
// process set request
if (length < 4)
Serial.println("D: too few arguments");
Serial.println("E: too few arguments");
else
{
#if (PRINT_DEBUG_LEVEL <= 0)
Serial.println("D: set request");
#endif
arg1 = inputBuffer.charAt(1);
arg2 = inputBuffer.charAt(2);
arg3 = inputBuffer.charAt(3);
@ -608,7 +653,7 @@ static int uart_thread(struct pt *pt)
case 'G':
// process get request
if (length < 2)
Serial.println("D: too few arguments");
Serial.println("E: too few arguments");
else
{
arg1 = inputBuffer.charAt(1);
@ -734,23 +779,23 @@ static int lightsensor_thread(struct pt *pt)
/*
// linear method
if (lightlevel_avg <= LIGHTSENSOR_BOTTOM)
current_brightnes = MINBRIGHTNESS;
current_brightness = MINBRIGHTNESS;
else if (lightlevel_avg >= LIGHTSENSOR_TOP)
current_brightnes = MAXBRIGHTNESS;
current_brightness = MAXBRIGHTNESS;
else
{
current_brightnes = (brightness_per_unit_light * (lightlevel_avg -
current_brightness = (brightness_per_unit_light * (lightlevel_avg -
LIGHTSENSOR_BOTTOM)) / (1 << LIGHTSENSOR_SCALE) + MINBRIGHTNESS;
} */
current_brightnes = ambient_light_to_brightness[lightlevel_avg];
current_brightness = ambient_light_to_brightness[lightlevel_avg];
/*Serial.print("D: lightsensor: ");
Serial.print(lightlevel_sample);
Serial.print(", avg: ");
Serial.print(lightlevel_avg);
Serial.print(", brightness: ");
Serial.println(current_brightnes);*/
Serial.println(current_brightness);*/
}
PT_END(pt);
@ -768,7 +813,9 @@ static int blink_thread(struct pt *pt)
PT_WAIT_UNTIL(pt, millisWillOverflow ? (millis() - msTick > 999) :
((millis() < 4294967295 - 998) && (millis() - msTick > 999)));
msTick=millis();
#if (PRINT_DEBUG_LEVEL <= 1)
Serial.println("D: blink thread is alive");
#endif
// Flash the onboard Pin13 Led so we know something is hapening!
digitalWrite(13,HIGH);
@ -822,12 +869,20 @@ static int wordclock_thread(struct pt *pt)
{
incrementtime();
displaytime();
#if (PRINT_DEBUG_LEVEL <= 1)
Serial.print("D: time: ");
if (hour < 10)
Serial.print(0, DEC);
Serial.print(hour, DEC);
Serial.print(":");
if (minute < 10)
Serial.print(0, DEC);
Serial.print(minute, DEC);
Serial.print(":");
if (second < 10)
Serial.print(0, DEC);
Serial.println(second, DEC);
#endif
}
if (DS1302Present==1) {
@ -841,9 +896,9 @@ static int wordclock_thread(struct pt *pt)
#if (USELIGHTSENSOR == 0)
// set the brightnes level based on the current hour - night=7pm - 6.59am
if ((hour < DAYLIGHTHOUR) | (hour >= NIGHTLIGHTHOUR))
current_brightnes=MINBRIGHTNESS;
current_brightness=MINBRIGHTNESS;
else
current_brightnes=MAXBRIGHTNESS;
current_brightness=MAXBRIGHTNESS;
#endif
PT_END(pt);