20170923

Dependencies:   FXOS8700 Hexi_KW40Z Hexi_OLED_SSD1351

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "FXOS8700.h"
00003 #include "Hexi_OLED_SSD1351.h"
00004 #include "Hexi_KW40Z.h"
00005 #include "motion.h"
00006 #include "button.h"
00007 #include "string.h"
00008 #include "font.h"
00009 
00010 #define LED_ON      0
00011 #define LED_OFF     1
00012 
00013 void StartHaptic(void);
00014 void StopHaptic(void const *n);
00015 void ButtonRight(void);
00016 void ButtonLeft(void);
00017 void ButtonUp(void);
00018 void ButtonDown(void);
00019 void PassKey(void);
00020 
00021 
00022 void txTask(void);
00023 void read_accelTask(void);
00024 void read_magTask(void);
00025 
00026 void main_screen(void);
00027 void accel_screen(void);
00028 void mag_screen(void);
00029 
00030 DigitalOut  haptic(PTB9);
00031 DigitalOut  redLed(LED1,1);
00032 DigitalOut  greenLed(LED2,1);
00033 DigitalOut  blueLed(LED3,1);
00034 
00035 Serial      pc(USBTX, USBRX);
00036 FXOS8700    accel(PTC11, PTC10);
00037 FXOS8700    mag(PTC11, PTC10);
00038 SSD1351     oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15);  // SSD1351 OLED Driver (MOSI,SCLK,POWER,CS,RST,DC)
00039 KW40Z       kw40z_device(PTE24, PTE25);                 // Instantiate the Hexi KW40Z Driver (UART TX, UART RX)
00040 RtosTimer   hapticTimer(StopHaptic, osTimerOnce);
00041 
00042 Thread      tx_Thread;                                  //Create a Thread to handle sending BLE Sensor Data
00043 Thread      Read_accelThread;
00044 Thread      Read_magThread;
00045 
00046 // Variables
00047 
00048 float accel_rms = 0.0f; // RMS value from the sensor
00049 float mag_rms = 0.0f;
00050 
00051 const uint8_t *display_image; // Pointer for the image1 to be displayed
00052 char text[20];
00053 char text1[20]; // Text Buffer for dynamic value displayed
00054 char text2[20]; // Text Buffer for dynamic value displayed
00055 char text3[20]; // Text Buffer for dynamic value displayed
00056 
00057 int8_t screen_num = 1;
00058 bool ble_flag = false;
00059 
00060 
00061 // main() runs in its own thread in the OS
00062 int main()
00063 {
00064     main_screen();
00065     pc.printf("init btn num : %d\n", screen_num);
00066     /* Register callbacks to application functions */
00067     kw40z_device.attach_buttonLeft(&ButtonLeft);
00068     kw40z_device.attach_buttonRight(&ButtonRight);
00069     kw40z_device.attach_buttonUp(&ButtonUp);
00070     kw40z_device.attach_buttonDown(&ButtonDown);
00071     kw40z_device.attach_passkey(&PassKey);
00072 
00073     accel.accel_config();
00074     mag.mag_config();
00075 
00076     //tx_Thread.start(txTask);
00077 
00078     while (true) {
00079 
00080         if(screen_num <= 0) {
00081             screen_num = 1;
00082         } else if(screen_num >= 3) {
00083             screen_num = 2;
00084         }
00085         blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/
00086     }
00087 }
00088 
00089 void StartHaptic(void)
00090 {
00091     hapticTimer.start(50);
00092     haptic = 1;
00093 }
00094 
00095 void StopHaptic(void const *n)
00096 {
00097     haptic = 0;
00098     hapticTimer.stop();
00099 }
00100 
00101 /****************************Call Back Functions*******************************/
00102 void ButtonRight(void)
00103 {
00104     StartHaptic();
00105     screen_num++;
00106 
00107     if(screen_num == 1 || screen_num == 0) {
00108         main_screen();
00109     } else if(screen_num == 2) {
00110         accel_screen();
00111     } else {
00112 
00113     }
00114 
00115     pc.printf("btn num : %d\n", screen_num);
00116 
00117 }
00118 
00119 void ButtonLeft(void)
00120 {
00121     StartHaptic();
00122     screen_num--;
00123     if(screen_num == 1 || screen_num == 0) {
00124         main_screen();
00125 
00126     } else if(screen_num == 2) {
00127         accel_screen();
00128     } else {
00129     }
00130 
00131     if(screen_num <= 1 && ble_flag == true) {
00132         kw40z_device.ToggleAdvertisementMode();
00133         ble_flag = false;
00134     }
00135 
00136     pc.printf("btn num af : %d\n", screen_num);
00137     ble_flag = true;
00138 
00139 }
00140 
00141 void ButtonUp(void)
00142 {
00143     StartHaptic();
00144 }
00145 
00146 void ButtonDown(void)
00147 {
00148     StartHaptic();
00149 
00150 }
00151 
00152 void PassKey(void)
00153 {
00154     StartHaptic();
00155     strcpy((char *) text,"PAIR CODE");
00156     oled.TextBox((uint8_t *)text,0,0,95,18);
00157 
00158     /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */
00159     sprintf(text,"%d", kw40z_device.GetPassKey());
00160     oled.TextBox((uint8_t *)text,0,15,95,18);
00161 }
00162 
00163 /* txTask() transmits the sensor data */
00164 void txTask(void)
00165 {
00166     float accel_data[3] = {0}; // Storage for the data from the sensor
00167     float mag_data[3]   = {0};
00168     int16_t ax, ay, az; // Integer value from the sensor to be displayed
00169     int16_t mx, my, mz; // Integer value from the sensor to be displayed
00170     
00171     accel.accel_config();
00172     mag.mag_config();
00173 
00174     while (true) {
00175 
00176         accel.acquire_accel_data_g(accel_data);
00177 
00178         ax = (int16_t)(accel_data[0] );
00179         ay = (int16_t)(accel_data[1] );
00180         az = (int16_t)(accel_data[2] );
00181         wait(0.01);
00182         
00183         mag.acquire_mag_data_uT(mag_data);
00184         mx = (int16_t)mag_data[0];
00185         my = (int16_t)mag_data[1];
00186         mz = (int16_t)mag_data[2];
00187         wait(0.01);
00188 
00189         /*Notify Hexiwear App that it is running Sensor Tag mode*/
00190         kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
00191         kw40z_device.SendAccel(ax,ay,az);  /*Send weight using acclero service*/
00192         wait(0.01);
00193         kw40z_device.SendMag(mx, my, mz);  /*Send weight using magnitute service*/
00194         
00195         Thread::wait(1000);
00196     }
00197 }
00198 
00199 void read_accelTask(void)
00200 {
00201     float accel_data[3] = {0}; // Storage for the data from the sensor
00202     float ax, ay, az; // Integer value from the sensor to be displayed
00203 
00204     while(true) {
00205 
00206         accel.acquire_accel_data_g(accel_data);
00207 
00208         ax = accel_data[0];
00209         ay = accel_data[1];
00210         az = accel_data[2];
00211 
00212         /* Format the value */
00213         sprintf(text1,"%4.2f",ax);
00214         /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
00215         oled.Label((uint8_t *)text1,70,0); //Increase textbox for more digits
00216 
00217         /* Format the value */
00218         sprintf(text2,"%4.2f",ay);
00219         /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
00220         oled.Label((uint8_t *)text2,70,30); //Increase textbox for more digits
00221 
00222         /* Format the value */
00223         sprintf(text3,"%4.2f",az);
00224         /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
00225         oled.Label((uint8_t *)text3,70,60); //Increase textbox for more digits
00226 
00227         Thread::wait(500);
00228     }
00229 }
00230 
00231 void read_magTask(void)
00232 {
00233 
00234     float mag_data[3]   = {0};
00235     float mx, my, mz; // Integer value from the sensor to be displayed
00236     while(1) {
00237         mag.acquire_mag_data_uT(mag_data);
00238         mx = mag_data[0];
00239         my = mag_data[1];
00240         mz = mag_data[2];
00241 
00242     }
00243 }
00244 
00245 void main_screen(void)
00246 {
00247 
00248     oled_text_properties_t textProperties = {0};
00249     oled.GetTextProperties(&textProperties);
00250 
00251     /* Turn on the backlight of the OLED Display */
00252     // 이것을 풀면 화면의 밝기가 좀 어두워짐 .. 주석처리하는게 좋음.
00253     //oled.DimScreenON();
00254 
00255     /* Fills the screen with solid black */
00256     oled.FillScreen(COLOR_BLACK);
00257 
00258     display_image  = motion_detected_bmp;
00259     oled.DrawImage(display_image,0,0);
00260 
00261     /* Change font color to Blue */
00262     textProperties.fontColor   = COLOR_BLUE;
00263     oled.SetTextProperties(&textProperties);
00264 
00265     /* Display Bluetooth Label at x=17,y=65 */
00266     strcpy((char *) text,"BLUETOOTH");
00267     oled.Label((uint8_t *)text,17,80);
00268 }
00269 
00270 void accel_screen(void)
00271 {
00272 
00273     oled_text_properties_t textProperties = {0};
00274     oled.GetTextProperties(&textProperties);
00275 
00276     /* Turn on the backlight of the OLED Display */
00277     // 이것을 풀면 화면의 밝기가 좀 어두워짐 .. 주석처리하는게 좋음.
00278     //oled.DimScreenON();
00279 
00280     /* Fills the screen with solid black */
00281     oled.FillScreen(COLOR_BLACK);
00282 
00283     display_image = gui_motionControl_acc_bmp;
00284     oled.DrawImage(display_image,0,0);
00285     display_image = buttonGroup_right_bmp;
00286     oled.DrawImage(display_image,92,30);
00287     display_image = buttonGroup_right_bmp;
00288     oled.DrawImage(display_image,92,57);
00289 }
00290 
00291 void mag_screen(void)
00292 {
00293 
00294     oled.FillScreen(COLOR_BLACK);
00295     display_image = gui_motionControl_gyro_bmp;
00296     oled.DrawImage(display_image,0,0);
00297     display_image = buttonGroup_right_bmp;
00298     oled.DrawImage(display_image,92,30);
00299     display_image = buttonGroup_right_bmp;
00300     oled.DrawImage(display_image,92,57);
00301 
00302 }