Split up wordclock thread into multiple threads
wordclock thread is now reduced to only taking care of displaying the time; blink thread and lightsensor thread take care of blinking led and reading lightsensor (and calculating new brightness value).
This commit is contained in:
parent
f74e4aedca
commit
c5561f8aaf
|
@ -102,7 +102,6 @@ unsigned int brightness_per_unit_light = ((1 << LIGHTSENSOR_SCALE) * (MAXBRIGHTN
|
|||
MINBRIGHTNESS)) / (LIGHTSENSOR_TOP - LIGHTSENSOR_BOTTOM);
|
||||
|
||||
int hour=12, minute=0, second=0;
|
||||
static unsigned long msTick =0; // the number of Millisecond Ticks since we last
|
||||
// incremented the second counter
|
||||
int count;
|
||||
int selftestmode; // 1 = in self test - flash display
|
||||
|
@ -159,7 +158,7 @@ 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, buttons_thread_pt, wordclock_thread_pt;
|
||||
static struct pt blink_thread_pt, lightsensor_thread_pt, buttons_thread_pt, wordclock_thread_pt;
|
||||
|
||||
void print_DS1302time()
|
||||
{
|
||||
|
@ -310,7 +309,6 @@ void setup()
|
|||
Serial.println("Finished setting up Timer2 Interrupt");
|
||||
|
||||
|
||||
msTick=millis(); // Initialise the msTick counter
|
||||
|
||||
#if (SKIPSELFTEST != 1)
|
||||
selftest(); // validate the hardware for the user
|
||||
|
@ -414,10 +412,6 @@ void process_buttons(unsigned char button_status)
|
|||
static unsigned long button_delay = BUTTON_DELAY_LONG;
|
||||
unsigned long millisNow = 0, new_button_delay;
|
||||
|
||||
// test to see if both buttons are being held down
|
||||
// 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 (BTNActive)
|
||||
{
|
||||
if ((button_status & FWD_BUTTON_MASK) > 0)
|
||||
|
@ -438,6 +432,10 @@ void process_buttons(unsigned char button_status)
|
|||
selftestmode != selftestmode;
|
||||
}
|
||||
|
||||
// test to see if both buttons are being held down
|
||||
// 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) {
|
||||
Serial.println("Selftest Mode TRUE");
|
||||
for(int i=0; i<100; i++)
|
||||
|
@ -468,7 +466,6 @@ void process_buttons(unsigned char button_status)
|
|||
displaytime();
|
||||
}
|
||||
|
||||
//if (digitalRead(REV_BUTTON_PIN)==BTNActive )
|
||||
if (revButtonPressed)
|
||||
{
|
||||
Serial.println("Backwards Button Down");
|
||||
|
@ -530,72 +527,22 @@ static int buttons_thread(struct pt *pt)
|
|||
PT_END(pt);
|
||||
}
|
||||
|
||||
static int blink_thread(struct pt *pt)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static int wordclock_thread(struct pt *pt)
|
||||
static int lightsensor_thread(struct pt *pt)
|
||||
{
|
||||
static unsigned long msTick =0; // the number of Millisecond Ticks since we last
|
||||
static unsigned long int lightlevel_avg;
|
||||
unsigned long int lightlevel_sample;
|
||||
static char n_lightlevel_samples = 0;
|
||||
static unsigned int counter = 0;
|
||||
static char millisWillOverflow = 0;
|
||||
|
||||
#ifdef TESTMODE
|
||||
second=59;
|
||||
#endif
|
||||
|
||||
//Serial.println("Loop Started");
|
||||
static char n_lightlevel_samples = 0;
|
||||
|
||||
PT_BEGIN(pt);
|
||||
|
||||
// heart of the timer - keep looking at the millisecond timer on the Arduino
|
||||
// and increment the seconds counter every 1000 ms
|
||||
|
||||
while(1)
|
||||
{
|
||||
PT_WAIT_UNTIL(pt, millisWillOverflow ? (millis() - msTick > 999) :
|
||||
((millis() < 4294967295 - 998) && (millis() - msTick > 999)));
|
||||
PT_WAIT_UNTIL(pt, millisWillOverflow ? (millis() - msTick > 99) :
|
||||
((millis() < 4294967295 - 98) && (millis() - msTick > 99)));
|
||||
msTick=millis();
|
||||
if (msTick > 4294967295 - 998)
|
||||
{
|
||||
// millis() will overflow --> adjust msTick
|
||||
msTick = 999 - (4294967295 - msTick);
|
||||
millisWillOverflow = 1;
|
||||
}
|
||||
else
|
||||
millisWillOverflow = 0;
|
||||
|
||||
second++;
|
||||
// Flash the onboard Pin13 Led so we know something is hapening!
|
||||
digitalWrite(13,HIGH);
|
||||
delay(50);
|
||||
digitalWrite(13,LOW);
|
||||
delay(50);
|
||||
digitalWrite(13,HIGH);
|
||||
delay(50);
|
||||
digitalWrite(13,LOW);
|
||||
|
||||
//test to see if we need to increment the time counters
|
||||
if (second==60)
|
||||
{
|
||||
incrementtime();
|
||||
displaytime();
|
||||
}
|
||||
|
||||
if (DS1302Present==1) {
|
||||
// Get the current time and date from the chip
|
||||
Time t = rtc.time();
|
||||
second=t.sec;
|
||||
minute=t.min;
|
||||
hour=t.hr;
|
||||
}
|
||||
|
||||
// set the brightnes level based on the current hour - night=7pm - 6.59am
|
||||
//
|
||||
#if (USELIGHTSENSOR == 1)
|
||||
lightlevel_sample = analogRead(LIGHTSENSOR_INPUTPIN);
|
||||
|
||||
// update average
|
||||
|
@ -639,7 +586,95 @@ static int wordclock_thread(struct pt *pt)
|
|||
Serial.print(lightlevel_avg);
|
||||
Serial.print(", brightness: ");
|
||||
Serial.println(current_brightnes);
|
||||
#else
|
||||
}
|
||||
|
||||
PT_END(pt);
|
||||
}
|
||||
|
||||
static int blink_thread(struct pt *pt)
|
||||
{
|
||||
static unsigned long msTick =0; // the number of Millisecond Ticks since we last
|
||||
static char millisWillOverflow = 0;
|
||||
|
||||
PT_BEGIN(pt);
|
||||
|
||||
while(1)
|
||||
{
|
||||
PT_WAIT_UNTIL(pt, millisWillOverflow ? (millis() - msTick > 999) :
|
||||
((millis() < 4294967295 - 998) && (millis() - msTick > 999)));
|
||||
msTick=millis();
|
||||
Serial.println(":)");
|
||||
|
||||
// Flash the onboard Pin13 Led so we know something is hapening!
|
||||
digitalWrite(13,HIGH);
|
||||
delay(50);
|
||||
digitalWrite(13,LOW);
|
||||
delay(50);
|
||||
digitalWrite(13,HIGH);
|
||||
delay(50);
|
||||
digitalWrite(13,LOW);
|
||||
}
|
||||
|
||||
PT_END(pt);
|
||||
}
|
||||
|
||||
static int wordclock_thread(struct pt *pt)
|
||||
{
|
||||
static unsigned int counter = 0;
|
||||
static unsigned long msTick =0; // the number of Millisecond Ticks since we last
|
||||
static char millisWillOverflow = 0;
|
||||
|
||||
#ifdef TESTMODE
|
||||
second=59;
|
||||
#endif
|
||||
|
||||
//Serial.println("Loop Started");
|
||||
|
||||
PT_BEGIN(pt);
|
||||
msTick=millis(); // Initialise the msTick counter
|
||||
|
||||
// heart of the timer - keep looking at the millisecond timer on the Arduino
|
||||
// and increment the seconds counter every 1000 ms
|
||||
|
||||
while(1)
|
||||
{
|
||||
PT_WAIT_UNTIL(pt, millisWillOverflow ? (millis() - msTick > 999) :
|
||||
((millis() < 4294967295 - 998) && (millis() - msTick > 999)));
|
||||
msTick=millis();
|
||||
if (msTick > 4294967295 - 998)
|
||||
{
|
||||
// millis() will overflow --> adjust msTick
|
||||
msTick = 999 - (4294967295 - msTick);
|
||||
millisWillOverflow = 1;
|
||||
}
|
||||
else
|
||||
millisWillOverflow = 0;
|
||||
|
||||
second++;
|
||||
|
||||
//test to see if we need to increment the time counters
|
||||
if (second==60)
|
||||
{
|
||||
incrementtime();
|
||||
displaytime();
|
||||
Serial.print("time: ");
|
||||
Serial.print(hour, DEC);
|
||||
Serial.print(":");
|
||||
Serial.print(minute, DEC);
|
||||
Serial.print(":");
|
||||
Serial.println(second, DEC);
|
||||
}
|
||||
|
||||
if (DS1302Present==1) {
|
||||
// Get the current time and date from the chip
|
||||
Time t = rtc.time();
|
||||
second=t.sec;
|
||||
minute=t.min;
|
||||
hour=t.hr;
|
||||
}
|
||||
|
||||
#if (USELIGHTSENSOR == 0)
|
||||
// set the brightnes level based on the current hour - night=7pm - 6.59am
|
||||
if ((hour < DAYLIGHTHOUR) | (hour >= NIGHTLIGHTHOUR))
|
||||
current_brightnes=MINBRIGHTNESS;
|
||||
else
|
||||
|
@ -655,5 +690,9 @@ void loop()
|
|||
// call each thread continuously
|
||||
wordclock_thread(&wordclock_thread_pt);
|
||||
buttons_thread(&buttons_thread_pt);
|
||||
#if (USELIGHTSENSOR == 1)
|
||||
lightsensor_thread(&lightsensor_thread_pt);
|
||||
#endif
|
||||
blink_thread(&blink_thread_pt);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue