EmbedEd
/
mbed_theremin
xypad theremin for LPC1768
Diff: touch.cpp
- Revision:
- 1:aa184d2eb2e3
- Parent:
- 0:8ee38453bad9
- Child:
- 2:c5eeaf1c8e69
diff -r 8ee38453bad9 -r aa184d2eb2e3 touch.cpp --- a/touch.cpp Sun Mar 13 01:12:40 2016 +0000 +++ b/touch.cpp Mon Mar 14 23:37:58 2016 +0000 @@ -32,7 +32,7 @@ * */ -typedef enum {TOUCH_NOTOUCH,TOUCH_DEBOUNCE,TOUCH_PRESENT} TOUCH_STATE; +typedef enum {TOUCH_NOTOUCH,TOUCH_DEBOUNCE,NOTOUCH_DEBOUNCE,TOUCH_PRESENT} TOUCH_STATE; #define TSC_ADDR 0x82 // (i2c address for touchscreen)<<1 @@ -100,12 +100,13 @@ // GPIO_AF -- GPIO Alternate FunctionRegister #define GPIO_AF 0x17 - +DigitalOut led1(LED1); DigitalOut led4(LED4); i2c_t touch_ctrl; // i2c interface struct for touch screen -static bool touch_present(void); +static int touch_present(void); +static void touch_reset(void); static void touch_compute_params(void); static char tsc_reset[2]={SYS_CTRL1, SOFT_RESET}; @@ -137,10 +138,17 @@ // used by state machine that debounces touch detection TOUCH_STATE touch_state=TOUCH_NOTOUCH; -#define GOOD_TOUCH_COUNT 8 -#define NO_TOUCH_COUNT 8 +#define GOOD_TOUCH_COUNT 4 +#define NO_TOUCH_COUNT 4 + +bool touch_init(void) +{ + touch_reset(); + touch_state = TOUCH_NOTOUCH; + return true; +} -bool touch_init(void) +void touch_reset(void) { char chipid[2]; @@ -205,44 +213,55 @@ i2c_write(&touch_ctrl,TSC_ADDR,ena_intrupt,2,1); wait_ms(1); - - touch_state = TOUCH_NOTOUCH; - return true; } bool touch_debounce(void) { static int debounce_count=0; + int tret; + tret=touch_present(); switch (touch_state) { case TOUCH_NOTOUCH: - if (touch_present()) { + if (tret>0) { debounce_count=0; touch_state = TOUCH_DEBOUNCE; } break; case TOUCH_DEBOUNCE: - if (touch_present()) { + if (tret>0) { if (++debounce_count > GOOD_TOUCH_COUNT) { touch_state = TOUCH_PRESENT; + led1=1; } - } else { + } else if(tret == 0) { touch_state = TOUCH_NOTOUCH; } break; + + case NOTOUCH_DEBOUNCE: + if(tret>0) { + touch_state = TOUCH_PRESENT; + } else if(tret== 0) { + if(++debounce_count > NO_TOUCH_COUNT) { + debounce_count=0; + touch_state=TOUCH_NOTOUCH; + } + } + break; case TOUCH_PRESENT: - if (touch_present()) { + if (tret>0) { touch_compute_params(); - return true; - } else { - touch_state = TOUCH_NOTOUCH; + } else if(tret==0) { + debounce_count=0; + touch_state=NOTOUCH_DEBOUNCE; } break; } - return false; + return ((touch_state==TOUCH_PRESENT) || (touch_state==NOTOUCH_DEBOUNCE)); } int touch_frequency(void) @@ -257,33 +276,35 @@ void touch_compute_params(void) { if(0>touch_get_xy(&touch_x,&touch_y)) return; - touch_audio_freq = TOUCH_MIN_FREQUENCY + if((touch_y>0x3F) && (touch_y!=0xFFF)) { + touch_audio_freq = (TOUCH_MIN_FREQUENCY + (TOUCH_MAX_FREQUENCY - TOUCH_MIN_FREQUENCY) - * touch_y/0xFFF; - touch_audio_amplitude = TOUCH_MAX_AMPLITUDE*touch_x/0xFFF; -// debug_hexshort((short)touch_audio_freq); -// debug_putch(','); -// debug_hexshort((short)touch_audio_amplitude); -// debug_crlf(); + * touch_y)>>12; + } + if((touch_x > 0x3F) && (touch_x != 0xFFF)) { + touch_audio_amplitude = (TOUCH_MAX_AMPLITUDE*touch_x)>>12; + } } - -bool touch_present(void) +/* + * return 1 if touch present, 0 if touch absent, and -1 + * if there was an i2c error + */ +int touch_present(void) { - i2c_init(&touch_ctrl,p28,p27); // sync i2c routines - i2c_frequency(&touch_ctrl,100000); // 100kHz clock +// <<<<< i2c_init(&touch_ctrl,p28,p27); // sync i2c routines +// <<<<< i2c_frequency(&touch_ctrl,100000); // 100kHz clock i2c_write(&touch_ctrl,TSC_ADDR,&tsc_ctrl,1,0); i2c_read(&touch_ctrl,TSC_ADDR,&touch_status,1,1); - if ((touch_status & TSC_EN_MASK)==0) { // i2c error + if ((touch_status & TSC_EN_MASK)==0) { // i2c error led4=1; // disables screen - wait_ms(10); - touch_init(); // re-init fixes - return false; + touch_reset(); // re-init fixes + return -1; } else if ((touch_status & TSC_TOUCH_DET)>0) { - return true; + return 1; } - return false; + return 0; } int touch_get_xy(short *x,short *y) @@ -313,10 +334,10 @@ // 0xFFF or 0x000 is some kind of glitch if (*x==0xFFF && *y==0xFFF) return NIL; if (*x==0 && *y==0) return NIL; -// debug_hexshort(*x); -// debug_putch(','); -// debug_hexshort(*y); -// debug_crlf(); + debug_hexshort(*x); + debug_putch(','); + debug_hexshort(*y); + debug_crlf(); return 0; }