buttonry.c File Reference

#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include <stdio.h>
#include "buttonry.h"
#include "util.h"
#include "rtc.h"
Include dependency graph for buttonry.c:

Defines

#define set_blinkhandler(x)

Enumerations

enum  _setstates {
  SET_NONE = 0, SET_HOUR, SET_MINUTE, SET_SEC0,
  SET_YEAR, SET_MONTH, SET_DAY
}

Functions

void debounce (uint8_t port, uint8_t *state, void(*handler)(uint8_t))
 Catch button press and release moments, call handler.
void buttons_init ()
 Initialize ports and variables.
void button1_handler (uint8_t on)
 Handler for button 1: "SET".
void button2_handler (uint8_t on)
 Handler for button 2, "+".
uint8_t is_setting ()
void buttonry_tick (uint8_t b1, uint8_t b2)

Variables

uint8_t reset_seconds = 0
RTC_TIME rtc_time

Define Documentation

#define set_blinkhandler (  ) 
Value:
{ cli();\
                              if (blinkhandler == NULL) {\
                                blinkhandler = x;\
                                skip = 1;\
                              }\
                              sei();\
                            }

Enumeration Type Documentation

enum _setstates
Enumerator:
SET_NONE 
SET_HOUR 
SET_MINUTE 
SET_SEC0 
SET_YEAR 
SET_MONTH 
SET_DAY 

00010                 {
00011     SET_NONE = 0,
00012     SET_HOUR,
00013     SET_MINUTE,
00014     SET_SEC0,
00015     SET_YEAR,
00016     SET_MONTH,
00017     SET_DAY,
00018 };


Function Documentation

void button1_handler ( uint8_t  on  ) 

Handler for button 1: "SET".

00056                                  {
00057     static uint8_t skip = 0;
00058     
00059     if (on) {
00060         if (skip > 0) {
00061             skip--;
00062             return;
00063         }
00064         
00065         switch (set_state) {
00066             case SET_NONE:
00067                 duty_set(duty_get() % 4 + 1);
00068                 skip = 1;
00069                 break;
00070                 
00071             case SET_HOUR:
00072                 set_blinkhandler(button1_handler);
00073                 
00074                 rtc_time.hour = bcd_increment(rtc_time.hour);
00075 
00076                 if (rtc_time.hour == 0x24) rtc_time.hour = 0;
00077                 
00078                 rtc_xhour(rtc_time.hour);
00079                 
00080                 fadeto((get_display_value() & 0377) | (rtc_time.hour << 8));
00081                 break;
00082                 
00083             case SET_MINUTE:
00084                 set_blinkhandler(button1_handler);
00085                 
00086                 rtc_time.minute = bcd_increment(rtc_time.minute);
00087                 if (rtc_time.minute == 0x60) rtc_time.minute = 0;
00088                 rtc_xminute(rtc_time.minute);
00089                 rtc_xseconds(0);
00090                 reset_seconds = 1;
00091                 
00092                 fadeto((get_display_value() & 0xff00) | rtc_time.minute);
00093                 break;
00094                 
00095             case SET_YEAR:
00096                 set_blinkhandler(button1_handler);
00097                 
00098                 rtc_time.year = bcd_increment(rtc_time.year);
00099                 rtc_xyear(rtc_time.year);
00100 
00101                 rtc_xdow(day_of_week(frombcd(rtc_time.year), frombcd(rtc_time.month), frombcd(rtc_time.day)));
00102 
00103                 fadeto(0x2000 + rtc_time.year);
00104                 break;     
00105                        
00106             case SET_MONTH:
00107                 set_blinkhandler(button1_handler);
00108 
00109                 rtc_time.month = bcd_increment(rtc_time.month);
00110                 if (rtc_time.month == 0x13) rtc_time.month = 1;                
00111                 rtc_xmonth(rtc_time.month);
00112 
00113                 rtc_xdow(day_of_week(frombcd(rtc_time.year), frombcd(rtc_time.month), frombcd(rtc_time.day)));
00114                 
00115                 fadeto(maketime(rtc_time.day, rtc_time.month));
00116                 break;
00117             
00118             case SET_DAY:
00119                 set_blinkhandler(button1_handler);
00120                 
00121                 rtc_time.day = bcd_increment(rtc_time.day);
00122                 if (rtc_time.day > days_in_month_bcd(rtc_time.year, rtc_time.month)) {
00123                     rtc_time.day = 1;
00124                 }
00125                 rtc_xday(rtc_time.day);
00126                 
00127                 rtc_xdow(day_of_week(frombcd(rtc_time.year), frombcd(rtc_time.month), frombcd(rtc_time.day)));
00128                 
00129                 fadeto(maketime(rtc_time.day, rtc_time.month));
00130                 break;
00131         }
00132     } else {
00133         cli(); blinkhandler = NULL; skip = 0; sei();
00134     }
00135 }

