Boolean flags moved into one register

Size optimization enabled
This commit is contained in:
Stefan Kalscheuer 2015-09-18 18:09:59 +02:00 committed by Stefan Kalscheuer
parent 2636181f06
commit 0cb9050e57
3 changed files with 53 additions and 55 deletions

View File

@ -36,7 +36,7 @@ AVRDUDE = avrdude
REMOVE = rm -f
# Some C flags
CFLAGS = -Wall -O3
CFLAGS = -Wall -Os
help:
@echo

View File

@ -18,16 +18,16 @@
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <stdbool.h>
#include "main.h"
// variables:
volatile unsigned int time_counter, user_time_counter = 0, sec_counter = 0; // global and universal time counter (ms) and second-counter (for AutoOff)
volatile unsigned int button_1_cup_counter = 0, button_2_cup_counter = 0; // button counter
volatile unsigned char button_power_counter = 0;
volatile unsigned char led = 0; // LED status flags
volatile bool water = false, temperature = false, make_clean = false; // water-, temperature-, clean-flags
volatile unsigned char make_coffee = 0, pump_time = 0; // pump time, clean mode flag
volatile uint16_t ms_counter = 0; // global and universal time counter (ms) and second-counter (for AutoOff)
volatile uint8_t sec_counter = 0;
volatile uint16_t button_1_cup_counter = 0, button_2_cup_counter = 0; // button counter
volatile uint8_t button_power_counter = 0;
volatile uint8_t led = 0; // LED status flags
volatile uint8_t flags = 0; // Flags: * * * * esc clean temperature water
volatile uint8_t make_coffee = 0, pump_time = 0; // pump time, clean mode flag
int main (void)
{
@ -39,8 +39,8 @@ int main (void)
if (sec_counter >= AUTO_OFF_THRESHOLD)
button_power_counter = BUTTON_THRESHOLD; // check for AutoOff Timer (generate OnOff-button push)
water = get_water (); // update water state
temperature = get_temperature (); // update temperature
get_water (); // update water state
get_temperature (); // update temperature
if (button_power_counter >= BUTTON_THRESHOLD) // button "OnOff" pushed:
{
@ -60,7 +60,7 @@ int main (void)
if (button_1_cup_counter >= BUTTON_CLEAN_THR
&& button_2_cup_counter >= BUTTON_CLEAN_THR) // both coffee buttons pushed: clean mode:
{
make_clean = true; // clean flag true
set_bit(flags, FLAG_CLEAN); // clean flag true
led = BLUE; // set blue LED
while (button_1_cup_counter > 0 && button_2_cup_counter > 0)
; // debounce buttons
@ -71,7 +71,7 @@ int main (void)
{
sec_counter = 0; // reset AutoOff counter
if (water && temperature) // machine ready:
if ((flags & 0b00000011) == 3) // machine ready:
{
while (button_1_cup_counter > 0) // check if button is pushed long time
{
@ -94,7 +94,7 @@ int main (void)
{
sec_counter = 0; // reset AutoOff counter
if (water && temperature) // machine ready:
if ((flags & 0b00000011) == 3) // machine ready:
{
while (button_2_cup_counter > 0) // check if button is pushed long time
{
@ -112,15 +112,15 @@ int main (void)
}
}
if (water) // water OK:
if (bit_is_set(flags, FLAG_WATER)) // water OK:
{
if (make_clean) // if clean-flag is set:
if (bit_is_set(flags, FLAG_CLEAN)) // if clean-flag is set:
{
set_bit(TRIAC_BOILER_w, TRIAC_BOILER_pin); // boiler off
bool escape = false; // init escape-flag
while (water && !escape)
{ // pump until water is empty or escape flag is set
clear_bit(flags, FLAG_ESC); // clear escape flag
while (bit_is_set(flags, FLAG_WATER) && bit_is_clear(flags, FLAG_ESC))
{ // pump until water is empty or escape flag is set
unsigned int sense = detect_zero_crossing (); // detect zero crossing
if (sense <= 100)
{
@ -128,18 +128,17 @@ int main (void)
_delay_ms (3);
set_bit(TRIAC_PUMP_w, TRIAC_PUMP_pin);
}
water = get_water (); // update water state
get_water (); // update water state
if (button_power_counter > BUTTON_THRESHOLD)
escape = true; // check for power button counter and set escape flag
set_bit(flags, FLAG_ESC); // check for power button counter and set escape flag
}
make_clean = false; // clear clean flag
clear_bit(flags, FLAG_CLEAN); // clear clean flag
}
else if (temperature) // temperature OK:
else if (bit_is_set(flags, FLAG_TEMPERATURE)) // temperature OK:
{
set_bit(TRIAC_BOILER_w, TRIAC_BOILER_pin); // boiler off
led = GREEN; // set green LED
if (make_coffee > 0) // if coffee flag is set:
@ -160,12 +159,13 @@ int main (void)
else
make_coffee = 0;
user_time_counter = 0; // reset user time counter
bool escape = false; // init escape flag
while (user_time_counter < (pump_time * 1000) && water && !escape)
sec_counter = 0; // reset user time counter
clear_bit(flags, FLAG_ESC); // clear escape flag
while (sec_counter < pump_time && bit_is_set(flags, FLAG_WATER) && bit_is_clear(flags, FLAG_ESC))
{ // loop until pump time is reached or water is empty
if (make_coffee > 2
|| (user_time_counter < 2000 || user_time_counter > 4000))
|| (sec_counter < 2 || sec_counter > 4))
{ // check for preinfusion break
unsigned int sense = detect_zero_crossing (); // detect zero crossing
if (sense <= 100)
@ -176,16 +176,15 @@ int main (void)
}
}
water = get_water (); // update water state
get_water (); // update water state
if (button_power_counter > BUTTON_THRESHOLD)
escape = true; // check for power button counter and set escape flag
set_bit(flags, FLAG_ESC); // check for power button counter and set escape flag
}
set_bit(TRIAC_PUMP_w, TRIAC_PUMP_pin); // pump off
clear_bit(flags, FLAG_CLEAN); // clear clean flag
make_coffee = 0; // clear coffee flag
sec_counter = 0; // reset AutoOff timer
}
}
@ -284,7 +283,7 @@ void power_off ()
asm volatile("sleep"::);
// entrance after wake-up:
time_counter = 0; // reset counter
ms_counter = 0; // reset counter
sec_counter = 0;
cli (); // disable interrupts
clear_bit(GIMSK, INT0); // disable interrupt 0
@ -293,37 +292,35 @@ void power_off ()
}
/* function: get_water()
* return: true water OK
* false not enough water
*
* Checks hall sensor for water state.
*/
bool get_water ()
void get_water ()
{
ADMUX = SENSOR_MAGNET_adc | (1 << ADLAR); // ADLAR
set_bit(ADCSR, ADSC);
loop_until_bit_is_clear (ADCSR, ADSC);
unsigned char sense = ADCH;
if ((water && sense > WATER_LOW) || (!water && sense >= WATER_OK))
return true;
return false;
if ((bit_is_set(flags, FLAG_WATER) && sense > WATER_LOW) || (bit_is_clear(flags, FLAG_WATER) && sense >= WATER_OK))
set_bit(flags, FLAG_WATER);
else
clear_bit(flags, FLAG_WATER);
}
/* function: get_temperature()
* return: true temperature OK
* false temperature too low
*
* Checks NTC sensor for temperature state.
*/
bool get_temperature ()
void get_temperature ()
{
ADMUX = SENSOR_TEMP_adc | (1 << ADLAR); // ADLAR
set_bit(ADCSR, ADSC);
loop_until_bit_is_clear (ADCSR, ADSC);
unsigned char sense = ADCH;
if (sense >= OPERATING_TEMPERATURE)
return true;
return false;
set_bit(flags, FLAG_TEMPERATURE);
else
clear_bit(flags, FLAG_TEMPERATURE);
}
/* function: detect_zero_crossing()
@ -355,20 +352,15 @@ ISR ( INT0_vect)
*/
ISR ( TIMER1_OVF1_vect)
{
if (time_counter < 1000)
time_counter++; // global milliseconds counter and seconds counter (for AutoOff)
if (ms_counter < 1000)
ms_counter++; // global milliseconds counter and seconds counter (for AutoOff)
else
{
time_counter = 0;
ms_counter = 0;
sec_counter++;
}
user_time_counter++; // universal counter (for pump time)
bool leds_blink_on; // status flag for blinking LEDs with 1Hz
if (time_counter < 499)
leds_blink_on = true;
else
leds_blink_on = false;
uint8_t leds_blink_on = (ms_counter < 500); // status flag for blinking LEDs with 1Hz
if (led & (1 << LED_RED_ON) || (led & (1 << LED_RED_BLINK) && leds_blink_on))
set_bit(LED_RED_w, LED_RED_pin);

View File

@ -95,6 +95,7 @@
#define BUTTON_CLEAN_THR 30 // button threshold for cleaning mode (ms)
#define BUTTON_THRESHOLD 100 // button threshold (ms)
#define BUTTON_LONG_THR 1500 // button threshold for long time push (ms)
#define BUTTON_LIMIT 1600
#define RED 0b00000001
#define RED_BLINK 0b00000010
@ -107,9 +108,14 @@
#define VIOLET 0b00010001
#define VIOLET_BLINK 0b00100010
#define FLAG_WATER 0
#define FLAG_TEMPERATURE 1
#define FLAG_CLEAN 2
#define FLAG_ESC 3
// prototypes:
void init (); // initialization
void power_off (); // power off to sleep mode
bool get_water (); // update water state
bool get_temperature (); // update tehmerature state
void get_water (); // update water state
void get_temperature (); // update tehmerature state
unsigned int detect_zero_crossing (); // detect zero crossing