J&W / Mbed OS frdm_LED_PWM

Dependencies:   tsi_sensor MMA8451Q

Committer:
Pythia
Date:
Sat Jun 13 22:53:37 2020 +0000
Revision:
1:1e448c750b63
Parent:
0:225e4447fdd3
Child:
3:9262b505d7a8
Updated by providing printout of state, sensor simulation and fault removal

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pythia 0:225e4447fdd3 1 #include "mbed.h"
Pythia 1:1e448c750b63 2 #include "tsi_sensor.h"
Pythia 1:1e448c750b63 3 #include "MMA8451Q.h"
Pythia 0:225e4447fdd3 4 #include "LED_line.h"
Pythia 0:225e4447fdd3 5
Pythia 1:1e448c750b63 6 /* This defines will be replaced by PinNames soon */
Pythia 1:1e448c750b63 7 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
Pythia 1:1e448c750b63 8 #define ELEC0 9
Pythia 1:1e448c750b63 9 #define ELEC1 10
Pythia 1:1e448c750b63 10 #elif defined (TARGET_KL05Z)
Pythia 1:1e448c750b63 11 #define ELEC0 9
Pythia 1:1e448c750b63 12 #define ELEC1 8
Pythia 1:1e448c750b63 13 #else
Pythia 1:1e448c750b63 14 #error TARGET NOT DEFINED
Pythia 1:1e448c750b63 15 #endif
Pythia 1:1e448c750b63 16
Pythia 1:1e448c750b63 17 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
Pythia 1:1e448c750b63 18 PinName const SDA = PTE25;
Pythia 1:1e448c750b63 19 PinName const SCL = PTE24;
Pythia 1:1e448c750b63 20 #elif defined (TARGET_KL05Z)
Pythia 1:1e448c750b63 21 PinName const SDA = PTB4;
Pythia 1:1e448c750b63 22 PinName const SCL = PTB3;
Pythia 1:1e448c750b63 23 #elif defined (TARGET_K20D50M)
Pythia 1:1e448c750b63 24 PinName const SDA = PTB1;
Pythia 1:1e448c750b63 25 PinName const SCL = PTB0;
Pythia 1:1e448c750b63 26 #else
Pythia 1:1e448c750b63 27 #error TARGET NOT DEFINED
Pythia 1:1e448c750b63 28 #endif
Pythia 1:1e448c750b63 29
Pythia 1:1e448c750b63 30 #define MMA8451_I2C_ADDRESS (0x1d<<1)
Pythia 1:1e448c750b63 31
Pythia 1:1e448c750b63 32 Thread thread_run(osPriorityNormal);
Pythia 1:1e448c750b63 33
Pythia 1:1e448c750b63 34 bool sym=false;
Pythia 1:1e448c750b63 35 bool prn=false;
Pythia 1:1e448c750b63 36
Pythia 1:1e448c750b63 37 #define SENSOR_LIMIT 1000
Pythia 0:225e4447fdd3 38 enum sensor {SENSOR_ON=1, SENSOR_OFF=0} s0=SENSOR_OFF, s1=SENSOR_OFF, s2=SENSOR_OFF;
Pythia 1:1e448c750b63 39 unsigned int s0_counter=0, s1_counter=0, s2_counter=0;
Pythia 1:1e448c750b63 40
Pythia 1:1e448c750b63 41 DigitalIn sens_0(PTC1);
Pythia 1:1e448c750b63 42 DigitalIn sens_1(PTC2);
Pythia 1:1e448c750b63 43 DigitalIn sens_2(PTB3);
Pythia 1:1e448c750b63 44
Pythia 1:1e448c750b63 45 LED_line l1 (PTB0, L_LED_OFF, L_LED_ON, L_CYCLE, false);
Pythia 1:1e448c750b63 46 LED_line l2 (PTB1, L_LED_OFF, L_LED_ON, L_CYCLE, true);
Pythia 1:1e448c750b63 47
Pythia 1:1e448c750b63 48 LED_line led_green(LED_GREEN, L_LED_OFF, L_LED_ON, L_CYCLE, false);
Pythia 1:1e448c750b63 49 LED_line led_red(LED_RED, L_LED_OFF, L_LED_ON, L_CYCLE, true);
Pythia 1:1e448c750b63 50 LED_line led_blue(LED_BLUE, L_LED_OFF, L_LED_ON, L_CYCLE, false);
Pythia 1:1e448c750b63 51
Pythia 1:1e448c750b63 52 TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
Pythia 1:1e448c750b63 53 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
Pythia 1:1e448c750b63 54
Pythia 1:1e448c750b63 55 float x, y, z;
Pythia 1:1e448c750b63 56 float p;
Pythia 1:1e448c750b63 57 uint32_t d;
Pythia 1:1e448c750b63 58
Pythia 1:1e448c750b63 59 inline void measure_once(void)
Pythia 1:1e448c750b63 60 {
Pythia 1:1e448c750b63 61 x = acc.getAccX();
Pythia 1:1e448c750b63 62 y = acc.getAccY();
Pythia 1:1e448c750b63 63 z = acc.getAccZ();
Pythia 1:1e448c750b63 64
Pythia 1:1e448c750b63 65 p = tsi.readPercentage();
Pythia 1:1e448c750b63 66 d = tsi.readDistance();
Pythia 1:1e448c750b63 67 }
Pythia 1:1e448c750b63 68
Pythia 1:1e448c750b63 69 inline void sensors_once(void)
Pythia 1:1e448c750b63 70 {
Pythia 1:1e448c750b63 71 if (sym)
Pythia 1:1e448c750b63 72 {
Pythia 1:1e448c750b63 73 s0 = ((d>1) && (d<10))?SENSOR_ON:SENSOR_OFF;
Pythia 1:1e448c750b63 74 s1 = ((d>10) && (d<20))?SENSOR_ON:SENSOR_OFF;
Pythia 1:1e448c750b63 75 s2 = ((d>20) && (d<30))?SENSOR_ON:SENSOR_OFF;
Pythia 1:1e448c750b63 76 }
Pythia 1:1e448c750b63 77 else
Pythia 1:1e448c750b63 78 {
Pythia 1:1e448c750b63 79 s0 = (sens_0 == 1) ? SENSOR_ON : SENSOR_OFF;
Pythia 1:1e448c750b63 80 s1 = (sens_1 == 1) ? SENSOR_ON : SENSOR_OFF;
Pythia 1:1e448c750b63 81 s2 = (sens_2 == 1) ? SENSOR_ON : SENSOR_OFF;
Pythia 1:1e448c750b63 82 }
Pythia 1:1e448c750b63 83
Pythia 1:1e448c750b63 84 if (SENSOR_OFF == s0)
Pythia 1:1e448c750b63 85 {
Pythia 1:1e448c750b63 86 s0_counter = 0;
Pythia 1:1e448c750b63 87 }
Pythia 1:1e448c750b63 88 else
Pythia 1:1e448c750b63 89 {
Pythia 1:1e448c750b63 90 if (s0_counter > SENSOR_LIMIT)
Pythia 1:1e448c750b63 91 {
Pythia 1:1e448c750b63 92 s0 = SENSOR_OFF;
Pythia 1:1e448c750b63 93 }
Pythia 1:1e448c750b63 94 else
Pythia 1:1e448c750b63 95 {
Pythia 1:1e448c750b63 96 s0_counter++;
Pythia 1:1e448c750b63 97 }
Pythia 1:1e448c750b63 98 }
Pythia 1:1e448c750b63 99
Pythia 1:1e448c750b63 100 if (SENSOR_OFF == s1)
Pythia 1:1e448c750b63 101 {
Pythia 1:1e448c750b63 102 s1_counter = 0;
Pythia 1:1e448c750b63 103 }
Pythia 1:1e448c750b63 104 else
Pythia 1:1e448c750b63 105 {
Pythia 1:1e448c750b63 106 if (s1_counter > SENSOR_LIMIT)
Pythia 1:1e448c750b63 107 {
Pythia 1:1e448c750b63 108 s1 = SENSOR_OFF;
Pythia 1:1e448c750b63 109 }
Pythia 1:1e448c750b63 110 else
Pythia 1:1e448c750b63 111 {
Pythia 1:1e448c750b63 112 s1_counter++;
Pythia 1:1e448c750b63 113 }
Pythia 1:1e448c750b63 114 }
Pythia 1:1e448c750b63 115
Pythia 1:1e448c750b63 116 if (SENSOR_OFF == s2)
Pythia 1:1e448c750b63 117 {
Pythia 1:1e448c750b63 118 s2_counter = 0;
Pythia 1:1e448c750b63 119 }
Pythia 1:1e448c750b63 120 else
Pythia 1:1e448c750b63 121 {
Pythia 1:1e448c750b63 122 if (s2_counter > SENSOR_LIMIT)
Pythia 1:1e448c750b63 123 {
Pythia 1:1e448c750b63 124 s2 = SENSOR_OFF;
Pythia 1:1e448c750b63 125 }
Pythia 1:1e448c750b63 126 else
Pythia 1:1e448c750b63 127 {
Pythia 1:1e448c750b63 128 s2_counter++;
Pythia 1:1e448c750b63 129 }
Pythia 1:1e448c750b63 130 }
Pythia 1:1e448c750b63 131
Pythia 1:1e448c750b63 132 if (d > 35)
Pythia 1:1e448c750b63 133 {
Pythia 1:1e448c750b63 134 sym = !sym;
Pythia 1:1e448c750b63 135 ThisThread::sleep_for(1000);
Pythia 1:1e448c750b63 136 }
Pythia 1:1e448c750b63 137
Pythia 1:1e448c750b63 138 if ((!sym) && (d>1) && (d<10))
Pythia 1:1e448c750b63 139 {
Pythia 1:1e448c750b63 140 prn = !prn;
Pythia 1:1e448c750b63 141 ThisThread::sleep_for(1000);
Pythia 1:1e448c750b63 142 }
Pythia 1:1e448c750b63 143 }
Pythia 1:1e448c750b63 144
Pythia 1:1e448c750b63 145 inline void action_once(void)
Pythia 1:1e448c750b63 146 {
Pythia 1:1e448c750b63 147 if (s0 == SENSOR_ON)
Pythia 1:1e448c750b63 148 {
Pythia 1:1e448c750b63 149 led_green.light();
Pythia 1:1e448c750b63 150 l1.light();
Pythia 1:1e448c750b63 151 }
Pythia 1:1e448c750b63 152 if (s1 == SENSOR_ON)
Pythia 1:1e448c750b63 153 {
Pythia 1:1e448c750b63 154 led_red.light();
Pythia 1:1e448c750b63 155 l1.light();
Pythia 1:1e448c750b63 156 l2.light();
Pythia 1:1e448c750b63 157 }
Pythia 1:1e448c750b63 158 if (s2 == SENSOR_ON)
Pythia 1:1e448c750b63 159 {
Pythia 1:1e448c750b63 160 led_blue.light();
Pythia 1:1e448c750b63 161 l2.light();
Pythia 1:1e448c750b63 162 }
Pythia 1:1e448c750b63 163 }
Pythia 1:1e448c750b63 164
Pythia 1:1e448c750b63 165 inline void print_once(void)
Pythia 1:1e448c750b63 166 {
Pythia 1:1e448c750b63 167 if (prn)
Pythia 1:1e448c750b63 168 {
Pythia 1:1e448c750b63 169 printf("%3.3s ", sym?"Sym":"IO ");
Pythia 1:1e448c750b63 170 printf("S0=%1d ", s0);
Pythia 1:1e448c750b63 171 printf("S1=%1d ", s1);
Pythia 1:1e448c750b63 172 printf("S2=%1d ", s2);
Pythia 1:1e448c750b63 173 printf("l1=%5d ", (int)l1.level());
Pythia 1:1e448c750b63 174 printf("l2=%5d ", (int)l2.level());
Pythia 1:1e448c750b63 175 printf("R=%5d ", (int)led_red.level());
Pythia 1:1e448c750b63 176 printf("G=%5d ", (int)led_green.level());
Pythia 1:1e448c750b63 177 printf("B=%5d ", (int)led_blue.level());
Pythia 1:1e448c750b63 178 printf("s:%5.2f ", p);
Pythia 1:1e448c750b63 179 printf("d:%2dmm ", d);
Pythia 1:1e448c750b63 180 printf("X: %1.2f, Y: %1.2f, Z: %1.2f\r\n", x, y, z);
Pythia 1:1e448c750b63 181 printf("\r\n");
Pythia 1:1e448c750b63 182 }
Pythia 1:1e448c750b63 183 }
Pythia 1:1e448c750b63 184
Pythia 1:1e448c750b63 185 void run_thread(void)
Pythia 1:1e448c750b63 186 {
Pythia 1:1e448c750b63 187 printf(" Started\r\n");
Pythia 1:1e448c750b63 188 while (true) {
Pythia 1:1e448c750b63 189 measure_once();
Pythia 1:1e448c750b63 190 sensors_once();
Pythia 1:1e448c750b63 191 action_once();
Pythia 1:1e448c750b63 192 print_once();
Pythia 1:1e448c750b63 193 ThisThread::sleep_for(500); //wait(1);
Pythia 1:1e448c750b63 194 }
Pythia 1:1e448c750b63 195 }
Pythia 0:225e4447fdd3 196
Pythia 0:225e4447fdd3 197 int main(void)
Pythia 0:225e4447fdd3 198 {
Pythia 1:1e448c750b63 199 printf("\nStarting...");
Pythia 0:225e4447fdd3 200
Pythia 1:1e448c750b63 201 thread_run.start(run_thread);
Pythia 0:225e4447fdd3 202
Pythia 0:225e4447fdd3 203 while (true)
Pythia 0:225e4447fdd3 204 {
Pythia 0:225e4447fdd3 205 l1.LED_run();
Pythia 0:225e4447fdd3 206 l2.LED_run();
Pythia 0:225e4447fdd3 207
Pythia 0:225e4447fdd3 208 led_green.LED_run();
Pythia 0:225e4447fdd3 209 led_red.LED_run();
Pythia 0:225e4447fdd3 210 led_blue.LED_run();
Pythia 1:1e448c750b63 211 ThisThread::yield();
Pythia 0:225e4447fdd3 212 }
Pythia 0:225e4447fdd3 213 }