ButterflyMP3

button.c

Go to the documentation of this file.
00001 
00021 //mtA
00022 //#include <inavr.h>
00023 //#include "iom169.h"
00024 #include <avr/io.h>
00025 #include <avr/interrupt.h>
00026 //#include <avr/signal.h>//This header file is obsolete.  Use <avr/interrupt.h>.
00027 #include <inttypes.h>
00028 //mtE
00029 
00030 #include "main.h"
00031 #include "types.h"
00032 #include "button.h"
00033 #include "timer0.h"
00034 #include "vs1001.h"
00035 #include "mmc.h"
00036 #include "uart.h"
00037 
00038 extern volatile uint8_t gPowerSaveTimer; 
00039 extern volatile uint16  gPlayTimeSeconds;
00040 extern volatile uint8   gMode;          // mode of player, idle, playing, off, etc
00041 // mt  unsigned char gButtonTimeout = FALSE;
00042 volatile uint16 gButtonTimeout = FALSE;
00043 volatile uint8 gButtonLongTimeout =FALSE; // this is to detect long presses on the button
00044 //mtA
00045 //char KEY = NULL;
00046 //char KEY_VALID = FALSE;
00047 volatile uint8 KEY = NULL;
00048 volatile uint8 KEY_VALID = FALSE;
00049 volatile uint8 KEY_SHIFT = FALSE;
00050 #ifdef MATRIX_BUTTONS 
00051 volatile uint8 KEY_COL = 0;
00052 #endif
00053 //mtE
00054 
00055 //char CountdownTimerHandle;
00056 int long_timeout_count=0;
00057 
00058 void ButtonTimer(void);
00059 
00060 
00061 #ifdef MATRIX_BUTTONS 
00062 
00071 void Matrix_timer(void)
00072 {
00073     static int delay = 0;
00074     char temp;
00075     char matrixmask;
00076     
00077     matrixmask = (1<<(MATRIXCOLS))-1; // mask for all matrix bits 
00078     temp = MATRIXPORT  & ~matrixmask ;                // save status of port 
00079     if (delay++>MATRIXDELAY){
00080         delay = 0;                  // reset delay count
00081         KEY_COL++;                  // next column
00082         if (KEY_COL>MATRIXCOLS-1)   // limit 0..MATRIXCOLS
00083            KEY_COL=0;
00084         MATRIXPORT = (~(1<<KEY_COL) & matrixmask ) | temp  ;  // Set new scan Column
00085     }
00086         
00087 }
00088 
00096 void Matrix_init(void)
00097 {
00098     MATRIXDDR  |= (1<<(MATRIXCOLS))-1 ;        // set to output
00099     MATRIXPORT |= (1<<(MATRIXCOLS))-1;                      // disable all columns
00100     Timer0_RegisterCallbackFunction(Matrix_timer);  // setup timer to scan matrix
00101 }
00102 
00103 #endif
00104 
00111 void Button_Init(void)
00112 {
00113     // Init port pins
00114     DDRB &= ~PINB_MASK;
00115     PORTB |= PINB_MASK;
00116     
00117         DDRE &= ~PINE_MASK;
00118     PORTE |= PINE_MASK;
00119 
00120     // Enable pin change interrupt on PORTB and PORTE
00121         PCMSK0 = PINE_MASK; 
00122         PCMSK1 = PINB_MASK;
00123         EIFR = (1<<PCIF0)|(1<<PCIF1);
00124         EIMSK = (1<<PCIE0)|(1<<PCIE1);
00125 
00126     #ifdef MATRIX_BUTTONS 
00127     Matrix_init();
00128     #endif
00129    
00130 //      CountdownTimerHandle = Timer0_AllocateCountdownTimer();
00131 }
00132 
00133 
00139 void PinChangeInterrupt(void)
00140 {
00141     uint8 buttons;
00142 
00143     uint8 key;
00144 
00145 /*
00146     Read the buttons:
00147 
00148     Bit             7   6   5   4   3   2   1   0
00149     ---------------------------------------------
00150     PORTB           B   A       O
00151     PORTE                           D   C
00152     ---------------------------------------------
00153     PORTB | PORTE   B   A       O   D   C
00154     =============================================
00155 */
00156 
00157 
00158     buttons = (~PINB) & PINB_MASK;
00159     buttons |= (~PINE) & PINE_MASK;
00160 
00161     // Output virtual keys
00162     if (buttons & (1<<BUTTON_A))
00163         key = KEY_PLUS;
00164     else if (buttons & (1<<BUTTON_B))
00165         key = KEY_MINUS;
00166     else if (buttons & (1<<BUTTON_C))
00167         key = KEY_PREV;
00168     else if (buttons & (1<<BUTTON_D))
00169         key = KEY_NEXT;
00170     else if (buttons & (1<<BUTTON_O))
00171         key = KEY_ENTER;
00172     else
00173         key = KEY_NULL;
00174     
00175 
00176         
00177     if(key != KEY_NULL)
00178     {
00179         
00180 #ifdef MATRIX_BUTTONS 
00181         key = key +  (MATRIXROWS * KEY_COL);
00182 #endif
00183         
00184         if(gButtonTimeout)  // gButtonTimeout is set in the LCD_SOF_interrupt in LCD_driver.c
00185         {
00186                         //gButtonLongTimeout = FALSE; // clear the hold down timer
00187                         //long_timeout_count=0;
00188                         
00189             if (!KEY_VALID)
00190             {
00191                 KEY = key;          // Store key in global key buffer
00192                                 KEY_VALID = TRUE;
00193             }
00194                         
00195                         gButtonTimeout = FALSE;
00196                         
00197         }
00198     }else/*{ // make key only valid when released.
00199             if(gButtonTimeout)  // gButtonTimeout is set in the LCD_SOF_interrupt in LCD_driver.c
00200         {       
00201             if (!KEY_VALID)
00202             {
00203                 KEY_VALID = TRUE;
00204                                 KEY_SHIFT = FALSE;
00205             }
00206                         
00207                         gButtonTimeout = FALSE;
00208                         
00209         }
00210                 
00211         
00212         }*/
00213     
00214     EIFR = (1<<PCIF1) | (1<<PCIF0);     // Delete pin change interrupt flags
00215 
00216    //gPowerSaveTimer = 0;                // Reset the Auto Power Down timer
00217     
00218 }
00219 
00220 
00228 char getkey(void)
00229 {
00230     uint8 k;
00231 
00232     cli(); // mt: __disable_interrupt();
00233 
00234     if (KEY_VALID)              // Check for unread key in buffer
00235     {
00236                 k=KEY;
00237         KEY_VALID = FALSE;
00238                 //if (KEY_SHIFT){
00239                 //      k += 8;
00240                 //      KEY_SHIFT = FALSE;
00241         //      }
00242                 gButtonTimeout = FALSE;
00243         //      gButtonLongTimeout = TRUE;
00244     }
00245     else
00246         k = KEY_NULL;           // No key stroke available
00247 
00248     sei(); // mt: __enable_interrupt();
00249 
00250     return k;
00251 }
00252 
00253 
00254 // #pragma vector = PCINT0_vect
00255 // __interrupt void PCINT0_interrupt(void)
00256 SIGNAL(SIG_PIN_CHANGE0)
00257 // mtE
00258 {
00259         PinChangeInterrupt();
00260 }
00261 
00262 // mtA
00263 // #pragma vector = PCINT1_vect
00264 // __interrupt void PCINT1_interrupt(void)
00265 // mtE
00266 SIGNAL(SIG_PIN_CHANGE1)
00267 {
00268         PinChangeInterrupt();
00269 }
00270 
00271 
 All Files Functions Variables Typedefs Enumerations Enumerator Defines