Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members

keys.c

Go to the documentation of this file.
00001 /***
00002  * This file is part of OpenHome, an open source home automation system.
00003  * Copyright (C) 2003 Jan Klötzke
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00023 #include <avr/io.h>
00024 #include <avr/interrupt.h>
00025 #include <inttypes.h>
00026 #include "main/keys.h"
00027 
00028 #define KEY_PRESS       5
00029 #define KEY_REPEAT      96
00030 #define KEY_DELAY       12
00031 
00032 static uint8_t cycle = 0;
00033 static uint8_t leds, status;
00034 static uint8_t key_counter[8];
00035 static uint8_t key_buffer[8];
00036 static uint8_t key_repeat_mask = 0;
00037 static volatile uint8_t kbd_rd, kbd_wr = 0;
00038 
00045 void keys_sense_keys(void)
00046 {
00047         uint8_t key, mask, stat;
00048         
00049         // drive led's and capture key stats
00050         cycle++;
00051         if (cycle & 0x01) {
00052                 // ch2
00053                 cbi(PORTD, PD5);
00054                 PORTC = (PORTC & 0xf0) | (leds >> 4);
00055                 sbi(PORTD, PD4);
00056                 asm volatile ("nop");
00057                 status = (status & 0x0f) | (PINF & 0xf0);
00058         } else {
00059                 // ch1
00060                 cbi(PORTD, PD4);
00061                 PORTC = (PORTC & 0xf0) | (leds & 0x0f);
00062                 sbi(PORTD, PD5);
00063                 asm volatile ("nop");
00064                 status = (status & 0xf0) | (PINF >> 4);
00065         }
00066         
00067         // scan for keypress every 8ms
00068         if (cycle < 8) return;
00069         cycle = 0;
00070         stat = status;
00071         mask = key_repeat_mask;
00072         for (key=0; key<8; key++) {
00073                 if (stat & 1) {
00074                         key_counter[key]++;     // key pressed
00075                 } else {
00076                         key_counter[key] = 0;   // key released
00077                 }
00078 
00079                 if (key_counter[key] > KEY_REPEAT) {
00080                         key_counter[key] -= KEY_DELAY;
00081                         if ((mask & 1) == 0) goto skip;
00082                 } else {
00083                         if (key_counter[key] != KEY_PRESS) goto skip;
00084                 }
00085                 key_buffer[kbd_wr++] = key+1;
00086                 kbd_wr &= 7;
00087 
00088         skip:
00089                 mask >>= 1;
00090                 stat >>= 1;
00091         }
00092 }
00093 
00097 uint8_t keys_key_pressed(void)
00098 {
00099         return kbd_rd != kbd_wr;
00100 }
00101 
00105 uint8_t keys_get_key(void)
00106 {
00107         uint8_t result;
00108         
00109         while (kbd_rd == kbd_wr);
00110         result = key_buffer[kbd_rd++];
00111         kbd_rd &= 7;
00112         return result;
00113 }
00114 
00118 void keys_set_leds(uint8_t mask)
00119 {
00120         leds = ~mask;
00121 }
00122 
00126 void keys_set_repeat_mask(uint8_t mask)
00127 {
00128         key_repeat_mask = mask;
00129 }

Generated on Thu Oct 16 13:13:41 2003 for OpenHomeMainPanel by doxygen 1.3.3