![](/media/cache/profiles/493c8b3821e768713a4d1c5b1e7f5ad4.jpg.50x50_q85.png)
miniblip USB piano example
Fork of Fruit_Piano by
main.cpp@1:93bbcf91f356, 2014-08-18 (annotated)
- Committer:
- yihui
- Date:
- Mon Aug 18 01:58:34 2014 +0000
- Revision:
- 1:93bbcf91f356
- Parent:
- 0:4755a81efb1d
- Child:
- 2:1cb69f19f49c
Custom for Fruit Piano
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:4755a81efb1d | 1 | #include "mbed.h" |
yihui | 0:4755a81efb1d | 2 | #include "USBKeyboard.h" |
yihui | 0:4755a81efb1d | 3 | |
yihui | 0:4755a81efb1d | 4 | #define LOG(args...) // printf(args) |
yihui | 0:4755a81efb1d | 5 | |
yihui | 0:4755a81efb1d | 6 | #define THRESHOLD 2 |
yihui | 0:4755a81efb1d | 7 | #define TOUCH_N 6 |
yihui | 0:4755a81efb1d | 8 | |
yihui | 0:4755a81efb1d | 9 | BusOut leds(LED1, LED2, LED3, LED4); |
yihui | 0:4755a81efb1d | 10 | Ticker tick; |
yihui | 0:4755a81efb1d | 11 | USBKeyboard keyboard; |
yihui | 0:4755a81efb1d | 12 | |
yihui | 1:93bbcf91f356 | 13 | uint8_t key_map[] = {'a', 'f', 'd', 'g', 'd', 'h', 'j', 'k', 'l', ';', '\'', '\n'}; |
yihui | 1:93bbcf91f356 | 14 | PinName touch_pin[] = {A0, A3, A2, A4, A1, A5}; |
yihui | 0:4755a81efb1d | 15 | DigitalInOut *p_touch_io[TOUCH_N]; |
yihui | 0:4755a81efb1d | 16 | |
yihui | 1:93bbcf91f356 | 17 | uint16_t touch_data[TOUCH_N] = {0, }; |
yihui | 0:4755a81efb1d | 18 | |
yihui | 0:4755a81efb1d | 19 | void detect(void) |
yihui | 0:4755a81efb1d | 20 | { |
yihui | 0:4755a81efb1d | 21 | for (int i = 0; i < TOUCH_N; i++) { |
yihui | 0:4755a81efb1d | 22 | uint8_t count = 0; |
yihui | 0:4755a81efb1d | 23 | DigitalInOut *touch_io = p_touch_io[i]; |
yihui | 0:4755a81efb1d | 24 | |
yihui | 0:4755a81efb1d | 25 | touch_io->input(); |
yihui | 0:4755a81efb1d | 26 | touch_data[i] <<= 1; |
yihui | 0:4755a81efb1d | 27 | while (touch_io->read()) { |
yihui | 0:4755a81efb1d | 28 | count++; |
yihui | 0:4755a81efb1d | 29 | if (count > THRESHOLD) { |
yihui | 0:4755a81efb1d | 30 | touch_data[i] |= 0x01; |
yihui | 0:4755a81efb1d | 31 | break; |
yihui | 0:4755a81efb1d | 32 | } |
yihui | 0:4755a81efb1d | 33 | } |
yihui | 0:4755a81efb1d | 34 | touch_io->output(); |
yihui | 0:4755a81efb1d | 35 | touch_io->write(1); |
yihui | 0:4755a81efb1d | 36 | |
yihui | 0:4755a81efb1d | 37 | if (0x01 == touch_data[i]) { // a measurement is about the threshold, get a touch |
yihui | 0:4755a81efb1d | 38 | leds = 1 << i; |
yihui | 0:4755a81efb1d | 39 | keyboard.putc(key_map[i]); |
yihui | 0:4755a81efb1d | 40 | LOG("No %d key is touched\r\n", i); |
yihui | 0:4755a81efb1d | 41 | } else if (0x80 == touch_data[i]) { // last 7 measurement is under the threshold, touch is released |
yihui | 0:4755a81efb1d | 42 | leds = 0x00; |
yihui | 0:4755a81efb1d | 43 | LOG("No %d key is released\r\n", i); |
yihui | 0:4755a81efb1d | 44 | } |
yihui | 0:4755a81efb1d | 45 | } |
yihui | 0:4755a81efb1d | 46 | } |
yihui | 0:4755a81efb1d | 47 | |
yihui | 0:4755a81efb1d | 48 | int main() |
yihui | 0:4755a81efb1d | 49 | { |
yihui | 0:4755a81efb1d | 50 | // setup |
yihui | 0:4755a81efb1d | 51 | for (int i = 0; i < TOUCH_N; i++) { |
yihui | 0:4755a81efb1d | 52 | p_touch_io[i] = new DigitalInOut(touch_pin[i]); |
yihui | 0:4755a81efb1d | 53 | p_touch_io[i]->mode(PullDown); |
yihui | 0:4755a81efb1d | 54 | p_touch_io[i]->output(); |
yihui | 0:4755a81efb1d | 55 | p_touch_io[i]->write(1); |
yihui | 0:4755a81efb1d | 56 | } |
yihui | 0:4755a81efb1d | 57 | |
yihui | 1:93bbcf91f356 | 58 | tick.attach(detect, 1.0 / 40.0); |
yihui | 0:4755a81efb1d | 59 | |
yihui | 0:4755a81efb1d | 60 | while(1) { |
yihui | 0:4755a81efb1d | 61 | // do something |
yihui | 0:4755a81efb1d | 62 | wait(1); |
yihui | 0:4755a81efb1d | 63 | } |
yihui | 0:4755a81efb1d | 64 | } |