Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: tsi_sensor MMA8451Q
Diff: main.cpp
- Revision:
- 1:1e448c750b63
- Parent:
- 0:225e4447fdd3
- Child:
- 3:9262b505d7a8
--- a/main.cpp Sun Jun 07 19:50:51 2020 +0000 +++ b/main.cpp Sat Jun 13 22:53:37 2020 +0000 @@ -1,68 +1,213 @@ #include "mbed.h" +#include "tsi_sensor.h" +#include "MMA8451Q.h" #include "LED_line.h" +/* This defines will be replaced by PinNames soon */ +#if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) + #define ELEC0 9 + #define ELEC1 10 +#elif defined (TARGET_KL05Z) + #define ELEC0 9 + #define ELEC1 8 +#else + #error TARGET NOT DEFINED +#endif + +#if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) + PinName const SDA = PTE25; + PinName const SCL = PTE24; +#elif defined (TARGET_KL05Z) + PinName const SDA = PTB4; + PinName const SCL = PTB3; +#elif defined (TARGET_K20D50M) + PinName const SDA = PTB1; + PinName const SCL = PTB0; +#else + #error TARGET NOT DEFINED +#endif + +#define MMA8451_I2C_ADDRESS (0x1d<<1) + +Thread thread_run(osPriorityNormal); + +bool sym=false; +bool prn=false; + +#define SENSOR_LIMIT 1000 enum sensor {SENSOR_ON=1, SENSOR_OFF=0} s0=SENSOR_OFF, s1=SENSOR_OFF, s2=SENSOR_OFF; +unsigned int s0_counter=0, s1_counter=0, s2_counter=0; + +DigitalIn sens_0(PTC1); +DigitalIn sens_1(PTC2); +DigitalIn sens_2(PTB3); + +LED_line l1 (PTB0, L_LED_OFF, L_LED_ON, L_CYCLE, false); +LED_line l2 (PTB1, L_LED_OFF, L_LED_ON, L_CYCLE, true); + +LED_line led_green(LED_GREEN, L_LED_OFF, L_LED_ON, L_CYCLE, false); +LED_line led_red(LED_RED, L_LED_OFF, L_LED_ON, L_CYCLE, true); +LED_line led_blue(LED_BLUE, L_LED_OFF, L_LED_ON, L_CYCLE, false); + +TSIAnalogSlider tsi(ELEC0, ELEC1, 40); +MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); + +float x, y, z; +float p; +uint32_t d; + +inline void measure_once(void) +{ + x = acc.getAccX(); + y = acc.getAccY(); + z = acc.getAccZ(); + + p = tsi.readPercentage(); + d = tsi.readDistance(); +} + +inline void sensors_once(void) +{ + if (sym) + { + s0 = ((d>1) && (d<10))?SENSOR_ON:SENSOR_OFF; + s1 = ((d>10) && (d<20))?SENSOR_ON:SENSOR_OFF; + s2 = ((d>20) && (d<30))?SENSOR_ON:SENSOR_OFF; + } + else + { + s0 = (sens_0 == 1) ? SENSOR_ON : SENSOR_OFF; + s1 = (sens_1 == 1) ? SENSOR_ON : SENSOR_OFF; + s2 = (sens_2 == 1) ? SENSOR_ON : SENSOR_OFF; + } + + if (SENSOR_OFF == s0) + { + s0_counter = 0; + } + else + { + if (s0_counter > SENSOR_LIMIT) + { + s0 = SENSOR_OFF; + } + else + { + s0_counter++; + } + } + + if (SENSOR_OFF == s1) + { + s1_counter = 0; + } + else + { + if (s1_counter > SENSOR_LIMIT) + { + s1 = SENSOR_OFF; + } + else + { + s1_counter++; + } + } + + if (SENSOR_OFF == s2) + { + s2_counter = 0; + } + else + { + if (s2_counter > SENSOR_LIMIT) + { + s2 = SENSOR_OFF; + } + else + { + s2_counter++; + } + } + + if (d > 35) + { + sym = !sym; + ThisThread::sleep_for(1000); + } + + if ((!sym) && (d>1) && (d<10)) + { + prn = !prn; + ThisThread::sleep_for(1000); + } +} + +inline void action_once(void) +{ + if (s0 == SENSOR_ON) + { + led_green.light(); + l1.light(); + } + if (s1 == SENSOR_ON) + { + led_red.light(); + l1.light(); + l2.light(); + } + if (s2 == SENSOR_ON) + { + led_blue.light(); + l2.light(); + } +} + +inline void print_once(void) +{ + if (prn) + { + printf("%3.3s ", sym?"Sym":"IO "); + printf("S0=%1d ", s0); + printf("S1=%1d ", s1); + printf("S2=%1d ", s2); + printf("l1=%5d ", (int)l1.level()); + printf("l2=%5d ", (int)l2.level()); + printf("R=%5d ", (int)led_red.level()); + printf("G=%5d ", (int)led_green.level()); + printf("B=%5d ", (int)led_blue.level()); + printf("s:%5.2f ", p); + printf("d:%2dmm ", d); + printf("X: %1.2f, Y: %1.2f, Z: %1.2f\r\n", x, y, z); + printf("\r\n"); + } +} + +void run_thread(void) +{ + printf(" Started\r\n"); + while (true) { + measure_once(); + sensors_once(); + action_once(); + print_once(); + ThisThread::sleep_for(500); //wait(1); + } +} int main(void) { - DigitalIn sens_0(PTC1); - DigitalIn sens_1(PTC2); - DigitalIn sens_2(PTB3); - - LED_line l1 (PTB0, L_LED_OFF, L_LED_ON, L_CYCLE, false); - LED_line l2 (PTB1, L_LED_OFF, L_LED_ON, L_CYCLE, true); + printf("\nStarting..."); - LED_line led_green(LED_GREEN, L_LED_OFF, L_LED_ON, L_CYCLE, false); - LED_line led_red(LED_RED, L_LED_OFF, L_LED_ON, L_CYCLE, true); - LED_line led_blue(LED_BLUE, L_LED_OFF, L_LED_ON, L_CYCLE, false); - - printf("\nStart\n"); - -// unsigned long int c=0, l=0; + thread_run.start(run_thread); while (true) { -// c++; -// if (c >= L_CYCLE) -// { -// c = 0; -// l += RAMP_SLOPE; -// if (l >= L_CYCLE) l = 0; -// } - - s0 = (sens_0 == 1) ? SENSOR_ON : SENSOR_OFF; - s1 = (sens_1 == 1) ? SENSOR_ON : SENSOR_OFF; - s2 = (sens_2 == 1) ? SENSOR_ON : SENSOR_OFF; - -// l1.level(l); -// l2.level(l); - -// led_red.level(l); -// led_green.level(l); -// led_blue.level(l); - l1.LED_run(); l2.LED_run(); led_green.LED_run(); led_red.LED_run(); led_blue.LED_run(); - - if (s0 == SENSOR_ON) - { - led_green.light(); - l1.light(); - } - if (s1 == SENSOR_ON) - { - led_red.light(); - l1.light(); - l2.light(); - } - if (s2 == SENSOR_ON) - { - led_blue.light(); - l2.light(); - } + ThisThread::yield(); } }