Alex Song / Mbed OS Hexiwear_Example

Dependencies:   FXOS8700 FXAS21002 Hexi_KW40Z Hexi_OLED_SSD1351

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**********************************************************************
00002 Texas State University Senior Project - HexiHeart
00003 Team Zeta: Alex Song, Jasmine Rounsaville, Issam Hichami, Neil Baker
00004 Version: HexiHeart_1st 11/12/17
00005 This version has basic menu layout and screen timeout feature.  The menu
00006 are just placeholders (for the most part) and will be either adjusted or
00007 replaced with graphic images.
00008 
00009 ***********************************************************************/
00010 
00011 #include "mbed.h"
00012 #include "Hexi_KW40Z.h"         // Button and BLE fuctions
00013 #include "FXOS8700.h"           // 3D Accelorometer & Mag
00014 #include "FXAS21002.h"          // 3-Axis Gyroscope
00015 #include "Hexi_OLED_SSD1351.h"  // OLED fuctions
00016 #include "OLED_types.h"         // Text attributs
00017 #include "string.h"
00018 #include "OpenSans_Font.h"
00019 
00020 
00021 /* We need to confirm whether it's better to include and
00022 configure every module for lowest power, or whether it's
00023 better to save memory by not doing that
00024 */
00025 
00026 // Definitions
00027 #define LED_ON          0
00028 #define LED_OFF         1
00029 #define SCRN_TIME       60.0
00030 
00031 
00032 void StartHaptic(void);
00033 void StartHaptic(int x);
00034 void StopHaptic(void const *n);
00035 void error_screen(void);
00036 void update_display(void);
00037 
00038 
00039 // *****************  Global variables  ***********************
00040 char text_1[20];            // Text buffer - Do we need more?
00041 char display_buff[30];      //Buffer for conversion to char to display
00042 bool OLED_ON = 1;
00043 uint8_t Screen_Num = 0;      // Initialize to main screen
00044 uint8_t variableExample = 0;
00045 
00046 
00047 // ***************** Define pins *****************************
00048 DigitalOut chips(PTB12);
00049 
00050 FXAS21002 gyro(PTC11,PTC10); // Gyroscope
00051 SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); // SSD1351 OLED Driver (MOSI,SCLK,POWER,CS,RST,DC)
00052 FXOS8700 accel(PTC11, PTC10); // Accelorometer
00053 FXOS8700 mag(PTC11, PTC10);   // Mag (same chip as Accel)
00054 
00055 
00056 DigitalOut RED_Led(LED1);
00057 DigitalOut GRN_Led(LED2);
00058 DigitalOut BLU_Led(LED3);
00059 DigitalOut haptic(PTB9);
00060 
00061 /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */
00062 KW40Z kw40z_device(PTE24, PTE25);
00063 
00064 /* Define timer for haptic feedback */
00065 RtosTimer hapticTimer(StopHaptic, osTimerOnce);
00066 
00067 //***************** Tickers and Timers *****************
00068 Ticker Screen_Timer;// use ticker to turn off OLED
00069 
00070 void timout_timer() // turn off display mode
00071 {
00072     oled.FillScreen(COLOR_BLACK); // Clear screen.. is there a better command for this?
00073     OLED_ON = 0;  // set flag to off
00074     Screen_Timer.detach();
00075 }//end routine
00076 
00077 void ButtonUp(void)
00078 {
00079     Screen_Timer.attach(&timout_timer,(SCRN_TIME));//Is this sufficient to reset/restart ticker timer for OLED?
00080     if (OLED_ON == 0) {
00081         OLED_ON = 1; // Scree was off, set to On
00082         update_display();
00083     } else {
00084         switch(Screen_Num) {
00085             case 0: {// We're in Main Screen
00086                 StartHaptic();
00087                 update_display();
00088                 break;
00089             }
00090             case 1: {// Second Screen
00091                 
00092                 update_display();
00093                 StartHaptic();
00094           
00095             }
00096             default: {
00097                 break;
00098             }
00099         }
00100     }
00101 
00102 }
00103 
00104 void ButtonDown(void)
00105 {
00106     Screen_Timer.attach(&timout_timer,(SCRN_TIME));//Is this sufficient to reset/restart ticker timer for OLED?
00107     if (OLED_ON == 0) {
00108         OLED_ON = 1; // Screen was off, set to On
00109         update_display();
00110     } else {
00111 
00112         switch(Screen_Num) {
00113             case 0: {// We're in Main Screen
00114                 StartHaptic();                
00115                 update_display();
00116                 break;
00117             }
00118             case 1: {// Second Screen
00119                 StartHaptic();
00120                 break;
00121             }
00122             default: {
00123                 break;
00124             }
00125         }
00126     }
00127 }
00128 
00129 void ButtonRight(void)
00130 {
00131     Screen_Timer.attach(&timout_timer,(SCRN_TIME));//Is this sufficient to reset/restart ticker timer for OLED?
00132     if (OLED_ON == 0) {
00133         OLED_ON = 1; // Screen was off, set to On
00134         update_display();
00135     } else {
00136         switch(Screen_Num) 
00137         {
00138             case 0: {// We're in Main Screen
00139                 StartHaptic();
00140                 variableExample++;
00141                 update_display();
00142                 break;
00143             }
00144             default:{
00145                 break;
00146             }
00147         }
00148     }
00149 }
00150 void ButtonLeft(void)
00151 {
00152     Screen_Timer.attach(&timout_timer,(SCRN_TIME));//Is this sufficient to reset/restart ticker timer for OLED?
00153     if (OLED_ON == 0) {
00154         OLED_ON = 1; // Screen was off, set to On
00155         update_display();
00156     } else {
00157         switch(Screen_Num) {
00158             case 0: {// We're in Main Screen
00159                 StartHaptic(); 
00160                 variableExample--;       
00161                 update_display();
00162                 break;
00163             }
00164             case 1:{
00165                 Screen_Num = 0;
00166                 update_display();
00167                 break;   
00168             }
00169         }
00170     }
00171 }
00172 
00173 
00174 int main()
00175 {
00176     oled.FillScreen(COLOR_BLACK); // Clear screen
00177 // *****************  Local variables  ***********************
00178 //   float accel_data[3]; float accel_rms=0.0;
00179 
00180 // **************  configure sensor modules  ******************
00181     accel.accel_config();
00182     mag.mag_config();
00183 //    gyro.gyro_config();
00184 //    maxim = 1;
00185     chips = 1;
00186 
00187     RED_Led = LED_OFF;
00188     GRN_Led = LED_OFF;
00189     BLU_Led = LED_OFF;
00190 // ***** Register callbacks/interupts to application functions *********
00191     kw40z_device.attach_buttonUp(&ButtonUp);
00192     kw40z_device.attach_buttonDown(&ButtonDown);
00193     kw40z_device.attach_buttonLeft(&ButtonLeft);
00194     kw40z_device.attach_buttonRight(&ButtonRight);
00195 
00196 // **** Get OLED Class Default Text Properties ****************
00197     oled_text_properties_t textProperties = {0};
00198     oled.GetTextProperties(&textProperties);
00199 
00200 // *********Set text color and screen alignment  **************
00201     textProperties.fontColor = COLOR_WHITE;
00202     textProperties.alignParam = OLED_TEXT_ALIGN_LEFT;
00203     oled.SetTextProperties(&textProperties);
00204 
00205 // **************  Display spash screen  **********************
00206     textProperties.fontColor = COLOR_RED;
00207     oled.SetTextProperties(&textProperties);
00208     oled.Label((uint8_t *)"Example",20,5); // Display red "Heart" at x,y
00209 
00210     textProperties.fontColor = COLOR_WHITE;
00211     oled.SetTextProperties(&textProperties);
00212     wait(3);  // wait 3 seconds
00213     update_display(); // Displays current screen (screen 0)
00214     Screen_Timer.attach(&timout_timer,(SCRN_TIME));//start ticker timer for turning off LCD
00215 //  ******************* Main Loop *************************
00216     while (true) {
00217 
00218         Thread::wait(500); // wait half a sec in each loop
00219     }
00220 }
00221 //  ************** end of main()
00222 
00223 void update_display(void)
00224 {
00225     oled_text_properties_t textProperties = {0};  // Need these to change font color
00226     oled.GetTextProperties(&textProperties);      // Need these to change font color
00227     switch(Screen_Num) {
00228         case 0: {// Main Screen
00229             oled.FillScreen(COLOR_BLACK); // Clear screen
00230             oled.Label((uint8_t *)"Test Screen",15,10); // Display "" at x,y
00231             oled.Label((uint8_t *)"*",85,15); // "*" at x,y
00232             oled.Label((uint8_t *)"*",85,60); // "*" at x,y
00233             oled.Label((uint8_t *)"-1",10,80); // Display "Back" at x,y
00234             oled.Label((uint8_t *)"+1",60,80);  //Display "enter" at x,y
00235             sprintf(display_buff, "%u", variableExample);
00236             oled.Label((uint8_t *)display_buff,15,40);  //Display "enter" at x,y
00237             break;
00238         }
00239         case 1: {// Panic Alert option
00240             oled.FillScreen(COLOR_BLACK); // Clear screen
00241             oled.Label((uint8_t *)"Second Screen",20,5); // Display at x,y
00242             oled.Label((uint8_t *)"Back",10,80); // Display "Back" at x,y
00243         }
00244     }
00245         
00246 
00247 }
00248 
00249 /*****************************************************************************
00250 Name: StartHaptic
00251 Purpose: Cause the HexiHeart device to vibrate for a predetermined amount of
00252          time
00253 Inputs: None
00254 Returns: None
00255 ******************************************************************************/
00256 void StartHaptic(void)
00257 {
00258     hapticTimer.start(30);  // was originaly 50
00259     haptic = 1;
00260 }
00261 
00262 /*****************************************************************************
00263 Name: StartHaptic
00264 Purpose: Cause the HexiHeart device to vibrate for x amount of time
00265 Inputs: An int representing the duration of the vibration
00266 Returns: None
00267 ******************************************************************************/
00268 
00269 void StartHaptic(int x)
00270 {
00271     hapticTimer.start(x);
00272     haptic = 1;
00273 }
00274 
00275 void StopHaptic(void const *n)
00276 {
00277     haptic = 0;
00278     hapticTimer.stop();
00279 }
00280 
00281 
00282 
00283