J&W / Mbed OS frdm_LED_PWM

Dependencies:   tsi_sensor MMA8451Q

Committer:
Pythia
Date:
Sun Jun 14 20:35:55 2020 +0000
Revision:
3:9262b505d7a8
Parent:
1:1e448c750b63
Working version

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