a
Dependencies: TS_F746 LCD_746 LbBSP746NG GUI_746
main.cpp@3:451a7e2f7928, 2019-12-10 (annotated)
- Committer:
- fundokukiri
- Date:
- Tue Dec 10 14:35:22 2019 +0000
- Revision:
- 3:451a7e2f7928
- Parent:
- 2:e11266cb3ba5
SPKT;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wsteenberg | 0:151a96a0fc95 | 1 | #include "mbed.h" |
wsteenberg | 0:151a96a0fc95 | 2 | #include "F746_BUTTON.hpp" |
wsteenberg | 0:151a96a0fc95 | 3 | |
wsteenberg | 0:151a96a0fc95 | 4 | TS_DISCO_F746NG ts_; |
wsteenberg | 0:151a96a0fc95 | 5 | LCD_DISCO_F746NG lcd_; |
wsteenberg | 0:151a96a0fc95 | 6 | |
tquang14 | 1:dcda1118b4ef | 7 | InterruptIn button1(USER_BUTTON); |
tquang14 | 1:dcda1118b4ef | 8 | volatile bool button1_pressed = false; // Used in the main loop |
tquang14 | 1:dcda1118b4ef | 9 | volatile bool button1_enabled = true; // Used for debouncing |
tquang14 | 1:dcda1118b4ef | 10 | Timeout button1_timeout; // Used for debouncing |
tquang14 | 1:dcda1118b4ef | 11 | |
tquang14 | 1:dcda1118b4ef | 12 | void button1_enabled_cb(void) |
tquang14 | 1:dcda1118b4ef | 13 | { |
tquang14 | 1:dcda1118b4ef | 14 | button1_enabled = true; |
tquang14 | 1:dcda1118b4ef | 15 | } |
tquang14 | 1:dcda1118b4ef | 16 | |
tquang14 | 1:dcda1118b4ef | 17 | // ISR handling button pressed event |
tquang14 | 1:dcda1118b4ef | 18 | void button1_onpressed_cb(void) |
tquang14 | 1:dcda1118b4ef | 19 | { |
tquang14 | 1:dcda1118b4ef | 20 | if (button1_enabled) { // Disabled while the button is bouncing |
tquang14 | 1:dcda1118b4ef | 21 | button1_enabled = false; |
tquang14 | 1:dcda1118b4ef | 22 | button1_pressed = true; // To be read by the main loop |
tquang14 | 1:dcda1118b4ef | 23 | button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms |
tquang14 | 1:dcda1118b4ef | 24 | } |
tquang14 | 1:dcda1118b4ef | 25 | } |
wsteenberg | 0:151a96a0fc95 | 26 | |
wsteenberg | 0:151a96a0fc95 | 27 | char char_SliderDisplayValue[4]; // String Value to display for the Slider Control value |
wsteenberg | 0:151a96a0fc95 | 28 | uint16_t SliderDisplayValue_; // Variable used to access Slider Control Value in F746SLIDER.cpp |
tquang14 | 1:dcda1118b4ef | 29 | DigitalOut led(LED1); |
wsteenberg | 0:151a96a0fc95 | 30 | int main() |
wsteenberg | 0:151a96a0fc95 | 31 | { |
wsteenberg | 0:151a96a0fc95 | 32 | lcd_.Clear(LCD_COLOR_WHITE); // Set LCD Background colour |
wsteenberg | 0:151a96a0fc95 | 33 | |
wsteenberg | 0:151a96a0fc95 | 34 | Button btn1(lcd_, ts_, 20, 50, 80, 40, |
fundokukiri | 3:451a7e2f7928 | 35 | LCD_COLOR_BLUE, LCD_COLOR_YELLOW, 1, "NUTNHAN", Font12); // Define btn1 button |
wsteenberg | 0:151a96a0fc95 | 36 | btn1.Render(); // Draw btn1 button |
tquang14 | 1:dcda1118b4ef | 37 | button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event |
wsteenberg | 0:151a96a0fc95 | 38 | |
tquang14 | 1:dcda1118b4ef | 39 | int idx = 0; // Just for printf below |
wsteenberg | 0:151a96a0fc95 | 40 | while (true) // Main program loop |
fundokukiri | 3:451a7e2f7928 | 41 | { |
tquang14 | 1:dcda1118b4ef | 42 | |
wsteenberg | 0:151a96a0fc95 | 43 | if (btn1.Press()) // Check if btn1 button was touched and run instructions if true |
wsteenberg | 0:151a96a0fc95 | 44 | { |
wsteenberg | 0:151a96a0fc95 | 45 | lcd_.SetFont(&Font12); |
wsteenberg | 0:151a96a0fc95 | 46 | lcd_.SetTextColor(LCD_COLOR_BLACK); |
tquang14 | 1:dcda1118b4ef | 47 | led = !led; |
wsteenberg | 0:151a96a0fc95 | 48 | wait(0.5); |
fundokukiri | 3:451a7e2f7928 | 49 | lcd_.DisplayStringAt(5, 110, (uint8_t *)"", LEFT_MODE); |
wsteenberg | 0:151a96a0fc95 | 50 | } // End btn1 button instructions |
tquang14 | 1:dcda1118b4ef | 51 | if (button1_pressed) { // Set when button is pressed |
tquang14 | 1:dcda1118b4ef | 52 | button1_pressed = false; |
tquang14 | 1:dcda1118b4ef | 53 | led = !led; |
tquang14 | 1:dcda1118b4ef | 54 | char *tmp; |
tquang14 | 1:dcda1118b4ef | 55 | sprintf(tmp, "Button pressed %d", idx++); |
wsteenberg | 0:151a96a0fc95 | 56 | wait(0.5); |
tquang14 | 1:dcda1118b4ef | 57 | lcd_.SetFont(&Font20); |
tquang14 | 1:dcda1118b4ef | 58 | |
tquang14 | 1:dcda1118b4ef | 59 | lcd_.DisplayStringAt(5, 160, (uint8_t *)tmp, LEFT_MODE); |
wsteenberg | 0:151a96a0fc95 | 60 | wait(0.5); |
fundokukiri | 3:451a7e2f7928 | 61 | } |
wsteenberg | 0:151a96a0fc95 | 62 | wait(0.02f); |
fundokukiri | 3:451a7e2f7928 | 63 | } |
fundokukiri | 3:451a7e2f7928 | 64 | } |