![](/media/cache/profiles/IMG_0710-2.jpg.50x50_q85.jpg)
Light Show library for organic, calm, light display.
Dependencies: BLE_API mbed nRF51822
Fork of mbed_blinky by
main.cpp@20:fa23847fbfef, 2015-12-04 (annotated)
- Committer:
- nargetdev
- Date:
- Fri Dec 04 21:29:49 2015 +0000
- Revision:
- 20:fa23847fbfef
- Parent:
- 19:fabe9521b0a1
- Child:
- 21:3960e3b8ca7b
should be implimented -- untested
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nargetdev | 18:7c05ef6a639c | 1 | #include "mbed.h" |
nargetdev | 18:7c05ef6a639c | 2 | #include "BLE.h" |
nargetdev | 19:fabe9521b0a1 | 3 | #include "ButtonService.h" |
nargetdev | 19:fabe9521b0a1 | 4 | //#include "UARTService.h" |
nargetdev | 18:7c05ef6a639c | 5 | #include <string> |
nargetdev | 18:7c05ef6a639c | 6 | |
nargetdev | 19:fabe9521b0a1 | 7 | #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; |
nargetdev | 18:7c05ef6a639c | 8 | * it will have an impact on code-size and power consumption. */ |
nargetdev | 11:2d3cc5459d5b | 9 | |
nargetdev | 18:7c05ef6a639c | 10 | #if NEED_CONSOLE_OUTPUT |
nargetdev | 18:7c05ef6a639c | 11 | //#define DEBUG(...) { printf(__VA_ARGS__); } |
nargetdev | 18:7c05ef6a639c | 12 | #define DEBUG(STR) { if (uartServicePtr) uartServicePtr->write(STR, strlen(STR)); } |
nargetdev | 18:7c05ef6a639c | 13 | #else |
nargetdev | 18:7c05ef6a639c | 14 | #define DEBUG(...) /* nothing */ |
nargetdev | 18:7c05ef6a639c | 15 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
nargetdev | 18:7c05ef6a639c | 16 | |
nargetdev | 18:7c05ef6a639c | 17 | //#define NRFDK |
nargetdev | 18:7c05ef6a639c | 18 | #define MKIT |
nargetdev | 18:7c05ef6a639c | 19 | |
nargetdev | 19:fabe9521b0a1 | 20 | #define CALIBRATION_TIME 3 |
nargetdev | 11:2d3cc5459d5b | 21 | |
nargetdev | 19:fabe9521b0a1 | 22 | //#define HPI 1.571 |
nargetdev | 19:fabe9521b0a1 | 23 | //#define PI 3.1416 |
nargetdev | 19:fabe9521b0a1 | 24 | //#define HYSTERESIS_QUANTITY PI/4 |
nargetdev | 19:fabe9521b0a1 | 25 | // |
nargetdev | 19:fabe9521b0a1 | 26 | //#define RWAIT 0 |
nargetdev | 19:fabe9521b0a1 | 27 | //#define GWAIT PI/8 |
nargetdev | 19:fabe9521b0a1 | 28 | //#define BWAIT PI/4 |
nargetdev | 11:2d3cc5459d5b | 29 | |
nargetdev | 19:fabe9521b0a1 | 30 | |
nargetdev | 19:fabe9521b0a1 | 31 | //UARTService *uartServicePtr; |
nargetdev | 19:fabe9521b0a1 | 32 | const static char DEVICE_NAME[] = "Bathroom"; |
nargetdev | 19:fabe9521b0a1 | 33 | static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; |
nargetdev | 18:7c05ef6a639c | 34 | |
nargetdev | 19:fabe9521b0a1 | 35 | uint8_t motionState = 0; |
nargetdev | 19:fabe9521b0a1 | 36 | ButtonService *buttonServicePtr; |
nargetdev | 18:7c05ef6a639c | 37 | |
nargetdev | 19:fabe9521b0a1 | 38 | |
nargetdev | 19:fabe9521b0a1 | 39 | unsigned long seed = 1151; |
dan | 0:7dec7e9ac085 | 40 | |
nargetdev | 9:1bd0f7a2ee71 | 41 | typedef unsigned char byte; |
nargetdev | 9:1bd0f7a2ee71 | 42 | typedef unsigned int uint; |
nargetdev | 9:1bd0f7a2ee71 | 43 | |
nargetdev | 18:7c05ef6a639c | 44 | #ifdef NRFDK |
nargetdev | 18:7c05ef6a639c | 45 | InterruptIn motion(p20); |
nargetdev | 18:7c05ef6a639c | 46 | PwmOut red(p21); |
nargetdev | 18:7c05ef6a639c | 47 | PwmOut green(p22); |
nargetdev | 18:7c05ef6a639c | 48 | PwmOut blue(p23); |
nargetdev | 18:7c05ef6a639c | 49 | #endif |
nargetdev | 18:7c05ef6a639c | 50 | |
nargetdev | 18:7c05ef6a639c | 51 | #ifdef MKIT |
nargetdev | 9:1bd0f7a2ee71 | 52 | PwmOut red(p18); |
nargetdev | 9:1bd0f7a2ee71 | 53 | PwmOut green(p19); |
nargetdev | 9:1bd0f7a2ee71 | 54 | PwmOut blue(p20); |
nargetdev | 18:7c05ef6a639c | 55 | InterruptIn motion(p1); |
nargetdev | 18:7c05ef6a639c | 56 | #endif |
nargetdev | 18:7c05ef6a639c | 57 | |
nargetdev | 19:fabe9521b0a1 | 58 | // get some randomness |
nargetdev | 19:fabe9521b0a1 | 59 | Timer t; |
nargetdev | 9:1bd0f7a2ee71 | 60 | |
nargetdev | 9:1bd0f7a2ee71 | 61 | Serial pc(USBTX, USBRX); // tx, rx |
nargetdev | 9:1bd0f7a2ee71 | 62 | |
nargetdev | 18:7c05ef6a639c | 63 | |
nargetdev | 18:7c05ef6a639c | 64 | BLEDevice ble; |
nargetdev | 18:7c05ef6a639c | 65 | |
nargetdev | 18:7c05ef6a639c | 66 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
nargetdev | 18:7c05ef6a639c | 67 | { |
nargetdev | 18:7c05ef6a639c | 68 | DEBUG("Disconnected!\n\r"); |
nargetdev | 18:7c05ef6a639c | 69 | DEBUG("Restarting the advertising process\n\r"); |
nargetdev | 18:7c05ef6a639c | 70 | ble.startAdvertising(); |
nargetdev | 18:7c05ef6a639c | 71 | } |
nargetdev | 18:7c05ef6a639c | 72 | |
nargetdev | 18:7c05ef6a639c | 73 | |
nargetdev | 18:7c05ef6a639c | 74 | |
nargetdev | 9:1bd0f7a2ee71 | 75 | unsigned int hash(unsigned int x) { |
nargetdev | 9:1bd0f7a2ee71 | 76 | x = ((x >> 16) ^ x) * 0x45d9f3b; |
nargetdev | 9:1bd0f7a2ee71 | 77 | x = ((x >> 16) ^ x) * 0x45d9f3b; |
nargetdev | 9:1bd0f7a2ee71 | 78 | x = ((x >> 16) ^ x); |
nargetdev | 9:1bd0f7a2ee71 | 79 | seed*=2; |
nargetdev | 9:1bd0f7a2ee71 | 80 | seed+=17; |
nargetdev | 9:1bd0f7a2ee71 | 81 | return x%100; |
dan | 0:7dec7e9ac085 | 82 | } |
nargetdev | 8:5ed23e276c8a | 83 | |
nargetdev | 9:1bd0f7a2ee71 | 84 | void identify(unsigned int m){ |
nargetdev | 9:1bd0f7a2ee71 | 85 | unsigned int hashable; |
nargetdev | 9:1bd0f7a2ee71 | 86 | float write_me; |
nargetdev | 9:1bd0f7a2ee71 | 87 | |
nargetdev | 9:1bd0f7a2ee71 | 88 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 89 | write_me = hashable/100.0; |
nargetdev | 15:ee94af1f90a0 | 90 | red.write(hashable >= 50); |
nargetdev | 18:7c05ef6a639c | 91 | char* STR; |
nargetdev | 18:7c05ef6a639c | 92 | sprintf(STR, "r, g, b: %f\t", write_me); |
nargetdev | 18:7c05ef6a639c | 93 | DEBUG(STR); |
nargetdev | 9:1bd0f7a2ee71 | 94 | |
nargetdev | 9:1bd0f7a2ee71 | 95 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 96 | write_me = hashable/100.0; |
nargetdev | 15:ee94af1f90a0 | 97 | green.write(hashable >= 50); |
nargetdev | 9:1bd0f7a2ee71 | 98 | pc.printf("%f\t", write_me); |
nargetdev | 9:1bd0f7a2ee71 | 99 | |
nargetdev | 9:1bd0f7a2ee71 | 100 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 101 | write_me = hashable/100.0; |
nargetdev | 15:ee94af1f90a0 | 102 | blue.write(hashable >= 50); |
nargetdev | 9:1bd0f7a2ee71 | 103 | pc.printf("%f\n", write_me); |
nargetdev | 8:5ed23e276c8a | 104 | } |
nargetdev | 8:5ed23e276c8a | 105 | |
nargetdev | 9:1bd0f7a2ee71 | 106 | void calibrate(){ |
nargetdev | 9:1bd0f7a2ee71 | 107 | |
nargetdev | 9:1bd0f7a2ee71 | 108 | //give the sensor some time to calibrate |
nargetdev | 9:1bd0f7a2ee71 | 109 | pc.printf("calibrating sensor\n"); |
nargetdev | 19:fabe9521b0a1 | 110 | for(int i = 0; i < CALIBRATION_TIME; i++){ |
nargetdev | 9:1bd0f7a2ee71 | 111 | pc.printf("."); |
nargetdev | 19:fabe9521b0a1 | 112 | identify(CALIBRATION_TIME); |
nargetdev | 15:ee94af1f90a0 | 113 | wait(.5); |
nargetdev | 9:1bd0f7a2ee71 | 114 | } |
nargetdev | 18:7c05ef6a639c | 115 | DEBUG(" done\n"); |
nargetdev | 18:7c05ef6a639c | 116 | DEBUG("SENSOR ACTIVE\n"); |
nargetdev | 9:1bd0f7a2ee71 | 117 | wait(0.05); |
nargetdev | 9:1bd0f7a2ee71 | 118 | } |
nargetdev | 9:1bd0f7a2ee71 | 119 | |
nargetdev | 19:fabe9521b0a1 | 120 | void motionIRQ(){ |
nargetdev | 19:fabe9521b0a1 | 121 | motionState = 1; |
nargetdev | 19:fabe9521b0a1 | 122 | } |
nargetdev | 10:fc61583e9e8f | 123 | |
nargetdev | 19:fabe9521b0a1 | 124 | class Rgb { |
nargetdev | 19:fabe9521b0a1 | 125 | private: |
nargetdev | 19:fabe9521b0a1 | 126 | // PI macros |
nargetdev | 19:fabe9521b0a1 | 127 | static const float INCREMENT = 0.00628; |
nargetdev | 19:fabe9521b0a1 | 128 | static const float HPI = 1.571; |
nargetdev | 19:fabe9521b0a1 | 129 | static const float PI = 3.1416; |
nargetdev | 19:fabe9521b0a1 | 130 | static const float HYSTERESIS_QUANTITY = PI/4; |
nargetdev | 9:1bd0f7a2ee71 | 131 | |
nargetdev | 19:fabe9521b0a1 | 132 | static const float RWAIT = 0; |
nargetdev | 19:fabe9521b0a1 | 133 | static const float GWAIT = PI/8; |
nargetdev | 19:fabe9521b0a1 | 134 | static const float BWAIT = PI/4; |
nargetdev | 19:fabe9521b0a1 | 135 | float WAIT [3]; |
nargetdev | 19:fabe9521b0a1 | 136 | float SCALE [3]; |
nargetdev | 19:fabe9521b0a1 | 137 | |
nargetdev | 19:fabe9521b0a1 | 138 | // static const float SCALE[3] = { |
nargetdev | 19:fabe9521b0a1 | 139 | |
nargetdev | 19:fabe9521b0a1 | 140 | // channel operators |
nargetdev | 19:fabe9521b0a1 | 141 | float rgb_c[3]; |
nargetdev | 19:fabe9521b0a1 | 142 | float in; |
nargetdev | 19:fabe9521b0a1 | 143 | uint8_t rgb; |
nargetdev | 19:fabe9521b0a1 | 144 | float hysteresis; |
nargetdev | 19:fabe9521b0a1 | 145 | |
nargetdev | 19:fabe9521b0a1 | 146 | uint8_t i; |
nargetdev | 19:fabe9521b0a1 | 147 | bool mov; |
nargetdev | 19:fabe9521b0a1 | 148 | |
nargetdev | 19:fabe9521b0a1 | 149 | void random_init() { |
nargetdev | 20:fa23847fbfef | 150 | random_init(); |
nargetdev | 19:fabe9521b0a1 | 151 | float rand_seed = t.read(); |
nargetdev | 19:fabe9521b0a1 | 152 | pc.printf("%f\n", rand_seed); |
nargetdev | 19:fabe9521b0a1 | 153 | |
nargetdev | 20:fa23847fbfef | 154 | unsigned int seconds = time(NULL); |
nargetdev | 20:fa23847fbfef | 155 | pc.printf("Time as seconds since January 1, 1970 = %d\n", seconds); |
nargetdev | 20:fa23847fbfef | 156 | |
nargetdev | 20:fa23847fbfef | 157 | srand(seconds); |
nargetdev | 20:fa23847fbfef | 158 | pc.printf("A random %i", rand() ); |
nargetdev | 20:fa23847fbfef | 159 | |
nargetdev | 20:fa23847fbfef | 160 | // generate random values in 0.0 - 1.0 |
nargetdev | 20:fa23847fbfef | 161 | uint8_t blah; |
nargetdev | 20:fa23847fbfef | 162 | for (int j = 0; j < 3; j++) { |
nargetdev | 20:fa23847fbfef | 163 | srand(seconds+j); |
nargetdev | 20:fa23847fbfef | 164 | blah = rand(); |
nargetdev | 20:fa23847fbfef | 165 | SCALE[i] = (float) blah; |
nargetdev | 20:fa23847fbfef | 166 | SCALE[i] /= (float) 0xff; |
nargetdev | 20:fa23847fbfef | 167 | pc.printf("scale %d, %f\n", i, SCALE[i]); |
nargetdev | 20:fa23847fbfef | 168 | srand(blah+j % 17 + 7); |
nargetdev | 20:fa23847fbfef | 169 | blah = rand(); |
nargetdev | 20:fa23847fbfef | 170 | WAIT[i] = (float) blah; |
nargetdev | 20:fa23847fbfef | 171 | WAIT[i] /= (float) 0xff; |
nargetdev | 20:fa23847fbfef | 172 | pc.printf("scale %d, %f\n", i, WAIT[i]); |
nargetdev | 20:fa23847fbfef | 173 | } |
nargetdev | 8:5ed23e276c8a | 174 | } |
nargetdev | 19:fabe9521b0a1 | 175 | |
nargetdev | 19:fabe9521b0a1 | 176 | void update_rgb_values () { |
nargetdev | 19:fabe9521b0a1 | 177 | for (i=0; i<3; i++){ |
nargetdev | 19:fabe9521b0a1 | 178 | if (!(rgb & (0x1 << i) )){ |
nargetdev | 19:fabe9521b0a1 | 179 | if ( i > WAIT[i]){ |
nargetdev | 19:fabe9521b0a1 | 180 | rgb_c[i] = -cos((in - WAIT[i])*i*WAIT[i]) + 1; |
nargetdev | 19:fabe9521b0a1 | 181 | } |
nargetdev | 19:fabe9521b0a1 | 182 | else { |
nargetdev | 19:fabe9521b0a1 | 183 | rgb_c[i] = 0.0; |
nargetdev | 19:fabe9521b0a1 | 184 | } |
nargetdev | 19:fabe9521b0a1 | 185 | } |
nargetdev | 19:fabe9521b0a1 | 186 | else |
nargetdev | 19:fabe9521b0a1 | 187 | rgb_c[i] = 0.0; |
nargetdev | 19:fabe9521b0a1 | 188 | pc.printf("%f\t",rgb_c[i]); |
nargetdev | 19:fabe9521b0a1 | 189 | } |
nargetdev | 19:fabe9521b0a1 | 190 | pc.printf("\n"); |
nargetdev | 19:fabe9521b0a1 | 191 | } |
nargetdev | 19:fabe9521b0a1 | 192 | |
nargetdev | 19:fabe9521b0a1 | 193 | void write_rgb (){ |
nargetdev | 19:fabe9521b0a1 | 194 | red.write(rgb_c[0]/2.0); |
nargetdev | 19:fabe9521b0a1 | 195 | green.write(rgb_c[1]/2.0); |
nargetdev | 19:fabe9521b0a1 | 196 | blue.write(rgb_c[2]/2.0); |
nargetdev | 19:fabe9521b0a1 | 197 | } |
nargetdev | 19:fabe9521b0a1 | 198 | |
nargetdev | 19:fabe9521b0a1 | 199 | public: |
nargetdev | 19:fabe9521b0a1 | 200 | Rgb() |
nargetdev | 19:fabe9521b0a1 | 201 | { |
nargetdev | 19:fabe9521b0a1 | 202 | WAIT[0] = 1.3; |
nargetdev | 19:fabe9521b0a1 | 203 | WAIT[1] = 0.8; |
nargetdev | 19:fabe9521b0a1 | 204 | WAIT[2] = 2.1; |
nargetdev | 19:fabe9521b0a1 | 205 | } |
nargetdev | 18:7c05ef6a639c | 206 | |
nargetdev | 9:1bd0f7a2ee71 | 207 | |
nargetdev | 19:fabe9521b0a1 | 208 | void show() { |
nargetdev | 20:fa23847fbfef | 209 | // randomize the delay and scale values |
nargetdev | 19:fabe9521b0a1 | 210 | random_init(); |
nargetdev | 19:fabe9521b0a1 | 211 | for (in = 0; in < hysteresis || rgb != 0x7; in = in + INCREMENT){ |
nargetdev | 19:fabe9521b0a1 | 212 | #ifdef MKIT |
nargetdev | 19:fabe9521b0a1 | 213 | bool mov = motion; |
nargetdev | 19:fabe9521b0a1 | 214 | #else |
nargetdev | 19:fabe9521b0a1 | 215 | bool mov = !motion; |
nargetdev | 19:fabe9521b0a1 | 216 | #endif |
nargetdev | 19:fabe9521b0a1 | 217 | if (mov){ |
nargetdev | 19:fabe9521b0a1 | 218 | hysteresis = in + HYSTERESIS_QUANTITY; |
nargetdev | 19:fabe9521b0a1 | 219 | pc.printf("hyst ||"); |
nargetdev | 19:fabe9521b0a1 | 220 | } |
nargetdev | 19:fabe9521b0a1 | 221 | |
nargetdev | 19:fabe9521b0a1 | 222 | // update rgb |
nargetdev | 19:fabe9521b0a1 | 223 | update_rgb_values(); |
nargetdev | 19:fabe9521b0a1 | 224 | |
nargetdev | 19:fabe9521b0a1 | 225 | // write values |
nargetdev | 19:fabe9521b0a1 | 226 | write_rgb(); |
nargetdev | 10:fc61583e9e8f | 227 | |
nargetdev | 19:fabe9521b0a1 | 228 | |
nargetdev | 19:fabe9521b0a1 | 229 | if (in > hysteresis){ |
nargetdev | 19:fabe9521b0a1 | 230 | if (rgb_c[0] < 0.01) |
nargetdev | 19:fabe9521b0a1 | 231 | rgb |= 0x1; |
nargetdev | 19:fabe9521b0a1 | 232 | if (rgb_c[1] < 0.01) |
nargetdev | 19:fabe9521b0a1 | 233 | rgb |= 0x2; |
nargetdev | 19:fabe9521b0a1 | 234 | if (rgb_c[2] < 0.01) |
nargetdev | 19:fabe9521b0a1 | 235 | rgb |= 0x4; |
nargetdev | 19:fabe9521b0a1 | 236 | } |
nargetdev | 19:fabe9521b0a1 | 237 | |
nargetdev | 19:fabe9521b0a1 | 238 | wait(0.0001f); |
nargetdev | 19:fabe9521b0a1 | 239 | } |
nargetdev | 10:fc61583e9e8f | 240 | } |
nargetdev | 19:fabe9521b0a1 | 241 | }; |
nargetdev | 19:fabe9521b0a1 | 242 | //void show(){ |
nargetdev | 19:fabe9521b0a1 | 243 | // float in, rout, bout, gout; |
nargetdev | 19:fabe9521b0a1 | 244 | // bool tit = true; |
nargetdev | 19:fabe9521b0a1 | 245 | // |
nargetdev | 19:fabe9521b0a1 | 246 | // unsigned int rgb = 0x0; |
nargetdev | 19:fabe9521b0a1 | 247 | // pc.printf("showing...\n"); |
nargetdev | 19:fabe9521b0a1 | 248 | // |
nargetdev | 19:fabe9521b0a1 | 249 | // float hysteresis = 0; |
nargetdev | 19:fabe9521b0a1 | 250 | // |
nargetdev | 19:fabe9521b0a1 | 251 | // for (in = -HPI; in < hysteresis || rgb != 0x7; in = in + 0.00628) |
nargetdev | 19:fabe9521b0a1 | 252 | // { |
nargetdev | 19:fabe9521b0a1 | 253 | //#ifdef MKIT |
nargetdev | 19:fabe9521b0a1 | 254 | // bool i = motion; |
nargetdev | 19:fabe9521b0a1 | 255 | //#else |
nargetdev | 19:fabe9521b0a1 | 256 | // bool i = !motion; |
nargetdev | 19:fabe9521b0a1 | 257 | //#endif |
nargetdev | 19:fabe9521b0a1 | 258 | // if (i){ |
nargetdev | 19:fabe9521b0a1 | 259 | // hysteresis = in + HYSTERESIS_QUANTITY; |
nargetdev | 19:fabe9521b0a1 | 260 | // pc.printf("hyst ||"); |
nargetdev | 19:fabe9521b0a1 | 261 | // } |
nargetdev | 19:fabe9521b0a1 | 262 | // tit = !tit; |
nargetdev | 19:fabe9521b0a1 | 263 | // if (!(rgb & 0x1)){ |
nargetdev | 19:fabe9521b0a1 | 264 | // if (in < RWAIT - HPI) |
nargetdev | 19:fabe9521b0a1 | 265 | // rout = 0.0f; |
nargetdev | 19:fabe9521b0a1 | 266 | // else |
nargetdev | 19:fabe9521b0a1 | 267 | // rout = sin(in + 2.88*(in + HPI - RWAIT)) + 1.0; |
nargetdev | 19:fabe9521b0a1 | 268 | // } |
nargetdev | 19:fabe9521b0a1 | 269 | // else |
nargetdev | 19:fabe9521b0a1 | 270 | // rout = 0.0f; |
nargetdev | 19:fabe9521b0a1 | 271 | // if (!(rgb & 0x2)){ |
nargetdev | 19:fabe9521b0a1 | 272 | // if (in < GWAIT - HPI) |
nargetdev | 19:fabe9521b0a1 | 273 | // gout = 0.0f; |
nargetdev | 19:fabe9521b0a1 | 274 | // else |
nargetdev | 19:fabe9521b0a1 | 275 | // gout = sin(in + 2.61*(in+HPI - GWAIT) ) + 1.0; |
nargetdev | 19:fabe9521b0a1 | 276 | // } |
nargetdev | 19:fabe9521b0a1 | 277 | // else |
nargetdev | 19:fabe9521b0a1 | 278 | // gout = 0.0f; |
nargetdev | 19:fabe9521b0a1 | 279 | // if (!(rgb & 0x4)){ |
nargetdev | 19:fabe9521b0a1 | 280 | // if (in < BWAIT - HPI) |
nargetdev | 19:fabe9521b0a1 | 281 | // bout = 0.0f; |
nargetdev | 19:fabe9521b0a1 | 282 | // else |
nargetdev | 19:fabe9521b0a1 | 283 | // bout = sin(in + 3.92*(in+HPI - BWAIT) ) + 1.0; |
nargetdev | 19:fabe9521b0a1 | 284 | // } |
nargetdev | 19:fabe9521b0a1 | 285 | // else |
nargetdev | 19:fabe9521b0a1 | 286 | // bout = 0.0f; |
nargetdev | 19:fabe9521b0a1 | 287 | // |
nargetdev | 19:fabe9521b0a1 | 288 | // pc.printf("%f\t%f\t%f\n",rout,gout, bout); |
nargetdev | 19:fabe9521b0a1 | 289 | // |
nargetdev | 19:fabe9521b0a1 | 290 | // |
nargetdev | 19:fabe9521b0a1 | 291 | // red.write(rout/2.0); |
nargetdev | 19:fabe9521b0a1 | 292 | // green.write(gout/2.0); |
nargetdev | 19:fabe9521b0a1 | 293 | // blue.write(bout/2.0); |
nargetdev | 19:fabe9521b0a1 | 294 | // |
nargetdev | 19:fabe9521b0a1 | 295 | // if (in > hysteresis){ |
nargetdev | 19:fabe9521b0a1 | 296 | // if (rout < 0.01) |
nargetdev | 19:fabe9521b0a1 | 297 | // rgb |= 0x1; |
nargetdev | 19:fabe9521b0a1 | 298 | // if (gout < 0.01) |
nargetdev | 19:fabe9521b0a1 | 299 | // rgb |= 0x2; |
nargetdev | 19:fabe9521b0a1 | 300 | // if (bout < 0.01) |
nargetdev | 19:fabe9521b0a1 | 301 | // rgb |= 0x4; |
nargetdev | 19:fabe9521b0a1 | 302 | // } |
nargetdev | 19:fabe9521b0a1 | 303 | // wait(0.0001f); |
nargetdev | 19:fabe9521b0a1 | 304 | // } |
nargetdev | 19:fabe9521b0a1 | 305 | // pc.printf("\n%X\n", rgb); |
nargetdev | 19:fabe9521b0a1 | 306 | //} |
nargetdev | 9:1bd0f7a2ee71 | 307 | |
nargetdev | 14:e8a1fe69b048 | 308 | void channel_check(){ |
nargetdev | 14:e8a1fe69b048 | 309 | |
nargetdev | 14:e8a1fe69b048 | 310 | red.write(1.0f); |
nargetdev | 14:e8a1fe69b048 | 311 | wait(.5); |
nargetdev | 14:e8a1fe69b048 | 312 | red.write(0.0f); |
nargetdev | 14:e8a1fe69b048 | 313 | |
nargetdev | 14:e8a1fe69b048 | 314 | green.write(1.0f); |
nargetdev | 14:e8a1fe69b048 | 315 | wait(.5); |
nargetdev | 14:e8a1fe69b048 | 316 | green.write(0.0f); |
nargetdev | 14:e8a1fe69b048 | 317 | |
nargetdev | 14:e8a1fe69b048 | 318 | blue.write(1.0f); |
nargetdev | 14:e8a1fe69b048 | 319 | wait(.5); |
nargetdev | 14:e8a1fe69b048 | 320 | blue.write(0.0f); |
nargetdev | 14:e8a1fe69b048 | 321 | } |
nargetdev | 14:e8a1fe69b048 | 322 | |
nargetdev | 19:fabe9521b0a1 | 323 | |
nargetdev | 19:fabe9521b0a1 | 324 | |
nargetdev | 9:1bd0f7a2ee71 | 325 | int main() { |
nargetdev | 19:fabe9521b0a1 | 326 | // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
nargetdev | 19:fabe9521b0a1 | 327 | set_time(1256729737); |
nargetdev | 19:fabe9521b0a1 | 328 | |
nargetdev | 19:fabe9521b0a1 | 329 | t.start(); |
nargetdev | 18:7c05ef6a639c | 330 | red.period(0.01f); |
nargetdev | 18:7c05ef6a639c | 331 | green.period(0.01f); |
nargetdev | 18:7c05ef6a639c | 332 | blue.period(0.01f); |
nargetdev | 19:fabe9521b0a1 | 333 | identify(CALIBRATION_TIME); |
nargetdev | 19:fabe9521b0a1 | 334 | |
nargetdev | 19:fabe9521b0a1 | 335 | ble.init(); |
nargetdev | 19:fabe9521b0a1 | 336 | ble.gap().onDisconnection(disconnectionCallback); |
nargetdev | 19:fabe9521b0a1 | 337 | |
nargetdev | 19:fabe9521b0a1 | 338 | ButtonService buttonService(ble, false /* initial value for button pressed */); |
nargetdev | 19:fabe9521b0a1 | 339 | buttonServicePtr = &buttonService; |
nargetdev | 19:fabe9521b0a1 | 340 | |
nargetdev | 19:fabe9521b0a1 | 341 | /* setup advertising */ |
nargetdev | 19:fabe9521b0a1 | 342 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
nargetdev | 19:fabe9521b0a1 | 343 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
nargetdev | 19:fabe9521b0a1 | 344 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
nargetdev | 19:fabe9521b0a1 | 345 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
nargetdev | 19:fabe9521b0a1 | 346 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
nargetdev | 19:fabe9521b0a1 | 347 | ble.gap().startAdvertising(); |
nargetdev | 14:e8a1fe69b048 | 348 | |
nargetdev | 18:7c05ef6a639c | 349 | |
nargetdev | 19:fabe9521b0a1 | 350 | Rgb strip; |
nargetdev | 9:1bd0f7a2ee71 | 351 | while(1){ |
nargetdev | 18:7c05ef6a639c | 352 | #ifdef MKIT |
nargetdev | 19:fabe9521b0a1 | 353 | motion.rise(&motionIRQ); |
nargetdev | 19:fabe9521b0a1 | 354 | if (motionState){ |
nargetdev | 19:fabe9521b0a1 | 355 | buttonServicePtr->updateButtonState(motionState); |
nargetdev | 19:fabe9521b0a1 | 356 | strip.show(); |
nargetdev | 19:fabe9521b0a1 | 357 | motionState = 0; |
nargetdev | 19:fabe9521b0a1 | 358 | buttonServicePtr->updateButtonState(motionState); |
nargetdev | 19:fabe9521b0a1 | 359 | red.write(0.0f); |
nargetdev | 19:fabe9521b0a1 | 360 | green.write(0.0f); |
nargetdev | 19:fabe9521b0a1 | 361 | blue.write(0.0f); |
nargetdev | 19:fabe9521b0a1 | 362 | } |
nargetdev | 18:7c05ef6a639c | 363 | #endif |
nargetdev | 18:7c05ef6a639c | 364 | #ifdef NRFDK |
nargetdev | 19:fabe9521b0a1 | 365 | motion.fall(&strip.show()); |
nargetdev | 18:7c05ef6a639c | 366 | #endif |
nargetdev | 18:7c05ef6a639c | 367 | // red.write(0.0f); |
nargetdev | 18:7c05ef6a639c | 368 | // green.write(0.0f); |
nargetdev | 18:7c05ef6a639c | 369 | // blue.write(0.0f); |
nargetdev | 18:7c05ef6a639c | 370 | ble.waitForEvent(); |
nargetdev | 9:1bd0f7a2ee71 | 371 | } |
nargetdev | 8:5ed23e276c8a | 372 | } |