Here is the call graph for this function:

void button2_handler ( uint8_t  on  ) 

Handler for button 2, "+".

00138                                  {
00139     if (on) {
00140         switch (set_state) {
00141             case SET_NONE:
00142                 set_state = SET_HOUR;
00143                 set_blinkmode(BLINK_HH);
00144                 rtc_time.hour = rtc_xhour(-1);
00145                 rtc_time.minute = rtc_xminute(-1); 
00146                 fadeto(maketime(rtc_time.hour, rtc_time.minute));
00147                 break;
00148             case SET_HOUR:
00149                 set_state = SET_MINUTE;
00150                 set_blinkmode(BLINK_MM);
00151                 break;
00152             case SET_MINUTE:
00153                 set_state = SET_YEAR;
00154                 set_blinkmode(BLINK_ALL);
00155                 rtc_time.year = rtc_xyear(-1);
00156                 fadeto(0x2000 + rtc_time.year);
00157 
00158                 if (reset_seconds) {
00159                     rtc_xseconds(0);
00160                     reset_seconds = 0;
00161                 }
00162                 break;
00163             case SET_YEAR:
00164                 set_state = SET_MONTH;
00165                 set_blinkmode(BLINK_MM);
00166                 
00167                 rtc_time.month = rtc_xmonth(-1);
00168                 if (rtc_time.month == 0) rtc_time.month = 1;
00169                 
00170                 rtc_time.day   = rtc_xday(-1);
00171                 if (rtc_time.day == 0) rtc_time.day = 1;
00172                 
00173                 fadeto(maketime(rtc_time.day, rtc_time.month));
00174                 break;
00175             case SET_MONTH:
00176                 set_state = SET_DAY;
00177                 set_blinkmode(BLINK_HH);
00178                 break;
00179             case SET_DAY:
00180             default:
00181                 set_state = SET_NONE;
00182                 set_blinkmode(BLINK_NONE);
00183                 break;
00184         }
00185     } else {
00186         cli(); blinkhandler = NULL; sei();
00187     }
00188 }

Here is the call graph for this function:

void buttonry_tick ( uint8_t  b1,
uint8_t  b2 
)

00194                                            {
00195     debounce(b1, &button1_state, button1_handler);
00196     debounce(b2, &button2_state, button2_handler);
00197 }

Here is the call graph for this function:

void buttons_init (  ) 

Initialize ports and variables.

00040                     {
00041     DDRD &= ~BV2(3,4);
00042     PORTD |= BV2(3,4);
00043     button1_state = button2_state = 0;
00044 }

void debounce ( uint8_t  port,
uint8_t *  state,
void(*)(uint8_t)  handler 
)

Catch button press and release moments, call handler.

00033                                                                       {
00034     if (*state && !port) handler(1);
00035     if (!*state && port) handler(0);
00036     *state = port;
00037 }

uint8_t is_setting (  ) 

00190                      {
00191     return set_state != SET_NONE;
00192 }


Variable Documentation

uint8_t reset_seconds = 0

Generated on Sat Dec 12 00:45:14 2009 for Patashnik by  doxygen 1.6.1