diff --git a/lambda/Makefile b/lambda/Makefile index 5e8337d..5525397 100644 --- a/lambda/Makefile +++ b/lambda/Makefile @@ -25,9 +25,11 @@ ########## (Can override. See bottom of file.) ########## ##########------------------------------------------------------########## -PROGRAMMER_TYPE = avrisp +# PROGRAMMER_TYPE = avrisp +PROGRAMMER_TYPE = avrispmkII # extra arguments to avrdude: baud rate, chip type, -F flag, etc. -PROGRAMMER_ARGS = -b 19200 -P /dev/ttyACM3 +# PROGRAMMER_ARGS = -b 19200 -P /dev/ttyACM3 +PROGRAMMER_ARGS = ##########------------------------------------------------------########## ########## Makefile Magic! ########## diff --git a/lambda/airgate.c b/lambda/airgate.c index e9ffc05..fbd7243 100644 --- a/lambda/airgate.c +++ b/lambda/airgate.c @@ -8,28 +8,27 @@ #include #include #include +#include #include #include "airgate.h" #include "integers.h" -#include "usart.h" - // TODO pins /* Direction */ -static int8_t dir = 0; +volatile static int8_t dir = 0; /* Current position */ -static uint16_t pos = 0; +volatile static uint16_t pos = 0; /* Target position */ -static int16_t target = 0; +volatile static int16_t target = 0; /* Steps remaining */ -static uint16_t steps = 0; +volatile static uint16_t steps = 0; /* Steps done */ -static uint16_t done = 0; +volatile static uint16_t done = 0; /* Acceleration profile ramp */ -static uint16_t ramp = 0; +volatile static uint16_t ramp = 0; /* Speed */ -static uint8_t speed = MIN_SPEED; +volatile static uint8_t speed = MIN_SPEED; /** * Wakes up the driver if sleeping, sets the direction and initial speed and @@ -71,14 +70,14 @@ * the motor. */ static void set(void) { - done = 0; int16_t diff = (target << SCALE) - pos; if (diff != 0) { - target = -1; dir = MAX(-1, MIN(diff, 1)); - steps = abs(diff); - ramp = MIN(MIN_SPEED - MAX_SPEED, steps >> 1); speed = MIN_SPEED; + ATOMIC_BLOCK(ATOMIC_FORCEON) { + steps = abs(diff); + ramp = MIN(MIN_SPEED - MAX_SPEED, steps >> 1); + } start(); } } @@ -87,8 +86,8 @@ if (steps > 0) { PORTB ^= (1 << PB7); pos += dir; - steps--; done++; + steps--; // accelerate within ramp if (done < ramp && speed > MAX_SPEED) speed--; // decelerate within ramp @@ -97,10 +96,9 @@ } else { done = 0; stop(); - if (target != -1) { - // move to target position - set(); - } else if (pos == 0) { + // move to new target position if necessary + set(); + if (pos == 0) { // driver sleep mode // TODO when is it best to do this? PORTC &= ~(1 << PC5); @@ -109,19 +107,21 @@ } void setAirgate(uint8_t const position) { - if (position == target) { - return; - } - target = position; - if (steps > 0) { - // motor busy - decelerate and move to target position when stopped - steps = MIN(ramp, steps); - } else { - // move to target position - set(); + ATOMIC_BLOCK(ATOMIC_FORCEON) { + if (position == target) { + return; + } + target = position; + if (steps > 0) { + // motor busy - decelerate and move to target position when stopped + steps = MIN(ramp, steps); + } else { + // move to target position + set(); + } } } uint8_t getAirgate(void) { - return pos >> SCALE; + return target; } diff --git a/lambda/alert.c b/lambda/alert.c index bc83fc2..87a1cb9 100644 --- a/lambda/alert.c +++ b/lambda/alert.c @@ -9,18 +9,19 @@ */ #include +#include #include "integers.h" #include "alert.h" #include "sensors.h" #include "display.h" #include "pins.h" -uint8_t beepCount = 0; -uint16_t beepLength = 0; -uint8_t oscCount = 0; +volatile uint8_t beepCount = 0; +volatile uint16_t beepLength = 0; +volatile uint8_t oscCount = 0; -static bool alertActive = false; -static bool keepActive = false; +volatile static bool alertActive = false; +volatile static bool keepActive = false; void oscillateBeep(void) { if (beepCount == 0) { @@ -47,7 +48,9 @@ if (TCNT1 >= tone) TCNT1 = 0; oscCount = 0; beepCount = beeps; - beepLength = length; + ATOMIC_BLOCK(ATOMIC_FORCEON) { + beepLength = length; + } } void alert(uint8_t const beeps, uint8_t const length, uint16_t const tone, @@ -56,10 +59,12 @@ OCR1A = tone; if (TCNT1 >= tone) TCNT1 = 0; alertActive = true; + keepActive = keep; oscCount = 0; beepCount = beeps; - beepLength = length; - keepActive = keep; + ATOMIC_BLOCK(ATOMIC_FORCEON) { + beepLength = length; + } displayText(line0, line1); }