A program that simulates a simple stopwatch program using three buttons, and uses LEDs and vibration as feedback to the buttons.

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351

Committer:
fusop
Date:
Wed Apr 19 15:00:09 2017 +0000
Revision:
0:623612f2cc48
Final version of Stopwatch;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fusop 0:623612f2cc48 1 //Filarius Peter Usop
fusop 0:623612f2cc48 2 //Mini Project 1
fusop 0:623612f2cc48 3 //Uses LEDs, Buttons, Haptic, and OLEd
fusop 0:623612f2cc48 4
fusop 0:623612f2cc48 5 #include "mbed.h"
fusop 0:623612f2cc48 6 #include "Hexi_OLED_SSD1351.h"
fusop 0:623612f2cc48 7 #include "Hexi_KW40Z.h"
fusop 0:623612f2cc48 8 #include "Functions.h"
fusop 0:623612f2cc48 9 #include "string.h"
fusop 0:623612f2cc48 10
fusop 0:623612f2cc48 11 //SSD1351 Driver (OLED) Instantiation
fusop 0:623612f2cc48 12 SSD1351 oled(PTB22, PTB21, PTC13, PTB20, PTE6, PTD15);
fusop 0:623612f2cc48 13
fusop 0:623612f2cc48 14 Timer t;
fusop 0:623612f2cc48 15
fusop 0:623612f2cc48 16 int main (void)
fusop 0:623612f2cc48 17 {
fusop 0:623612f2cc48 18 //Initialize buttons
fusop 0:623612f2cc48 19 Button_Init();
fusop 0:623612f2cc48 20
fusop 0:623612f2cc48 21 //Initialize the text properties to default
fusop 0:623612f2cc48 22 oled_text_properties_t textProp = {0};
fusop 0:623612f2cc48 23 oled.GetTextProperties(&textProp);
fusop 0:623612f2cc48 24
fusop 0:623612f2cc48 25 //String Pointer
fusop 0:623612f2cc48 26 char text[20];
fusop 0:623612f2cc48 27
fusop 0:623612f2cc48 28 //Flag
fusop 0:623612f2cc48 29 int flag = 1;
fusop 0:623612f2cc48 30
fusop 0:623612f2cc48 31 //Clear screen
fusop 0:623612f2cc48 32 oled.DrawBox(0,0,96,96,COLOR_BLACK);
fusop 0:623612f2cc48 33
fusop 0:623612f2cc48 34 //Top Title
fusop 0:623612f2cc48 35 strcpy((char *) text, "Stopwatch");
fusop 0:623612f2cc48 36 textProp.fontColor = COLOR_WHITE;
fusop 0:623612f2cc48 37 textProp.alignParam = OLED_TEXT_ALIGN_CENTER;
fusop 0:623612f2cc48 38 oled.SetTextProperties(&textProp);
fusop 0:623612f2cc48 39 oled.TextBox((const uint8_t *) text,0,0,96,15);
fusop 0:623612f2cc48 40
fusop 0:623612f2cc48 41 textProp.fontColor = COLOR_RED;
fusop 0:623612f2cc48 42 oled.SetTextProperties(&textProp);
fusop 0:623612f2cc48 43
fusop 0:623612f2cc48 44 t.start(); //Start stopwatch. You may remove this line
fusop 0:623612f2cc48 45
fusop 0:623612f2cc48 46 //Print to OLED
fusop 0:623612f2cc48 47 sprintf(text, "%.0f", t.read());
fusop 0:623612f2cc48 48 oled.TextBox((uint8_t *) text,0,40,96,15);
fusop 0:623612f2cc48 49
fusop 0:623612f2cc48 50 while (1) {
fusop 0:623612f2cc48 51 Thread::wait(1000); //Refresh every 1 second
fusop 0:623612f2cc48 52 if (flag == 1) //Down button: Stops and resets the stopwatch
fusop 0:623612f2cc48 53 {
fusop 0:623612f2cc48 54 t.stop();
fusop 0:623612f2cc48 55 t.reset();
fusop 0:623612f2cc48 56 sprintf(text, "%.0f", t.read());
fusop 0:623612f2cc48 57 flag++;
fusop 0:623612f2cc48 58 }
fusop 0:623612f2cc48 59 else if (flag == 2) //Right button: Starts the stopwatch
fusop 0:623612f2cc48 60 {
fusop 0:623612f2cc48 61 if (but == 1)
fusop 0:623612f2cc48 62 {
fusop 0:623612f2cc48 63 t.start();
fusop 0:623612f2cc48 64 flag++;
fusop 0:623612f2cc48 65 }
fusop 0:623612f2cc48 66 else if (but == 0) flag--;
fusop 0:623612f2cc48 67 }
fusop 0:623612f2cc48 68 else if (flag == 3) //Left button: Stops the stopwatch
fusop 0:623612f2cc48 69 {
fusop 0:623612f2cc48 70 if (but == 2)
fusop 0:623612f2cc48 71 {
fusop 0:623612f2cc48 72 t.stop();
fusop 0:623612f2cc48 73 flag--;
fusop 0:623612f2cc48 74 }
fusop 0:623612f2cc48 75 else if (but == 0) flag = flag-2;
fusop 0:623612f2cc48 76 }
fusop 0:623612f2cc48 77
fusop 0:623612f2cc48 78 //Update value on OLED
fusop 0:623612f2cc48 79 sprintf(text, "%.0f", t.read());
fusop 0:623612f2cc48 80 oled.TextBox((uint8_t *) text,0,40,96,15);
fusop 0:623612f2cc48 81 }
fusop 0:623612f2cc48 82 }