![](/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@24:52319c0a14b8, 2016-01-30 (annotated)
- Committer:
- nargetdev
- Date:
- Sat Jan 30 20:07:55 2016 +0000
- Revision:
- 24:52319c0a14b8
- Parent:
- 23:4bb74b53e112
- Child:
- 25:d48f46d753fd
main test harness
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nargetdev | 24:52319c0a14b8 | 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 | 23:4bb74b53e112 | 7 | #include "rgb_led.h" |
nargetdev | 23:4bb74b53e112 | 8 | |
nargetdev | 24:52319c0a14b8 | 9 | #define MKIT |
nargetdev | 24:52319c0a14b8 | 10 | |
nargetdev | 24:52319c0a14b8 | 11 | #ifdef NRFDK |
nargetdev | 24:52319c0a14b8 | 12 | #define MOTION_PIN p20 |
nargetdev | 24:52319c0a14b8 | 13 | |
nargetdev | 24:52319c0a14b8 | 14 | #define RED_PIN p21 |
nargetdev | 24:52319c0a14b8 | 15 | #define GREEN_PIN p22 |
nargetdev | 24:52319c0a14b8 | 16 | #define BLUE_PIN p23 |
nargetdev | 24:52319c0a14b8 | 17 | #endif |
nargetdev | 24:52319c0a14b8 | 18 | |
nargetdev | 24:52319c0a14b8 | 19 | #ifdef MKIT |
nargetdev | 24:52319c0a14b8 | 20 | #define MOTION_PIN p1 |
nargetdev | 24:52319c0a14b8 | 21 | |
nargetdev | 24:52319c0a14b8 | 22 | #define RED_PIN p6 |
nargetdev | 24:52319c0a14b8 | 23 | #define GREEN_PIN p22 |
nargetdev | 24:52319c0a14b8 | 24 | #define BLUE_PIN p30 |
nargetdev | 24:52319c0a14b8 | 25 | #endif |
nargetdev | 24:52319c0a14b8 | 26 | |
nargetdev | 21:3960e3b8ca7b | 27 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
nargetdev | 24:52319c0a14b8 | 28 | * it will have an impact on code-size and power consumption. */ |
nargetdev | 11:2d3cc5459d5b | 29 | |
nargetdev | 18:7c05ef6a639c | 30 | #if NEED_CONSOLE_OUTPUT |
nargetdev | 21:3960e3b8ca7b | 31 | #define DEBUG(...) { printf(__VA_ARGS__); } |
nargetdev | 18:7c05ef6a639c | 32 | #else |
nargetdev | 21:3960e3b8ca7b | 33 | #define DEBUG(STR) { if (uartServicePtr) uartServicePtr->write(STR, strlen(STR)); } |
nargetdev | 21:3960e3b8ca7b | 34 | //#else |
nargetdev | 21:3960e3b8ca7b | 35 | //#define DEBUG(...) /* nothing */ |
nargetdev | 21:3960e3b8ca7b | 36 | #endif /* #if NEED_CONSOLE_OUxTPUT */ |
nargetdev | 18:7c05ef6a639c | 37 | |
nargetdev | 24:52319c0a14b8 | 38 | Serial pc(USBTX, USBRX); // tx, rx |
nargetdev | 18:7c05ef6a639c | 39 | |
nargetdev | 19:fabe9521b0a1 | 40 | #define CALIBRATION_TIME 3 |
nargetdev | 11:2d3cc5459d5b | 41 | |
nargetdev | 24:52319c0a14b8 | 42 | |
nargetdev | 24:52319c0a14b8 | 43 | Rgb strip(RED_PIN, GREEN_PIN, BLUE_PIN, &pc); |
nargetdev | 11:2d3cc5459d5b | 44 | |
nargetdev | 19:fabe9521b0a1 | 45 | |
nargetdev | 19:fabe9521b0a1 | 46 | //UARTService *uartServicePtr; |
nargetdev | 19:fabe9521b0a1 | 47 | const static char DEVICE_NAME[] = "Bathroom"; |
nargetdev | 19:fabe9521b0a1 | 48 | static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; |
nargetdev | 18:7c05ef6a639c | 49 | |
nargetdev | 19:fabe9521b0a1 | 50 | uint8_t motionState = 0; |
nargetdev | 19:fabe9521b0a1 | 51 | ButtonService *buttonServicePtr; |
nargetdev | 18:7c05ef6a639c | 52 | |
nargetdev | 19:fabe9521b0a1 | 53 | |
nargetdev | 21:3960e3b8ca7b | 54 | unsigned long seed = 151; |
dan | 0:7dec7e9ac085 | 55 | |
nargetdev | 24:52319c0a14b8 | 56 | typedef unsigned char byte; |
nargetdev | 24:52319c0a14b8 | 57 | typedef unsigned int uint; |
nargetdev | 24:52319c0a14b8 | 58 | |
nargetdev | 24:52319c0a14b8 | 59 | |
nargetdev | 18:7c05ef6a639c | 60 | |
nargetdev | 18:7c05ef6a639c | 61 | |
nargetdev | 24:52319c0a14b8 | 62 | InterruptIn motion(MOTION_PIN); |
nargetdev | 9:1bd0f7a2ee71 | 63 | |
nargetdev | 24:52319c0a14b8 | 64 | |
nargetdev | 9:1bd0f7a2ee71 | 65 | |
nargetdev | 18:7c05ef6a639c | 66 | |
nargetdev | 18:7c05ef6a639c | 67 | BLEDevice ble; |
nargetdev | 18:7c05ef6a639c | 68 | |
nargetdev | 18:7c05ef6a639c | 69 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
nargetdev | 18:7c05ef6a639c | 70 | { |
nargetdev | 18:7c05ef6a639c | 71 | DEBUG("Disconnected!\n\r"); |
nargetdev | 18:7c05ef6a639c | 72 | DEBUG("Restarting the advertising process\n\r"); |
nargetdev | 18:7c05ef6a639c | 73 | ble.startAdvertising(); |
nargetdev | 18:7c05ef6a639c | 74 | } |
nargetdev | 18:7c05ef6a639c | 75 | |
nargetdev | 18:7c05ef6a639c | 76 | |
nargetdev | 18:7c05ef6a639c | 77 | |
nargetdev | 24:52319c0a14b8 | 78 | unsigned int hash(unsigned int x) |
nargetdev | 24:52319c0a14b8 | 79 | { |
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 | 24:52319c0a14b8 | 88 | |
nargetdev | 21:3960e3b8ca7b | 89 | |
nargetdev | 24:52319c0a14b8 | 90 | void identify(unsigned int m) |
nargetdev | 24:52319c0a14b8 | 91 | { |
nargetdev | 21:3960e3b8ca7b | 92 | DEBUG("IDENTIFYING as: "); |
nargetdev | 9:1bd0f7a2ee71 | 93 | unsigned int hashable; |
nargetdev | 9:1bd0f7a2ee71 | 94 | float write_me; |
nargetdev | 24:52319c0a14b8 | 95 | |
nargetdev | 21:3960e3b8ca7b | 96 | int r, g, b; |
nargetdev | 24:52319c0a14b8 | 97 | |
nargetdev | 9:1bd0f7a2ee71 | 98 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 99 | write_me = hashable/100.0; |
nargetdev | 21:3960e3b8ca7b | 100 | r = hashable >= 50; |
nargetdev | 24:52319c0a14b8 | 101 | strip.write(RED, r); |
nargetdev | 9:1bd0f7a2ee71 | 102 | |
nargetdev | 9:1bd0f7a2ee71 | 103 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 104 | write_me = hashable/100.0; |
nargetdev | 21:3960e3b8ca7b | 105 | g = hashable >= 50; |
nargetdev | 24:52319c0a14b8 | 106 | strip.write(GREEN, g); |
nargetdev | 24:52319c0a14b8 | 107 | |
nargetdev | 9:1bd0f7a2ee71 | 108 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 109 | write_me = hashable/100.0; |
nargetdev | 21:3960e3b8ca7b | 110 | b = hashable >= 50; |
nargetdev | 24:52319c0a14b8 | 111 | strip.write(BLUE, b); |
nargetdev | 24:52319c0a14b8 | 112 | |
nargetdev | 21:3960e3b8ca7b | 113 | char* STR; |
nargetdev | 21:3960e3b8ca7b | 114 | // sprintf(STR, "r, g, b: %f\t\r\n", write_me); |
nargetdev | 21:3960e3b8ca7b | 115 | // DEBUG(STR); |
nargetdev | 21:3960e3b8ca7b | 116 | DEBUG("%d%d%d\r\n",r,g,b); |
nargetdev | 8:5ed23e276c8a | 117 | } |
nargetdev | 24:52319c0a14b8 | 118 | |
nargetdev | 24:52319c0a14b8 | 119 | void calibrate() |
nargetdev | 24:52319c0a14b8 | 120 | { |
nargetdev | 9:1bd0f7a2ee71 | 121 | |
nargetdev | 24:52319c0a14b8 | 122 | //give the sensor some time to calibrate |
nargetdev | 24:52319c0a14b8 | 123 | pc.printf("calibrating sensor\n\r"); |
nargetdev | 24:52319c0a14b8 | 124 | for(int i = 0; i < CALIBRATION_TIME; i++) { |
nargetdev | 24:52319c0a14b8 | 125 | pc.printf("."); |
nargetdev | 24:52319c0a14b8 | 126 | identify(CALIBRATION_TIME); |
nargetdev | 24:52319c0a14b8 | 127 | wait(.5); |
nargetdev | 24:52319c0a14b8 | 128 | } |
nargetdev | 21:3960e3b8ca7b | 129 | DEBUG(" done\n\r"); |
nargetdev | 24:52319c0a14b8 | 130 | strip.quiet(); |
nargetdev | 21:3960e3b8ca7b | 131 | |
nargetdev | 21:3960e3b8ca7b | 132 | DEBUG("SENSOR ACTIVE\n\r"); |
nargetdev | 9:1bd0f7a2ee71 | 133 | wait(0.05); |
nargetdev | 9:1bd0f7a2ee71 | 134 | } |
nargetdev | 9:1bd0f7a2ee71 | 135 | |
nargetdev | 24:52319c0a14b8 | 136 | void motionIRQ() |
nargetdev | 24:52319c0a14b8 | 137 | { |
nargetdev | 19:fabe9521b0a1 | 138 | motionState = 1; |
nargetdev | 19:fabe9521b0a1 | 139 | } |
nargetdev | 10:fc61583e9e8f | 140 | |
nargetdev | 9:1bd0f7a2ee71 | 141 | |
nargetdev | 14:e8a1fe69b048 | 142 | |
nargetdev | 24:52319c0a14b8 | 143 | void show() |
nargetdev | 24:52319c0a14b8 | 144 | { |
nargetdev | 24:52319c0a14b8 | 145 | printf("show\r\n"); |
nargetdev | 24:52319c0a14b8 | 146 | // randomize the delay and scale values |
nargetdev | 24:52319c0a14b8 | 147 | strip.randomize_params(); |
nargetdev | 24:52319c0a14b8 | 148 | printf("params initialized:\n\r"); |
nargetdev | 24:52319c0a14b8 | 149 | pc.printf("WAIT:\t%f\t%f\t%f\n\r", WAIT[0], WAIT[1], WAIT[2]); |
nargetdev | 24:52319c0a14b8 | 150 | pc.printf("SCALE:\t%f\t%f\t%f\n\r", SCALE[0], SCALE[1], SCALE[2]); |
nargetdev | 24:52319c0a14b8 | 151 | for (in = 0; in < hysteresis || rgb != 0x7; in = in + INCREMENT) { |
nargetdev | 24:52319c0a14b8 | 152 | #ifdef MKIT |
nargetdev | 24:52319c0a14b8 | 153 | bool mov = motion; |
nargetdev | 24:52319c0a14b8 | 154 | #else |
nargetdev | 24:52319c0a14b8 | 155 | bool mov = !motion; |
nargetdev | 24:52319c0a14b8 | 156 | #endif |
nargetdev | 24:52319c0a14b8 | 157 | if (mov) { |
nargetdev | 24:52319c0a14b8 | 158 | strip.hysteresis = in + HYSTERESIS_QUANTITY; |
nargetdev | 24:52319c0a14b8 | 159 | } |
nargetdev | 24:52319c0a14b8 | 160 | |
nargetdev | 24:52319c0a14b8 | 161 | // update rgb |
nargetdev | 24:52319c0a14b8 | 162 | strip.update_rgb_values(); |
nargetdev | 24:52319c0a14b8 | 163 | |
nargetdev | 24:52319c0a14b8 | 164 | // write values |
nargetdev | 24:52319c0a14b8 | 165 | strip.write_rgb(); |
nargetdev | 24:52319c0a14b8 | 166 | |
nargetdev | 24:52319c0a14b8 | 167 | |
nargetdev | 24:52319c0a14b8 | 168 | if (in > hysteresis) { |
nargetdev | 24:52319c0a14b8 | 169 | if (rgb_c[0] < 0.01) |
nargetdev | 24:52319c0a14b8 | 170 | rgb |= 0x1; |
nargetdev | 24:52319c0a14b8 | 171 | if (rgb_c[1] < 0.01) |
nargetdev | 24:52319c0a14b8 | 172 | rgb |= 0x2; |
nargetdev | 24:52319c0a14b8 | 173 | if (rgb_c[2] < 0.01) |
nargetdev | 24:52319c0a14b8 | 174 | rgb |= 0x4; |
nargetdev | 24:52319c0a14b8 | 175 | } |
nargetdev | 24:52319c0a14b8 | 176 | } |
nargetdev | 24:52319c0a14b8 | 177 | } |
nargetdev | 24:52319c0a14b8 | 178 | |
nargetdev | 24:52319c0a14b8 | 179 | |
nargetdev | 24:52319c0a14b8 | 180 | |
nargetdev | 19:fabe9521b0a1 | 181 | |
nargetdev | 24:52319c0a14b8 | 182 | int main() |
nargetdev | 24:52319c0a14b8 | 183 | { |
nargetdev | 21:3960e3b8ca7b | 184 | DEBUG("Start Main.\r\n"); |
nargetdev | 24:52319c0a14b8 | 185 | |
nargetdev | 21:3960e3b8ca7b | 186 | |
nargetdev | 21:3960e3b8ca7b | 187 | |
nargetdev | 21:3960e3b8ca7b | 188 | calibrate(); |
nargetdev | 19:fabe9521b0a1 | 189 | |
nargetdev | 19:fabe9521b0a1 | 190 | ble.init(); |
nargetdev | 19:fabe9521b0a1 | 191 | ble.gap().onDisconnection(disconnectionCallback); |
nargetdev | 19:fabe9521b0a1 | 192 | |
nargetdev | 19:fabe9521b0a1 | 193 | ButtonService buttonService(ble, false /* initial value for button pressed */); |
nargetdev | 19:fabe9521b0a1 | 194 | buttonServicePtr = &buttonService; |
nargetdev | 19:fabe9521b0a1 | 195 | |
nargetdev | 19:fabe9521b0a1 | 196 | /* setup advertising */ |
nargetdev | 19:fabe9521b0a1 | 197 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
nargetdev | 19:fabe9521b0a1 | 198 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
nargetdev | 19:fabe9521b0a1 | 199 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
nargetdev | 19:fabe9521b0a1 | 200 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
nargetdev | 19:fabe9521b0a1 | 201 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
nargetdev | 19:fabe9521b0a1 | 202 | ble.gap().startAdvertising(); |
nargetdev | 24:52319c0a14b8 | 203 | |
nargetdev | 18:7c05ef6a639c | 204 | |
nargetdev | 24:52319c0a14b8 | 205 | |
nargetdev | 24:52319c0a14b8 | 206 | while(1) { |
nargetdev | 18:7c05ef6a639c | 207 | #ifdef MKIT |
nargetdev | 19:fabe9521b0a1 | 208 | motion.rise(&motionIRQ); |
nargetdev | 24:52319c0a14b8 | 209 | if (motionState) { |
nargetdev | 21:3960e3b8ca7b | 210 | pc.printf("Motion detected.\r\n"); |
nargetdev | 19:fabe9521b0a1 | 211 | buttonServicePtr->updateButtonState(motionState); |
nargetdev | 24:52319c0a14b8 | 212 | show(); |
nargetdev | 19:fabe9521b0a1 | 213 | motionState = 0; |
nargetdev | 19:fabe9521b0a1 | 214 | buttonServicePtr->updateButtonState(motionState); |
nargetdev | 24:52319c0a14b8 | 215 | strip.quiet(); |
nargetdev | 19:fabe9521b0a1 | 216 | } |
nargetdev | 18:7c05ef6a639c | 217 | #endif |
nargetdev | 18:7c05ef6a639c | 218 | #ifdef NRFDK |
nargetdev | 19:fabe9521b0a1 | 219 | motion.fall(&strip.show()); |
nargetdev | 18:7c05ef6a639c | 220 | #endif |
nargetdev | 24:52319c0a14b8 | 221 | |
nargetdev | 18:7c05ef6a639c | 222 | ble.waitForEvent(); |
nargetdev | 9:1bd0f7a2ee71 | 223 | } |
nargetdev | 8:5ed23e276c8a | 224 | } |