Basic touch buttons example for Hexiwear

Dependencies:   Hexi_KW40Z

Committer:
cotigac
Date:
Mon Sep 19 03:50:53 2016 +0000
Revision:
2:5b025ef2835a
Parent:
1:80e17bcc26e4
Small bug fix for left color

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cotigac 0:f7ef26f17610 1 #include "mbed.h"
cotigac 0:f7ef26f17610 2 #include "Hexi_KW40Z.h"
cotigac 0:f7ef26f17610 3
cotigac 0:f7ef26f17610 4 #define LED_ON 0
cotigac 0:f7ef26f17610 5 #define LED_OFF 1
cotigac 0:f7ef26f17610 6
cotigac 0:f7ef26f17610 7 void StartHaptic(void);
cotigac 0:f7ef26f17610 8 void StopHaptic(void const *n);
cotigac 0:f7ef26f17610 9
cotigac 0:f7ef26f17610 10 DigitalOut redLed(LED1);
cotigac 0:f7ef26f17610 11 DigitalOut greenLed(LED2);
cotigac 0:f7ef26f17610 12 DigitalOut blueLed(LED3);
cotigac 0:f7ef26f17610 13 DigitalOut haptic(PTB9);
cotigac 0:f7ef26f17610 14
cotigac 0:f7ef26f17610 15 /* Define timer for haptic feedback */
cotigac 0:f7ef26f17610 16 RtosTimer hapticTimer(StopHaptic, osTimerOnce);
cotigac 0:f7ef26f17610 17
cotigac 0:f7ef26f17610 18 /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */
cotigac 0:f7ef26f17610 19 KW40Z kw40z_device(PTE24, PTE25);
cotigac 1:80e17bcc26e4 20
cotigac 1:80e17bcc26e4 21 void ButtonUp(void)
cotigac 1:80e17bcc26e4 22 {
cotigac 1:80e17bcc26e4 23 StartHaptic();
cotigac 0:f7ef26f17610 24
cotigac 1:80e17bcc26e4 25 redLed = LED_ON;
cotigac 1:80e17bcc26e4 26 greenLed = LED_OFF;
cotigac 1:80e17bcc26e4 27 blueLed = LED_OFF;
cotigac 1:80e17bcc26e4 28 }
cotigac 1:80e17bcc26e4 29
cotigac 1:80e17bcc26e4 30 void ButtonDown(void)
cotigac 1:80e17bcc26e4 31 {
cotigac 1:80e17bcc26e4 32 StartHaptic();
cotigac 1:80e17bcc26e4 33
cotigac 1:80e17bcc26e4 34 redLed = LED_OFF;
cotigac 1:80e17bcc26e4 35 greenLed = LED_ON;
cotigac 1:80e17bcc26e4 36 blueLed = LED_OFF;
cotigac 1:80e17bcc26e4 37 }
cotigac 1:80e17bcc26e4 38
cotigac 1:80e17bcc26e4 39 void ButtonRight(void)
cotigac 1:80e17bcc26e4 40 {
cotigac 1:80e17bcc26e4 41 StartHaptic();
cotigac 1:80e17bcc26e4 42
cotigac 1:80e17bcc26e4 43 redLed = LED_OFF;
cotigac 1:80e17bcc26e4 44 greenLed = LED_OFF;
cotigac 1:80e17bcc26e4 45 blueLed = LED_ON;
cotigac 1:80e17bcc26e4 46 }
cotigac 1:80e17bcc26e4 47
cotigac 1:80e17bcc26e4 48 void ButtonLeft(void)
cotigac 1:80e17bcc26e4 49 {
cotigac 1:80e17bcc26e4 50 StartHaptic();
cotigac 1:80e17bcc26e4 51
cotigac 1:80e17bcc26e4 52 redLed = LED_ON;
cotigac 2:5b025ef2835a 53 greenLed = LED_ON;
cotigac 1:80e17bcc26e4 54 blueLed = LED_OFF;
cotigac 1:80e17bcc26e4 55 }
cotigac 1:80e17bcc26e4 56
cotigac 1:80e17bcc26e4 57 void ButtonSlide(void)
cotigac 1:80e17bcc26e4 58 {
cotigac 1:80e17bcc26e4 59 StartHaptic();
cotigac 1:80e17bcc26e4 60
cotigac 1:80e17bcc26e4 61 redLed = LED_ON;
cotigac 1:80e17bcc26e4 62 greenLed = LED_ON;
cotigac 2:5b025ef2835a 63 blueLed = LED_ON;
cotigac 1:80e17bcc26e4 64 }
cotigac 1:80e17bcc26e4 65
cotigac 0:f7ef26f17610 66 int main()
cotigac 0:f7ef26f17610 67 {
cotigac 0:f7ef26f17610 68 /* Register callbacks to application functions */
cotigac 1:80e17bcc26e4 69 kw40z_device.attach_buttonUp(&ButtonUp);
cotigac 1:80e17bcc26e4 70 kw40z_device.attach_buttonDown(&ButtonDown);
cotigac 1:80e17bcc26e4 71 kw40z_device.attach_buttonLeft(&ButtonLeft);
cotigac 1:80e17bcc26e4 72 kw40z_device.attach_buttonRight(&ButtonRight);
cotigac 1:80e17bcc26e4 73 kw40z_device.attach_buttonSlide(&ButtonSlide);
cotigac 0:f7ef26f17610 74
cotigac 0:f7ef26f17610 75 while (true) {
cotigac 0:f7ef26f17610 76 Thread::wait(500);
cotigac 0:f7ef26f17610 77 }
cotigac 0:f7ef26f17610 78 }
cotigac 0:f7ef26f17610 79
cotigac 0:f7ef26f17610 80 void StartHaptic(void)
cotigac 0:f7ef26f17610 81 {
cotigac 0:f7ef26f17610 82 hapticTimer.start(50);
cotigac 0:f7ef26f17610 83 haptic = 1;
cotigac 0:f7ef26f17610 84 }
cotigac 0:f7ef26f17610 85
cotigac 0:f7ef26f17610 86 void StopHaptic(void const *n) {
cotigac 0:f7ef26f17610 87 haptic = 0;
cotigac 0:f7ef26f17610 88 hapticTimer.stop();
cotigac 0:f7ef26f17610 89 }