#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
+#include <util/delay.h>
+#include <stdlib.h> // for NULL
#include "lights.h"
-static uint16_t button_start[N_BUTTONS];
-static unsigned char button_pressed[N_BUTTONS];
+#define WAKEUP_LIMIT 5 // times 100 ms
+#define SHORT_PRESS_MIN 2 // in jiffies (16 Hz ticks)
+#define SHORT_PRESS_MAX 5
+#define LONG_PRESS_MIN 10
+#define SETUP_TIMEOUT 255 // 15 seconds in jiffies
+// #define USER_PARAMS_WRAPPING 1
-static unsigned char sleep_after_release;
+static uint16_t button_start[3];
+union {
+ unsigned char all;
+ struct {
+ unsigned char btn1 : 1;
+ unsigned char btn2 : 1;
+ unsigned char brake : 1;
+ unsigned char brake_working : 1;
+ unsigned char brake_reported : 1;
+ };
+} button_state, prev_state;
-void init_buttons()
+/*
+ * How does the user_params setup mode work:
+ *
+ * There is an array of user-settable parameters user_params[N_USER_PARAMS],
+ * each of which has its own maximum value (user_params_max[N_USER_PARAMS].
+ * These parameters can be modified in the following way:
+ *
+ * Long press of button 1 enters the setup mode.
+ * In the setup mode, status and illumination LEDs both blink in inversion
+ * number mode.
+ * The # of blinks of the illumination LED shows which user_param is
+ * being modified now,
+ * and the # of blinks of the status LED shows the value of this particular
+ * user_param.
+ *
+ * Long press of button 1 selects the next user_param,
+ * short press of button 0 decrements this particular user_param value,
+ * short press of button 1 increments this particular user_param value.
+ *
+ * If USER_PARAMS_WRAPPING is #defined, decrementing the zero leads to
+ * the maximum value - 1, and incrementing the maximum value - 1 leads
+ * to zero.
+ *
+ * After the SETUP_TIMEOUT with no button press, the system leaves the setup
+ * mode.
+ */
+static unsigned char user_params[N_USER_PARAMS] = { 0, 0, 0 };
+static unsigned char user_params_max[N_USER_PARAMS] = {
+ N_AMBIENT_ZONES+1, 2, 2
+};
+
+static unsigned char user_params_state = 0;
+ /*
+ * Here 0 means "no setup currently in progress",
+ * 1 .. N_USER_PARAMS means "now short presses increase or decrease
+ * the value of user_params[user_params_state-1]"
+ */
+
+static unsigned char user_params_timeout;
+
+static void inline set_status_led(unsigned char n, pattern_t *pattern)
{
- DDRB &= ~(_BV(PB6) | _BV(PB0));
- PORTB |= _BV(PB6) | _BV(PB0);
+ led_set_pattern(n + 1, pattern);
+}
- sleep_after_release = 0;
- // log_byte(PORTB);
+unsigned char buttons_setup_in_progress()
+{
+ if (user_params_state) // setup in progress ...
+ return 1;
+ return 0;
}
-static void inline long_press(unsigned char n)
+pattern_t *buttons_setup_status1_pattern_select()
{
- sleep_after_release = 1;
- gpio_set(1, 1);
+ if (user_params_state) // Setup in progress
+ return number_pattern(user_params_state, 1);
+ else
+ return NULL;
}
-static void do_sleep()
+pattern_t *buttons_setup_status0_pattern_select()
{
- // led_set_status(2);
- // MCUCR |= _BV(ISC00); // any edge generates IRQ
- log_byte(0xb0);
- log_flush();
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- PORTA = 0;
- PORTB = 0;
- DDRA = 0;
- DDRB = 0;
- GIMSK |= _BV(INT0); // enable INT0
- sleep_enable();
- sei();
- sleep_cpu();
- sleep_disable();
- cli();
- GIMSK &= ~_BV(INT0); // disable INT0
- hw_setup();
+ if (user_params_state) // Setup in progress
+ return number_pattern(
+ 1 + user_params[user_params_state-1],
+ 1
+ );
+ else
+ return NULL;
}
-static void inline short_press(unsigned char n)
+unsigned char get_user_param(unsigned char param)
{
- led_set_status(1);
+ if (param < N_USER_PARAMS)
+ return user_params[param];
+ return 0; // FIXME: internal error?
}
-void timer_check_buttons()
+static inline void short_press(unsigned char button)
{
- unsigned char pinb = PINB;
- unsigned char i;
- unsigned char port_states[N_BUTTONS] = {
- pinb & _BV(PB6),
- pinb & _BV(PB0),
- };
- for (i = 0; i < N_BUTTONS; i++) {
- if (!port_states[i]) { // is pressed
- if (button_pressed[i] == 0) {
- // begin of button press
- button_pressed[i] = 1;
- button_start[i] = jiffies;
-#if 0
- // log_byte(0xC0);
- // log_word(jiffies);
+ unsigned char param;
+
+ if (user_params_state == 0) {
+ if (button == 0)
+ toggle_dim_mode();
+ else
+ set_panic_mode();
+ return;
+ }
+
+ param = user_params_state-1;
+
+ if (button == 0) {
+ if (user_params[param])
+ --user_params[param];
+#if SETUP_VALUES_WRAPPING
+ else
+ user_params[param] = user_params_max[param]-1;
#endif
- } else if (button_pressed[i] == 1) {
- // been already pressed
- uint16_t duration = jiffies - button_start[i];
- if (duration > 80) {
- // long button press
- button_pressed[i] = 2;
- long_press(i);
- }
- }
- } else { // is not pressed
- if (button_pressed[i]) { // just depressed
- uint16_t duration = jiffies - button_start[i];
-#if 1
- log_byte(0xC1);
- log_word(duration);
- log_flush();
+ } else {
+ ++user_params[param];
+
+ if (user_params[param] >= user_params_max[param])
+#if SETUP_VALUES_WRAPPING
+ user_params[param] = 0;
+#else
+ user_params[param] = user_params_max[param]-1;
#endif
- button_pressed[i] = 0;
- if (duration > 6 && duration < 30)
- short_press(i);
- if (sleep_after_release)
- do_sleep();
+ }
+ // FIXME: notify somebody about user_params change?
+
+ set_status_led(1, buttons_setup_status1_pattern_select());
+
+ user_params_timeout = SETUP_TIMEOUT;
+}
+
+static inline void long_press(unsigned char button)
+{
+ if (button == 0) {
+ power_down(0);
+ return;
+ }
+
+ // button 1 - cycle through states
+ user_params_state++;
+
+ if (user_params_state > N_USER_PARAMS)
+ user_params_state = 1;
+
+ set_status_led(0, buttons_setup_status0_pattern_select());
+ set_status_led(1, buttons_setup_status1_pattern_select());
+ user_params_timeout = SETUP_TIMEOUT;
+}
+
+void init_buttons()
+{
+ DDRA &= ~_BV(PA4); // set as input
+ DDRA |= _BV(PA3); // output
+ PORTA &= ~(_BV(PA3) | _BV(PA4)); // PA4 disable internal pull-up
+ // PA3 set to zero
+
+ GIMSK &= ~(_BV(PCIE0) | _BV(PCIE1)); // disable pin-change IRQs
+ PCMSK0 = 0; // disable pin-change IRQs on all pins of port A
+ PCMSK1 = 0; // disable pin-change IRQs on all pins of port B
+
+ button_start[0] = 0;
+ button_start[1] = 0;
+ button_start[2] = 0;
+
+ prev_state.all = 0;
+ button_state.all = 0;
+ user_params_state = 0;
+}
+
+void susp_buttons()
+{
+ DDRA &= ~(_BV(PA3) | _BV(PA4)); // set as input
+ PORTA |= _BV(PA4); // enable internal pull-up
+ PORTA &= ~_BV(PA3); // disable pull-up
+
+ GIMSK &= ~_BV(PCIE0);
+ GIMSK |= _BV(PCIE1);
+
+ PCMSK0 = _BV(PCINT4);
+ // disable pin-change IRQs on all pins except PA4
+ PCMSK1 = 0; // disable pin-change IRQs on all pins of port B
+}
+
+static void handle_button(unsigned char button, unsigned char cur,
+ unsigned char prev)
+{
+ if (cur && !prev) { // --- just pressed ---
+ button_start[button] = jiffies;
+ if (!user_params_state)
+ set_status_led(button, NULL);
+
+ } else if (cur && prev) { // --- is still pressed ---
+ uint16_t duration = jiffies - button_start[button];
+
+ if (duration >= LONG_PRESS_MIN) {
+ set_status_led(button, on_pattern);
+ // acknowledge long press
+ }
+ } else if (!cur && prev) { // --- just released ---
+ uint16_t duration = jiffies - button_start[button];
+
+ if (duration >= SHORT_PRESS_MIN && duration < SHORT_PRESS_MAX) {
+ short_press(button);
+ } else if (duration > LONG_PRESS_MIN) {
+ set_status_led(button, NULL);
+ long_press(button);
+ }
+ // ignore other button-press durations
+ }
+}
+
+static void handle_brake(unsigned char cur, unsigned char prev)
+{
+ uint16_t duration;
+
+ if (cur && !prev) { // --- just pressed ---
+ button_start[2] = jiffies;
+ return;
+ } else if (!cur && prev) { // --- just released ---
+ button_start[2] = jiffies;
+ return;
+ }
+ // --- no change ---
+ duration = jiffies - button_start[2];
+
+ if (duration <= 3)
+ return;
+
+ if (cur) {
+ if (button_state.brake_working) {
+ static unsigned int brake_time;
+ if (button_state.brake_reported) {
+ if (brake_time) {
+ brake_time--;
+ } else {
+ brake_off();
+ button_state.brake_working = 0;
+ button_state.brake_reported = 0;
+ }
+ } else {
+ button_state.brake_reported = 1;
+ brake_on();
+ brake_time = 255; // avoid longer than ~16s
}
}
+ } else {
+ button_state.brake_working = 1;
+ if (button_state.brake_reported) {
+ button_state.brake_reported = 0;
+ brake_off();
+ }
+ }
+
+ button_start[2] = jiffies - 7; // avoid overflow
+}
+
+void timer_check_buttons()
+{
+ handle_button(0, button_state.btn1, prev_state.btn1);
+
+ // when button 1 is on, all other states are unreadable
+ if (button_state.btn1) {
+ prev_state.btn1 = button_state.btn1;
+ } else {
+ handle_button(1, button_state.btn2, prev_state.btn2);
+ handle_brake(button_state.brake, prev_state.brake);
+ prev_state.all = button_state.all;
+ }
+
+ if (user_params_timeout)
+ --user_params_timeout;
+
+ if (user_params_state && !user_params_timeout) {
+ user_params_state = 0;
+ set_status_led(0, buttons_setup_status0_pattern_select());
+ set_status_led(1, buttons_setup_status1_pattern_select());
}
}
-ISR(INT0_vect)
+unsigned char buttons_wait_for_release()
{
- hw_setup();
- log_byte(0xbb);
+ uint16_t wake_count = 0;
+ unsigned char pin;
+
+ do {
+ if (wake_count++ > WAKEUP_LIMIT)
+ gpio_set(0, 1); // inform the user
+
+ _delay_ms(100);
+
+ pin = PINA & _BV(PA4);
+ } while (!(pin & _BV(PA4)));
+
+ gpio_set(0, 0);
+
+ return wake_count > WAKEUP_LIMIT;
+}
+
+void button_adc(uint16_t adcval)
+{
+#if 0
+ static unsigned char count=250;
+#endif
+ PORTA &= ~_BV(PA3); // disable +5V to the hall probe
+ // adc.c will re-enable it again
+
+ button_state.btn1 = 0;
+ button_state.btn2 = 0;
+ button_state.brake = 0;
+
+ if (adcval == 0x3FF) {
+ button_state.brake = 1;
+ } else if (adcval > 0x240) {
+ button_state.brake = 1;
+ button_state.btn2 = 1;
+ } else if (adcval > 0x180) {
+ // nothing
+ } else if (adcval > 0xc0) {
+ button_state.btn2 = 1;
+ } else {
+ button_state.btn1 = 1;
+ }
+
+#if 0
+ if (--count)
+ return;
+
+ count = 250;
+ log_word(adcval);
log_flush();
+#endif
+}
+
+ISR(PCINT_vect)
+{
+ // empty - let it wake us from sleep, but do nothing else
}