From 061430973e82995368d27ff9081391f9475da3c7 Mon Sep 17 00:00:00 2001 From: James McKenzie Date: Mon, 3 Aug 2015 10:34:07 +0100 Subject: fish --- app/Makefile | 4 +- app/code.c | 98 ++++++++++++++++++++++++++++++++++++ app/keypad.c | 148 +++++++++++++++++++++++++++++++++++++++---------------- app/main.c | 8 +-- app/project.h | 3 ++ app/prototypes.h | 7 ++- app/ticker.c | 3 +- app/usb.c | 5 ++ 8 files changed, 227 insertions(+), 49 deletions(-) create mode 100644 app/code.c (limited to 'app') diff --git a/app/Makefile b/app/Makefile index f02196e..c875b94 100644 --- a/app/Makefile +++ b/app/Makefile @@ -24,7 +24,7 @@ PROG=crypto V=1 default: ${PROG}.elf -CSRCS=dfu.c crypto.c main.c usb.c led.c ticker.c i2c.c lcd.c keypad.c +CSRCS=dfu.c crypto.c main.c usb.c led.c ticker.c i2c.c lcd.c keypad.c code.c BINARY = ${PROG} @@ -34,7 +34,7 @@ include ../Makefile.include #DID=$(shell printf '\#include "id.h"\nID_PRODUCT' | ${CC} -I.. -E - | grep -v ^\# ) -INCLUDES += -I.. +INCLUDES += -I../include dfu:${PROG}.dfu dfu-util -R -a 0 -d 1d6b:1932 -s 0x08002000:leave -D $< diff --git a/app/code.c b/app/code.c new file mode 100644 index 0000000..4d32f7b --- /dev/null +++ b/app/code.c @@ -0,0 +1,98 @@ +#include "project.h" + + +static uint8_t code[16]; +static int code_len; +static int hide; +static int show; + + +void code_display(void) +{ +size_t i; + +#if 0 +lcd_erase_line(0,16); + +if (code_len!=16) + lcd_write("Enter code:",0,0); +else + lcd_write("Code entered:",0,0); +#endif + + for (i=0;ibmRequestType & 0x7F) != 0x41) + return 0; /* Only accept vendor request. */ + + switch (req->bRequest) + { + + case 0x34: + (*buf)[0] = 0x1; + (*buf)[1] = 0; + (*buf)[2] = 0; + (*buf)[3] = 0; + (*buf)[4] = 0x2; + (*buf)[5] = 0; /* iString not used here */ + *len = 6; + return 1; + } + + return 0; + +} + + +void code_tick(void) +{ +if (!show) return; +show--; + if (!show) { + hide++; + code_display(); + } + +} + + + + +void key_event (uint8_t v, int ud) +{ + if (!ud) return; + + if (code_len==sizeof(code)) { + code_len=0; + memset(code,' ',sizeof(code)); + } + + + show=5000; + hide=0; + + code[code_len++]=v; + + + code_display(); + + +} diff --git a/app/keypad.c b/app/keypad.c index daea5c7..d4a82db 100644 --- a/app/keypad.c +++ b/app/keypad.c @@ -7,76 +7,140 @@ #define KEYPAD_DELAY do { delay_us(1); } while (0) -uint16_t keypad_read(void) -{ -uint16_t ret=0; -uint16_t c; -/*Reset the state machine in the keypad */ +#define SCAN_INTERVAL 73 +#define DEBOUNCE_INTERVAL 1 +#define DEBOUNCE_COUNT 3 -gpio_set (GPIO_DATA, DATA); -KEYPAD_DELAY; -gpio_clear (GPIO_DATA, DATA); -KEYPAD_DELAY; -gpio_set (GPIO_DATA, DATA); -KEYPAD_DELAY; +static uint32_t next_scan; -gpio_clear (GPIO_CLOCK, CLOCK); -KEYPAD_DELAY; -for (c=0x8000;c;c>>=1) +uint16_t +keypad_raw_read (void) { -gpio_set (GPIO_CLOCK, CLOCK); -KEYPAD_DELAY; -if (!(gpio_get (GPIO_DATA, DATA) & DATA)) ret|=c; -gpio_clear (GPIO_CLOCK, CLOCK); -KEYPAD_DELAY; -} + uint16_t ret = 0; + uint16_t c; -gpio_set (GPIO_CLOCK, CLOCK); +/*Reset the state machine in the keypad */ -return ret; + gpio_set (GPIO_DATA, DATA); + KEYPAD_DELAY; + gpio_clear (GPIO_DATA, DATA); + KEYPAD_DELAY; + gpio_set (GPIO_DATA, DATA); + KEYPAD_DELAY; + + gpio_clear (GPIO_CLOCK, CLOCK); + KEYPAD_DELAY; + + for (c = 0x8000; c; c >>= 1) + { + gpio_set (GPIO_CLOCK, CLOCK); + KEYPAD_DELAY; + if (!(gpio_get (GPIO_DATA, DATA) & DATA)) + ret |= c; + gpio_clear (GPIO_CLOCK, CLOCK); + KEYPAD_DELAY; + } + + gpio_set (GPIO_CLOCK, CLOCK); + + return ret; } -static void -keypad_scan (void) +uint16_t +keypad_read (void) { + int c; + const uint8_t lut[] = "cdef89ab45670123"; + uint8_t ret = 0; - uint16_t v; - char buf[16]; - v=keypad_read(); - - sprintf (buf, "%04x", v); - lcd_write (buf, 0, 1); - +/*Reset the state machine in the keypad */ + gpio_set (GPIO_DATA, DATA); + KEYPAD_DELAY; + gpio_clear (GPIO_DATA, DATA); + KEYPAD_DELAY; + gpio_set (GPIO_DATA, DATA); + KEYPAD_DELAY; + + gpio_clear (GPIO_CLOCK, CLOCK); + KEYPAD_DELAY; + + for (c = 0; c < 16; ++c) + { + gpio_set (GPIO_CLOCK, CLOCK); + KEYPAD_DELAY; + if (!(gpio_get (GPIO_DATA, DATA) & DATA)) + ret = lut[c]; + gpio_clear (GPIO_CLOCK, CLOCK); + KEYPAD_DELAY; + } + + gpio_set (GPIO_CLOCK, CLOCK); + + return ret; } -void -keypad_tick (void) -{ - static int c; - c++; +static void +keypad_scan (void) +{ + static uint8_t last_v; + static uint8_t key_down; + static int same; + uint8_t v; + + v = keypad_read (); + + next_scan = DEBOUNCE_INTERVAL; + + if (v!=last_v) { + last_v=v; + same=0; + return; + } else { + if (same #include +#include #include #include #include @@ -8,6 +9,8 @@ #include #include #include +#include +#include #include diff --git a/app/prototypes.h b/app/prototypes.h index 7f5498a..9a89a8c 100644 --- a/app/prototypes.h +++ b/app/prototypes.h @@ -52,7 +52,12 @@ extern void lcd_reset(void); extern void lcd_init(void); extern void lcd_shutdown(void); /* keypad.c */ -extern int i2c2_bb(int scl, int sda); +extern uint16_t keypad_raw_read(void); extern uint16_t keypad_read(void); extern void keypad_tick(void); extern void keypad_init(void); +/* code.c */ +extern void code_display(void); +extern int vendor_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, int (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)); +extern void code_tick(void); +extern void key_event(uint8_t v, int ud); diff --git a/app/ticker.c b/app/ticker.c index c9d12ec..d054317 100644 --- a/app/ticker.c +++ b/app/ticker.c @@ -29,7 +29,8 @@ sys_tick_handler (void) led_tick (); lcd_tick (); - keypad_tick(); + keypad_tick (); + code_tick(); } diff --git a/app/usb.c b/app/usb.c index ea03f2a..2dfac76 100644 --- a/app/usb.c +++ b/app/usb.c @@ -143,6 +143,11 @@ usb_set_config (usbd_device * usbd_dev, uint16_t wValue) dfu_control_request); + usbd_register_control_callback (usbd_dev, + USB_REQ_TYPE_VENDOR | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + vendor_control_request); + } /* Buffer to be used for control requests. */ -- cgit v1.2.3