![](/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@22:b618d55e9c9b, 2016-01-29 (annotated)
- Committer:
- nargetdev
- Date:
- Fri Jan 29 19:39:07 2016 +0000
- Revision:
- 22:b618d55e9c9b
- Parent:
- 21:3960e3b8ca7b
- Child:
- 23:4bb74b53e112
pre separate color sinusoid module
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 | 21:3960e3b8ca7b | 7 | #define NEED_CONSOLE_OUTPUT 1 /* 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 | 21:3960e3b8ca7b | 11 | #define DEBUG(...) { printf(__VA_ARGS__); } |
nargetdev | 18:7c05ef6a639c | 12 | #else |
nargetdev | 21:3960e3b8ca7b | 13 | #define DEBUG(STR) { if (uartServicePtr) uartServicePtr->write(STR, strlen(STR)); } |
nargetdev | 21:3960e3b8ca7b | 14 | //#else |
nargetdev | 21:3960e3b8ca7b | 15 | //#define DEBUG(...) /* nothing */ |
nargetdev | 21:3960e3b8ca7b | 16 | #endif /* #if NEED_CONSOLE_OUxTPUT */ |
nargetdev | 18:7c05ef6a639c | 17 | |
nargetdev | 18:7c05ef6a639c | 18 | //#define NRFDK |
nargetdev | 18:7c05ef6a639c | 19 | #define MKIT |
nargetdev | 18:7c05ef6a639c | 20 | |
nargetdev | 19:fabe9521b0a1 | 21 | #define CALIBRATION_TIME 3 |
nargetdev | 11:2d3cc5459d5b | 22 | |
nargetdev | 19:fabe9521b0a1 | 23 | //#define HPI 1.571 |
nargetdev | 19:fabe9521b0a1 | 24 | //#define PI 3.1416 |
nargetdev | 19:fabe9521b0a1 | 25 | //#define HYSTERESIS_QUANTITY PI/4 |
nargetdev | 19:fabe9521b0a1 | 26 | // |
nargetdev | 19:fabe9521b0a1 | 27 | //#define RWAIT 0 |
nargetdev | 19:fabe9521b0a1 | 28 | //#define GWAIT PI/8 |
nargetdev | 19:fabe9521b0a1 | 29 | //#define BWAIT PI/4 |
nargetdev | 11:2d3cc5459d5b | 30 | |
nargetdev | 19:fabe9521b0a1 | 31 | |
nargetdev | 19:fabe9521b0a1 | 32 | //UARTService *uartServicePtr; |
nargetdev | 19:fabe9521b0a1 | 33 | const static char DEVICE_NAME[] = "Bathroom"; |
nargetdev | 19:fabe9521b0a1 | 34 | static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; |
nargetdev | 18:7c05ef6a639c | 35 | |
nargetdev | 19:fabe9521b0a1 | 36 | uint8_t motionState = 0; |
nargetdev | 19:fabe9521b0a1 | 37 | ButtonService *buttonServicePtr; |
nargetdev | 18:7c05ef6a639c | 38 | |
nargetdev | 19:fabe9521b0a1 | 39 | |
nargetdev | 21:3960e3b8ca7b | 40 | unsigned long seed = 151; |
dan | 0:7dec7e9ac085 | 41 | |
nargetdev | 9:1bd0f7a2ee71 | 42 | typedef unsigned char byte; |
nargetdev | 9:1bd0f7a2ee71 | 43 | typedef unsigned int uint; |
nargetdev | 9:1bd0f7a2ee71 | 44 | |
nargetdev | 18:7c05ef6a639c | 45 | #ifdef NRFDK |
nargetdev | 18:7c05ef6a639c | 46 | InterruptIn motion(p20); |
nargetdev | 18:7c05ef6a639c | 47 | PwmOut red(p21); |
nargetdev | 18:7c05ef6a639c | 48 | PwmOut green(p22); |
nargetdev | 18:7c05ef6a639c | 49 | PwmOut blue(p23); |
nargetdev | 18:7c05ef6a639c | 50 | #endif |
nargetdev | 18:7c05ef6a639c | 51 | |
nargetdev | 18:7c05ef6a639c | 52 | #ifdef MKIT |
nargetdev | 22:b618d55e9c9b | 53 | //PwmOut red(p18); |
nargetdev | 22:b618d55e9c9b | 54 | //PwmOut green(p19); |
nargetdev | 22:b618d55e9c9b | 55 | //PwmOut blue(p20); |
nargetdev | 22:b618d55e9c9b | 56 | PwmOut red(p6); |
nargetdev | 22:b618d55e9c9b | 57 | PwmOut green(p22); |
nargetdev | 22:b618d55e9c9b | 58 | PwmOut blue(p30); |
nargetdev | 18:7c05ef6a639c | 59 | InterruptIn motion(p1); |
nargetdev | 18:7c05ef6a639c | 60 | #endif |
nargetdev | 18:7c05ef6a639c | 61 | |
nargetdev | 19:fabe9521b0a1 | 62 | // get some randomness |
nargetdev | 19:fabe9521b0a1 | 63 | Timer t; |
nargetdev | 9:1bd0f7a2ee71 | 64 | |
nargetdev | 9:1bd0f7a2ee71 | 65 | Serial pc(USBTX, USBRX); // tx, rx |
nargetdev | 9:1bd0f7a2ee71 | 66 | |
nargetdev | 18:7c05ef6a639c | 67 | |
nargetdev | 18:7c05ef6a639c | 68 | BLEDevice ble; |
nargetdev | 18:7c05ef6a639c | 69 | |
nargetdev | 18:7c05ef6a639c | 70 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
nargetdev | 18:7c05ef6a639c | 71 | { |
nargetdev | 18:7c05ef6a639c | 72 | DEBUG("Disconnected!\n\r"); |
nargetdev | 18:7c05ef6a639c | 73 | DEBUG("Restarting the advertising process\n\r"); |
nargetdev | 18:7c05ef6a639c | 74 | ble.startAdvertising(); |
nargetdev | 18:7c05ef6a639c | 75 | } |
nargetdev | 18:7c05ef6a639c | 76 | |
nargetdev | 18:7c05ef6a639c | 77 | |
nargetdev | 18:7c05ef6a639c | 78 | |
nargetdev | 9:1bd0f7a2ee71 | 79 | unsigned int hash(unsigned int x) { |
nargetdev | 9:1bd0f7a2ee71 | 80 | x = ((x >> 16) ^ x) * 0x45d9f3b; |
nargetdev | 9:1bd0f7a2ee71 | 81 | x = ((x >> 16) ^ x) * 0x45d9f3b; |
nargetdev | 9:1bd0f7a2ee71 | 82 | x = ((x >> 16) ^ x); |
nargetdev | 9:1bd0f7a2ee71 | 83 | seed*=2; |
nargetdev | 9:1bd0f7a2ee71 | 84 | seed+=17; |
nargetdev | 9:1bd0f7a2ee71 | 85 | return x%100; |
dan | 0:7dec7e9ac085 | 86 | } |
nargetdev | 8:5ed23e276c8a | 87 | |
nargetdev | 21:3960e3b8ca7b | 88 | void zero_out() { |
nargetdev | 21:3960e3b8ca7b | 89 | red.write(0); |
nargetdev | 21:3960e3b8ca7b | 90 | green.write(0); |
nargetdev | 21:3960e3b8ca7b | 91 | blue.write(0); |
nargetdev | 21:3960e3b8ca7b | 92 | printf("zero_out()\r\n"); |
nargetdev | 21:3960e3b8ca7b | 93 | } |
nargetdev | 21:3960e3b8ca7b | 94 | |
nargetdev | 9:1bd0f7a2ee71 | 95 | void identify(unsigned int m){ |
nargetdev | 21:3960e3b8ca7b | 96 | DEBUG("IDENTIFYING as: "); |
nargetdev | 9:1bd0f7a2ee71 | 97 | unsigned int hashable; |
nargetdev | 9:1bd0f7a2ee71 | 98 | float write_me; |
nargetdev | 9:1bd0f7a2ee71 | 99 | |
nargetdev | 21:3960e3b8ca7b | 100 | int r, g, b; |
nargetdev | 21:3960e3b8ca7b | 101 | |
nargetdev | 9:1bd0f7a2ee71 | 102 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 103 | write_me = hashable/100.0; |
nargetdev | 21:3960e3b8ca7b | 104 | r = hashable >= 50; |
nargetdev | 21:3960e3b8ca7b | 105 | red.write(r); |
nargetdev | 9:1bd0f7a2ee71 | 106 | |
nargetdev | 9:1bd0f7a2ee71 | 107 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 108 | write_me = hashable/100.0; |
nargetdev | 21:3960e3b8ca7b | 109 | g = hashable >= 50; |
nargetdev | 21:3960e3b8ca7b | 110 | green.write(g); |
nargetdev | 9:1bd0f7a2ee71 | 111 | |
nargetdev | 9:1bd0f7a2ee71 | 112 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 113 | write_me = hashable/100.0; |
nargetdev | 21:3960e3b8ca7b | 114 | b = hashable >= 50; |
nargetdev | 21:3960e3b8ca7b | 115 | blue.write(b); |
nargetdev | 21:3960e3b8ca7b | 116 | |
nargetdev | 21:3960e3b8ca7b | 117 | char* STR; |
nargetdev | 21:3960e3b8ca7b | 118 | // sprintf(STR, "r, g, b: %f\t\r\n", write_me); |
nargetdev | 21:3960e3b8ca7b | 119 | // DEBUG(STR); |
nargetdev | 21:3960e3b8ca7b | 120 | DEBUG("%d%d%d\r\n",r,g,b); |
nargetdev | 8:5ed23e276c8a | 121 | } |
nargetdev | 8:5ed23e276c8a | 122 | |
nargetdev | 9:1bd0f7a2ee71 | 123 | void calibrate(){ |
nargetdev | 9:1bd0f7a2ee71 | 124 | |
nargetdev | 9:1bd0f7a2ee71 | 125 | //give the sensor some time to calibrate |
nargetdev | 21:3960e3b8ca7b | 126 | pc.printf("calibrating sensor\n\r"); |
nargetdev | 19:fabe9521b0a1 | 127 | for(int i = 0; i < CALIBRATION_TIME; i++){ |
nargetdev | 9:1bd0f7a2ee71 | 128 | pc.printf("."); |
nargetdev | 19:fabe9521b0a1 | 129 | identify(CALIBRATION_TIME); |
nargetdev | 15:ee94af1f90a0 | 130 | wait(.5); |
nargetdev | 9:1bd0f7a2ee71 | 131 | } |
nargetdev | 21:3960e3b8ca7b | 132 | DEBUG(" done\n\r"); |
nargetdev | 21:3960e3b8ca7b | 133 | zero_out(); |
nargetdev | 21:3960e3b8ca7b | 134 | |
nargetdev | 21:3960e3b8ca7b | 135 | DEBUG("SENSOR ACTIVE\n\r"); |
nargetdev | 9:1bd0f7a2ee71 | 136 | wait(0.05); |
nargetdev | 9:1bd0f7a2ee71 | 137 | } |
nargetdev | 9:1bd0f7a2ee71 | 138 | |
nargetdev | 19:fabe9521b0a1 | 139 | void motionIRQ(){ |
nargetdev | 19:fabe9521b0a1 | 140 | motionState = 1; |
nargetdev | 19:fabe9521b0a1 | 141 | } |
nargetdev | 10:fc61583e9e8f | 142 | |
nargetdev | 19:fabe9521b0a1 | 143 | class Rgb { |
nargetdev | 19:fabe9521b0a1 | 144 | private: |
nargetdev | 19:fabe9521b0a1 | 145 | // PI macros |
nargetdev | 21:3960e3b8ca7b | 146 | static const float INCREMENT = 0.00628*4; |
nargetdev | 19:fabe9521b0a1 | 147 | static const float HPI = 1.571; |
nargetdev | 19:fabe9521b0a1 | 148 | static const float PI = 3.1416; |
nargetdev | 19:fabe9521b0a1 | 149 | static const float HYSTERESIS_QUANTITY = PI/4; |
nargetdev | 9:1bd0f7a2ee71 | 150 | |
nargetdev | 19:fabe9521b0a1 | 151 | static const float RWAIT = 0; |
nargetdev | 19:fabe9521b0a1 | 152 | static const float GWAIT = PI/8; |
nargetdev | 19:fabe9521b0a1 | 153 | static const float BWAIT = PI/4; |
nargetdev | 19:fabe9521b0a1 | 154 | float WAIT [3]; |
nargetdev | 19:fabe9521b0a1 | 155 | float SCALE [3]; |
nargetdev | 19:fabe9521b0a1 | 156 | |
nargetdev | 19:fabe9521b0a1 | 157 | // static const float SCALE[3] = { |
nargetdev | 19:fabe9521b0a1 | 158 | |
nargetdev | 19:fabe9521b0a1 | 159 | // channel operators |
nargetdev | 19:fabe9521b0a1 | 160 | float rgb_c[3]; |
nargetdev | 19:fabe9521b0a1 | 161 | float in; |
nargetdev | 19:fabe9521b0a1 | 162 | uint8_t rgb; |
nargetdev | 19:fabe9521b0a1 | 163 | float hysteresis; |
nargetdev | 19:fabe9521b0a1 | 164 | |
nargetdev | 19:fabe9521b0a1 | 165 | uint8_t i; |
nargetdev | 19:fabe9521b0a1 | 166 | bool mov; |
nargetdev | 19:fabe9521b0a1 | 167 | |
nargetdev | 21:3960e3b8ca7b | 168 | void randomize_params() { |
nargetdev | 19:fabe9521b0a1 | 169 | float rand_seed = t.read(); |
nargetdev | 21:3960e3b8ca7b | 170 | pc.printf("%f\n\r", rand_seed); |
nargetdev | 21:3960e3b8ca7b | 171 | int rand_int = t.read() * 7919; |
nargetdev | 19:fabe9521b0a1 | 172 | |
nargetdev | 21:3960e3b8ca7b | 173 | time_t seconds = time(NULL); |
nargetdev | 21:3960e3b8ca7b | 174 | pc.printf("Time as seconds since January 1, 1970 = %d\n\r", seconds); |
nargetdev | 20:fa23847fbfef | 175 | |
nargetdev | 21:3960e3b8ca7b | 176 | srand(rand_int); |
nargetdev | 21:3960e3b8ca7b | 177 | pc.printf("A random %d\r\n", rand() ); |
nargetdev | 20:fa23847fbfef | 178 | |
nargetdev | 20:fa23847fbfef | 179 | // generate random values in 0.0 - 1.0 |
nargetdev | 20:fa23847fbfef | 180 | uint8_t blah; |
nargetdev | 20:fa23847fbfef | 181 | for (int j = 0; j < 3; j++) { |
nargetdev | 21:3960e3b8ca7b | 182 | srand(rand_int+j); |
nargetdev | 20:fa23847fbfef | 183 | blah = rand(); |
nargetdev | 21:3960e3b8ca7b | 184 | SCALE[j] = (float) blah; |
nargetdev | 21:3960e3b8ca7b | 185 | SCALE[j] /= (float) 0xff; |
nargetdev | 21:3960e3b8ca7b | 186 | SCALE[j] = SCALE[j]*HPI + 1; |
nargetdev | 21:3960e3b8ca7b | 187 | pc.printf("scale %d, %f\n\r", i, SCALE[i]); |
nargetdev | 20:fa23847fbfef | 188 | srand(blah+j % 17 + 7); |
nargetdev | 20:fa23847fbfef | 189 | blah = rand(); |
nargetdev | 21:3960e3b8ca7b | 190 | WAIT[j] = (float) blah; |
nargetdev | 21:3960e3b8ca7b | 191 | WAIT[j] /= (float) 0xff; |
nargetdev | 21:3960e3b8ca7b | 192 | WAIT[j] *= HPI; |
nargetdev | 21:3960e3b8ca7b | 193 | pc.printf("wait %d, %f\n\r", i, WAIT[i]); |
nargetdev | 20:fa23847fbfef | 194 | } |
nargetdev | 21:3960e3b8ca7b | 195 | rgb = 0x0; |
nargetdev | 21:3960e3b8ca7b | 196 | printf("Params Initialized\r\n"); |
nargetdev | 8:5ed23e276c8a | 197 | } |
nargetdev | 19:fabe9521b0a1 | 198 | |
nargetdev | 19:fabe9521b0a1 | 199 | void update_rgb_values () { |
nargetdev | 21:3960e3b8ca7b | 200 | // printf("Updating RGB values...\r\n"); |
nargetdev | 19:fabe9521b0a1 | 201 | for (i=0; i<3; i++){ |
nargetdev | 19:fabe9521b0a1 | 202 | if (!(rgb & (0x1 << i) )){ |
nargetdev | 21:3960e3b8ca7b | 203 | if ( in > WAIT[i]){ |
nargetdev | 21:3960e3b8ca7b | 204 | // printf("%d, %d, result: %d\r\n", rgb, (0x1 << i), (!(rgb & (0x1 << i)) ) ); |
nargetdev | 21:3960e3b8ca7b | 205 | rgb_c[i] = -cos((in - WAIT[i])*SCALE[i]) + 1; |
nargetdev | 19:fabe9521b0a1 | 206 | } |
nargetdev | 19:fabe9521b0a1 | 207 | else { |
nargetdev | 19:fabe9521b0a1 | 208 | rgb_c[i] = 0.0; |
nargetdev | 19:fabe9521b0a1 | 209 | } |
nargetdev | 19:fabe9521b0a1 | 210 | } |
nargetdev | 19:fabe9521b0a1 | 211 | else |
nargetdev | 19:fabe9521b0a1 | 212 | rgb_c[i] = 0.0; |
nargetdev | 19:fabe9521b0a1 | 213 | pc.printf("%f\t",rgb_c[i]); |
nargetdev | 19:fabe9521b0a1 | 214 | } |
nargetdev | 21:3960e3b8ca7b | 215 | pc.printf("\n\r"); |
nargetdev | 21:3960e3b8ca7b | 216 | // exit(0); |
nargetdev | 19:fabe9521b0a1 | 217 | } |
nargetdev | 19:fabe9521b0a1 | 218 | |
nargetdev | 19:fabe9521b0a1 | 219 | void write_rgb (){ |
nargetdev | 19:fabe9521b0a1 | 220 | red.write(rgb_c[0]/2.0); |
nargetdev | 19:fabe9521b0a1 | 221 | green.write(rgb_c[1]/2.0); |
nargetdev | 19:fabe9521b0a1 | 222 | blue.write(rgb_c[2]/2.0); |
nargetdev | 19:fabe9521b0a1 | 223 | } |
nargetdev | 19:fabe9521b0a1 | 224 | |
nargetdev | 19:fabe9521b0a1 | 225 | public: |
nargetdev | 21:3960e3b8ca7b | 226 | //Rgb(); |
nargetdev | 18:7c05ef6a639c | 227 | |
nargetdev | 9:1bd0f7a2ee71 | 228 | |
nargetdev | 19:fabe9521b0a1 | 229 | void show() { |
nargetdev | 21:3960e3b8ca7b | 230 | pc.printf("show\r\n"); |
nargetdev | 20:fa23847fbfef | 231 | // randomize the delay and scale values |
nargetdev | 21:3960e3b8ca7b | 232 | randomize_params(); |
nargetdev | 21:3960e3b8ca7b | 233 | printf("params initialized:\n\r"); |
nargetdev | 21:3960e3b8ca7b | 234 | printf("WAIT:\t%f\t%f\t%f\n\r", WAIT[0], WAIT[1], WAIT[2]); |
nargetdev | 21:3960e3b8ca7b | 235 | printf("SCALE:\t%f\t%f\t%f\n\r", SCALE[0], SCALE[1], SCALE[2]); |
nargetdev | 19:fabe9521b0a1 | 236 | for (in = 0; in < hysteresis || rgb != 0x7; in = in + INCREMENT){ |
nargetdev | 19:fabe9521b0a1 | 237 | #ifdef MKIT |
nargetdev | 19:fabe9521b0a1 | 238 | bool mov = motion; |
nargetdev | 19:fabe9521b0a1 | 239 | #else |
nargetdev | 19:fabe9521b0a1 | 240 | bool mov = !motion; |
nargetdev | 19:fabe9521b0a1 | 241 | #endif |
nargetdev | 19:fabe9521b0a1 | 242 | if (mov){ |
nargetdev | 19:fabe9521b0a1 | 243 | hysteresis = in + HYSTERESIS_QUANTITY; |
nargetdev | 19:fabe9521b0a1 | 244 | } |
nargetdev | 19:fabe9521b0a1 | 245 | |
nargetdev | 19:fabe9521b0a1 | 246 | // update rgb |
nargetdev | 19:fabe9521b0a1 | 247 | update_rgb_values(); |
nargetdev | 19:fabe9521b0a1 | 248 | |
nargetdev | 19:fabe9521b0a1 | 249 | // write values |
nargetdev | 19:fabe9521b0a1 | 250 | write_rgb(); |
nargetdev | 10:fc61583e9e8f | 251 | |
nargetdev | 19:fabe9521b0a1 | 252 | |
nargetdev | 19:fabe9521b0a1 | 253 | if (in > hysteresis){ |
nargetdev | 19:fabe9521b0a1 | 254 | if (rgb_c[0] < 0.01) |
nargetdev | 19:fabe9521b0a1 | 255 | rgb |= 0x1; |
nargetdev | 19:fabe9521b0a1 | 256 | if (rgb_c[1] < 0.01) |
nargetdev | 19:fabe9521b0a1 | 257 | rgb |= 0x2; |
nargetdev | 19:fabe9521b0a1 | 258 | if (rgb_c[2] < 0.01) |
nargetdev | 19:fabe9521b0a1 | 259 | rgb |= 0x4; |
nargetdev | 19:fabe9521b0a1 | 260 | } |
nargetdev | 19:fabe9521b0a1 | 261 | |
nargetdev | 21:3960e3b8ca7b | 262 | } |
nargetdev | 10:fc61583e9e8f | 263 | } |
nargetdev | 19:fabe9521b0a1 | 264 | }; |
nargetdev | 21:3960e3b8ca7b | 265 | |
nargetdev | 9:1bd0f7a2ee71 | 266 | |
nargetdev | 14:e8a1fe69b048 | 267 | void channel_check(){ |
nargetdev | 14:e8a1fe69b048 | 268 | |
nargetdev | 14:e8a1fe69b048 | 269 | red.write(1.0f); |
nargetdev | 14:e8a1fe69b048 | 270 | wait(.5); |
nargetdev | 14:e8a1fe69b048 | 271 | red.write(0.0f); |
nargetdev | 14:e8a1fe69b048 | 272 | |
nargetdev | 14:e8a1fe69b048 | 273 | green.write(1.0f); |
nargetdev | 14:e8a1fe69b048 | 274 | wait(.5); |
nargetdev | 14:e8a1fe69b048 | 275 | green.write(0.0f); |
nargetdev | 14:e8a1fe69b048 | 276 | |
nargetdev | 14:e8a1fe69b048 | 277 | blue.write(1.0f); |
nargetdev | 14:e8a1fe69b048 | 278 | wait(.5); |
nargetdev | 14:e8a1fe69b048 | 279 | blue.write(0.0f); |
nargetdev | 14:e8a1fe69b048 | 280 | } |
nargetdev | 14:e8a1fe69b048 | 281 | |
nargetdev | 19:fabe9521b0a1 | 282 | |
nargetdev | 19:fabe9521b0a1 | 283 | |
nargetdev | 9:1bd0f7a2ee71 | 284 | int main() { |
nargetdev | 21:3960e3b8ca7b | 285 | DEBUG("Start Main.\r\n"); |
nargetdev | 21:3960e3b8ca7b | 286 | |
nargetdev | 19:fabe9521b0a1 | 287 | // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
nargetdev | 19:fabe9521b0a1 | 288 | set_time(1256729737); |
nargetdev | 19:fabe9521b0a1 | 289 | t.start(); |
nargetdev | 18:7c05ef6a639c | 290 | red.period(0.01f); |
nargetdev | 18:7c05ef6a639c | 291 | green.period(0.01f); |
nargetdev | 18:7c05ef6a639c | 292 | blue.period(0.01f); |
nargetdev | 21:3960e3b8ca7b | 293 | |
nargetdev | 21:3960e3b8ca7b | 294 | |
nargetdev | 21:3960e3b8ca7b | 295 | calibrate(); |
nargetdev | 19:fabe9521b0a1 | 296 | |
nargetdev | 19:fabe9521b0a1 | 297 | ble.init(); |
nargetdev | 19:fabe9521b0a1 | 298 | ble.gap().onDisconnection(disconnectionCallback); |
nargetdev | 19:fabe9521b0a1 | 299 | |
nargetdev | 19:fabe9521b0a1 | 300 | ButtonService buttonService(ble, false /* initial value for button pressed */); |
nargetdev | 19:fabe9521b0a1 | 301 | buttonServicePtr = &buttonService; |
nargetdev | 19:fabe9521b0a1 | 302 | |
nargetdev | 19:fabe9521b0a1 | 303 | /* setup advertising */ |
nargetdev | 19:fabe9521b0a1 | 304 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
nargetdev | 19:fabe9521b0a1 | 305 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
nargetdev | 19:fabe9521b0a1 | 306 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
nargetdev | 19:fabe9521b0a1 | 307 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
nargetdev | 19:fabe9521b0a1 | 308 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
nargetdev | 19:fabe9521b0a1 | 309 | ble.gap().startAdvertising(); |
nargetdev | 14:e8a1fe69b048 | 310 | |
nargetdev | 21:3960e3b8ca7b | 311 | DEBUG("Instantiate strip.\r\n"); |
nargetdev | 21:3960e3b8ca7b | 312 | Rgb strip; |
nargetdev | 21:3960e3b8ca7b | 313 | DEBUG("I have a strip.\r\n"); |
nargetdev | 18:7c05ef6a639c | 314 | |
nargetdev | 9:1bd0f7a2ee71 | 315 | while(1){ |
nargetdev | 18:7c05ef6a639c | 316 | #ifdef MKIT |
nargetdev | 19:fabe9521b0a1 | 317 | motion.rise(&motionIRQ); |
nargetdev | 19:fabe9521b0a1 | 318 | if (motionState){ |
nargetdev | 21:3960e3b8ca7b | 319 | pc.printf("Motion detected.\r\n"); |
nargetdev | 19:fabe9521b0a1 | 320 | buttonServicePtr->updateButtonState(motionState); |
nargetdev | 19:fabe9521b0a1 | 321 | strip.show(); |
nargetdev | 19:fabe9521b0a1 | 322 | motionState = 0; |
nargetdev | 19:fabe9521b0a1 | 323 | buttonServicePtr->updateButtonState(motionState); |
nargetdev | 19:fabe9521b0a1 | 324 | red.write(0.0f); |
nargetdev | 19:fabe9521b0a1 | 325 | green.write(0.0f); |
nargetdev | 19:fabe9521b0a1 | 326 | blue.write(0.0f); |
nargetdev | 19:fabe9521b0a1 | 327 | } |
nargetdev | 18:7c05ef6a639c | 328 | #endif |
nargetdev | 18:7c05ef6a639c | 329 | #ifdef NRFDK |
nargetdev | 19:fabe9521b0a1 | 330 | motion.fall(&strip.show()); |
nargetdev | 18:7c05ef6a639c | 331 | #endif |
nargetdev | 18:7c05ef6a639c | 332 | // red.write(0.0f); |
nargetdev | 18:7c05ef6a639c | 333 | // green.write(0.0f); |
nargetdev | 18:7c05ef6a639c | 334 | // blue.write(0.0f); |
nargetdev | 18:7c05ef6a639c | 335 | ble.waitForEvent(); |
nargetdev | 9:1bd0f7a2ee71 | 336 | } |
nargetdev | 8:5ed23e276c8a | 337 | } |