Code to be run for the project Group Fitness
Dependencies: FXOS8700 Hexi_KW40Z Hexi_OLED_SSD1351 MAX30101
Fork of final_project_ee119 by
main.cpp
- Committer:
- trhackett
- Date:
- 2018-06-15
- Revision:
- 5:a2f68bbb5400
- Parent:
- 4:eb89733b8642
- Child:
- 6:328ef7a29083
File content as of revision 5:a2f68bbb5400:
#include "mbed.h"
#include "mbed_events.h"
#include "Hexi_KW40Z.h"
#include "Hexi_OLED_SSD1351.h"
#include "OLED_types.h"
#include "OpenSans_Font.h"
#include "string.h"
#include "FXOS8700.h"
void UpdateSensorData(void);
void txTask(void);
FXOS8700 accel(PTC11, PTC10);
/* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */
KW40Z kw40z_device(PTE24, PTE25);
/* Instantiate the SSD1351 OLED Driver */
SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */
/*Create a Thread to handle sending BLE Sensor Data */
Thread txThread;
Thread t;
char text[20];
// Variables
float accel_data[3]; // Storage for the data from the sensor
float accel_rms=0.0; // RMS value from the sensor
float ax, ay, az; // Integer value from the sensor to be displayed
const uint8_t *image1; // Pointer for the image1 to be displayed
char text1[20]; // Text Buffer for dynamic value displayed
char text2[20]; // Text Buffer for dynamic value displayed
char text3[20]; // Text Buffer for dynamic value displayed
float dot;
float old_acc=0;
float new_acc=0;
float old_accx, old_accy, old_accz, old_dot=0.0;
uint8_t StepNum = 0, StepNumber = 0;
float filter_buf[75];
/****************************Call Back Functions*******************************/
float Filter(int s)
{
accel.acquire_accel_data_g(accel_data);
float filter_sum = 0.0;
//printf("%d\n\r",s);
for(int i = 0; i < 75; i++)
{
filter_buf[i] = accel_data[s];
//printf("%4.2f\n\r",filter_buf[i]);
filter_sum += filter_buf[i];
}
return (float)(filter_sum / 75);
}
// main() runs in its own thread in the OS
int main() {
accel.accel_config();
txThread.start(txTask); /*Start transmitting Sensor Tag Data */
while (true) {
Thread::wait(50);
}
}
void txTask(void){
while (true)
{
UpdateSensorData();
/*Notify Hexiwear App that it is running Sensor Tag mode*/
kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
/*Send Battery Level for 20% */
kw40z_device.SendBatteryLevel(StepNumber);
Thread::wait(10);
}
}
void UpdateSensorData(void)
{
accel.acquire_accel_data_g(accel_data);
ax = Filter(0);
ay = Filter(1);
az = Filter(2);
wait(0.02);
accel_rms = sqrt((ax*ax)+(ay*ay)+(az*az)/3);
dot = (old_accx * ax)+(old_accy * ay)+(old_accz * az);
old_acc = abs(sqrt(old_accx*old_accx+old_accy*old_accy+old_accz*old_accz));
new_acc = abs(sqrt(ax*ax+ay*ay+az*az));
dot /= (old_acc * new_acc);
/* Display Legends */
StepNum = StepNumber;
if(abs(dot - old_dot) >= 0.05 && abs(dot - old_dot) <= 0.10) {
StepNumber += 1;
}
old_accx = ax;
old_accy = ay;
old_accz = az;
old_dot = dot;
Thread::wait(250);
}

