![](/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@18:7c05ef6a639c, 2015-10-30 (annotated)
- Committer:
- nargetdev
- Date:
- Fri Oct 30 02:41:18 2015 +0000
- Revision:
- 18:7c05ef6a639c
- Parent:
- 17:76c74851442f
- Child:
- 19:fabe9521b0a1
This does not work - wierd BLE stuff.
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 | 18:7c05ef6a639c | 3 | #include "UARTService.h" |
nargetdev | 18:7c05ef6a639c | 4 | #include <string> |
nargetdev | 18:7c05ef6a639c | 5 | |
nargetdev | 18:7c05ef6a639c | 6 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
nargetdev | 18:7c05ef6a639c | 7 | * it will have an impact on code-size and power consumption. */ |
nargetdev | 11:2d3cc5459d5b | 8 | |
nargetdev | 18:7c05ef6a639c | 9 | #if NEED_CONSOLE_OUTPUT |
nargetdev | 18:7c05ef6a639c | 10 | //#define DEBUG(...) { printf(__VA_ARGS__); } |
nargetdev | 18:7c05ef6a639c | 11 | #define DEBUG(STR) { if (uartServicePtr) uartServicePtr->write(STR, strlen(STR)); } |
nargetdev | 18:7c05ef6a639c | 12 | #else |
nargetdev | 18:7c05ef6a639c | 13 | #define DEBUG(...) /* nothing */ |
nargetdev | 18:7c05ef6a639c | 14 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
nargetdev | 18:7c05ef6a639c | 15 | |
nargetdev | 18:7c05ef6a639c | 16 | //#define NRFDK |
nargetdev | 18:7c05ef6a639c | 17 | #define MKIT |
nargetdev | 18:7c05ef6a639c | 18 | |
nargetdev | 18:7c05ef6a639c | 19 | #define calibrationTime 2 |
nargetdev | 11:2d3cc5459d5b | 20 | |
nargetdev | 11:2d3cc5459d5b | 21 | #define HPI 1.571 |
nargetdev | 11:2d3cc5459d5b | 22 | #define PI 3.1416 |
nargetdev | 18:7c05ef6a639c | 23 | #define HYSTERESIS_QUANTITY PI/4 |
nargetdev | 11:2d3cc5459d5b | 24 | |
nargetdev | 18:7c05ef6a639c | 25 | #define RWAIT 0 |
nargetdev | 18:7c05ef6a639c | 26 | #define GWAIT 0 |
nargetdev | 18:7c05ef6a639c | 27 | #define BWAIT 0 |
nargetdev | 18:7c05ef6a639c | 28 | |
nargetdev | 18:7c05ef6a639c | 29 | UARTService *uartServicePtr; |
nargetdev | 18:7c05ef6a639c | 30 | |
nargetdev | 18:7c05ef6a639c | 31 | unsigned long seed = 113; |
dan | 0:7dec7e9ac085 | 32 | |
nargetdev | 9:1bd0f7a2ee71 | 33 | typedef unsigned char byte; |
nargetdev | 9:1bd0f7a2ee71 | 34 | typedef unsigned int uint; |
nargetdev | 9:1bd0f7a2ee71 | 35 | |
nargetdev | 18:7c05ef6a639c | 36 | #ifdef NRFDK |
nargetdev | 18:7c05ef6a639c | 37 | InterruptIn motion(p20); |
nargetdev | 18:7c05ef6a639c | 38 | PwmOut red(p21); |
nargetdev | 18:7c05ef6a639c | 39 | PwmOut green(p22); |
nargetdev | 18:7c05ef6a639c | 40 | PwmOut blue(p23); |
nargetdev | 18:7c05ef6a639c | 41 | #endif |
nargetdev | 18:7c05ef6a639c | 42 | |
nargetdev | 18:7c05ef6a639c | 43 | #ifdef MKIT |
nargetdev | 9:1bd0f7a2ee71 | 44 | PwmOut red(p18); |
nargetdev | 9:1bd0f7a2ee71 | 45 | PwmOut green(p19); |
nargetdev | 9:1bd0f7a2ee71 | 46 | PwmOut blue(p20); |
nargetdev | 18:7c05ef6a639c | 47 | InterruptIn motion(p1); |
nargetdev | 18:7c05ef6a639c | 48 | #endif |
nargetdev | 18:7c05ef6a639c | 49 | |
nargetdev | 9:1bd0f7a2ee71 | 50 | |
nargetdev | 9:1bd0f7a2ee71 | 51 | Serial pc(USBTX, USBRX); // tx, rx |
nargetdev | 9:1bd0f7a2ee71 | 52 | |
nargetdev | 18:7c05ef6a639c | 53 | |
nargetdev | 18:7c05ef6a639c | 54 | BLEDevice ble; |
nargetdev | 18:7c05ef6a639c | 55 | |
nargetdev | 18:7c05ef6a639c | 56 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
nargetdev | 18:7c05ef6a639c | 57 | { |
nargetdev | 18:7c05ef6a639c | 58 | DEBUG("Disconnected!\n\r"); |
nargetdev | 18:7c05ef6a639c | 59 | DEBUG("Restarting the advertising process\n\r"); |
nargetdev | 18:7c05ef6a639c | 60 | ble.startAdvertising(); |
nargetdev | 18:7c05ef6a639c | 61 | } |
nargetdev | 18:7c05ef6a639c | 62 | |
nargetdev | 18:7c05ef6a639c | 63 | |
nargetdev | 18:7c05ef6a639c | 64 | |
nargetdev | 9:1bd0f7a2ee71 | 65 | unsigned int hash(unsigned int x) { |
nargetdev | 9:1bd0f7a2ee71 | 66 | x = ((x >> 16) ^ x) * 0x45d9f3b; |
nargetdev | 9:1bd0f7a2ee71 | 67 | x = ((x >> 16) ^ x) * 0x45d9f3b; |
nargetdev | 9:1bd0f7a2ee71 | 68 | x = ((x >> 16) ^ x); |
nargetdev | 9:1bd0f7a2ee71 | 69 | seed*=2; |
nargetdev | 9:1bd0f7a2ee71 | 70 | seed+=17; |
nargetdev | 9:1bd0f7a2ee71 | 71 | return x%100; |
dan | 0:7dec7e9ac085 | 72 | } |
nargetdev | 8:5ed23e276c8a | 73 | |
nargetdev | 9:1bd0f7a2ee71 | 74 | void identify(unsigned int m){ |
nargetdev | 9:1bd0f7a2ee71 | 75 | unsigned int hashable; |
nargetdev | 9:1bd0f7a2ee71 | 76 | float write_me; |
nargetdev | 9:1bd0f7a2ee71 | 77 | |
nargetdev | 9:1bd0f7a2ee71 | 78 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 79 | write_me = hashable/100.0; |
nargetdev | 15:ee94af1f90a0 | 80 | red.write(hashable >= 50); |
nargetdev | 18:7c05ef6a639c | 81 | char* STR; |
nargetdev | 18:7c05ef6a639c | 82 | sprintf(STR, "r, g, b: %f\t", write_me); |
nargetdev | 18:7c05ef6a639c | 83 | DEBUG(STR); |
nargetdev | 9:1bd0f7a2ee71 | 84 | |
nargetdev | 9:1bd0f7a2ee71 | 85 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 86 | write_me = hashable/100.0; |
nargetdev | 15:ee94af1f90a0 | 87 | green.write(hashable >= 50); |
nargetdev | 9:1bd0f7a2ee71 | 88 | pc.printf("%f\t", write_me); |
nargetdev | 9:1bd0f7a2ee71 | 89 | |
nargetdev | 9:1bd0f7a2ee71 | 90 | hashable = hash(m + seed); |
nargetdev | 9:1bd0f7a2ee71 | 91 | write_me = hashable/100.0; |
nargetdev | 15:ee94af1f90a0 | 92 | blue.write(hashable >= 50); |
nargetdev | 9:1bd0f7a2ee71 | 93 | pc.printf("%f\n", write_me); |
nargetdev | 8:5ed23e276c8a | 94 | } |
nargetdev | 8:5ed23e276c8a | 95 | |
nargetdev | 9:1bd0f7a2ee71 | 96 | void calibrate(){ |
nargetdev | 9:1bd0f7a2ee71 | 97 | |
nargetdev | 9:1bd0f7a2ee71 | 98 | //give the sensor some time to calibrate |
nargetdev | 9:1bd0f7a2ee71 | 99 | pc.printf("calibrating sensor\n"); |
nargetdev | 9:1bd0f7a2ee71 | 100 | for(int i = 0; i < calibrationTime; i++){ |
nargetdev | 9:1bd0f7a2ee71 | 101 | pc.printf("."); |
nargetdev | 9:1bd0f7a2ee71 | 102 | identify(calibrationTime); |
nargetdev | 15:ee94af1f90a0 | 103 | wait(.5); |
nargetdev | 9:1bd0f7a2ee71 | 104 | } |
nargetdev | 18:7c05ef6a639c | 105 | DEBUG(" done\n"); |
nargetdev | 18:7c05ef6a639c | 106 | DEBUG("SENSOR ACTIVE\n"); |
nargetdev | 9:1bd0f7a2ee71 | 107 | wait(0.05); |
nargetdev | 9:1bd0f7a2ee71 | 108 | } |
nargetdev | 9:1bd0f7a2ee71 | 109 | |
nargetdev | 9:1bd0f7a2ee71 | 110 | void show(){ |
nargetdev | 9:1bd0f7a2ee71 | 111 | float in, rout, bout, gout; |
nargetdev | 9:1bd0f7a2ee71 | 112 | bool tit = true; |
nargetdev | 10:fc61583e9e8f | 113 | |
nargetdev | 10:fc61583e9e8f | 114 | unsigned int rgb = 0x0; |
nargetdev | 9:1bd0f7a2ee71 | 115 | pc.printf("showing...\n"); |
nargetdev | 8:5ed23e276c8a | 116 | |
nargetdev | 9:1bd0f7a2ee71 | 117 | float hysteresis = 0; |
nargetdev | 9:1bd0f7a2ee71 | 118 | |
nargetdev | 11:2d3cc5459d5b | 119 | for (in = -HPI; in < hysteresis || rgb != 0x7; in = in + 0.00628) |
nargetdev | 9:1bd0f7a2ee71 | 120 | { |
nargetdev | 18:7c05ef6a639c | 121 | #ifdef MKIT |
nargetdev | 18:7c05ef6a639c | 122 | bool i = motion; |
nargetdev | 18:7c05ef6a639c | 123 | #else |
nargetdev | 18:7c05ef6a639c | 124 | bool i = !motion; |
nargetdev | 18:7c05ef6a639c | 125 | #endif |
nargetdev | 18:7c05ef6a639c | 126 | if (i){ |
nargetdev | 10:fc61583e9e8f | 127 | hysteresis = in + HYSTERESIS_QUANTITY; |
nargetdev | 11:2d3cc5459d5b | 128 | pc.printf("hyst ||"); |
nargetdev | 8:5ed23e276c8a | 129 | } |
nargetdev | 9:1bd0f7a2ee71 | 130 | tit = !tit; |
nargetdev | 10:fc61583e9e8f | 131 | if (!(rgb & 0x1)) |
nargetdev | 13:ed92d45dc9ab | 132 | rout = sin(in + 2.88*(in + HPI)) + 1.0; |
nargetdev | 10:fc61583e9e8f | 133 | else |
nargetdev | 10:fc61583e9e8f | 134 | rout = 0.0f; |
nargetdev | 10:fc61583e9e8f | 135 | if (!(rgb & 0x2)) |
nargetdev | 13:ed92d45dc9ab | 136 | gout = sin(in + 2.61*(in+HPI) ) + 1.0; |
nargetdev | 13:ed92d45dc9ab | 137 | else |
nargetdev | 13:ed92d45dc9ab | 138 | gout = 0.0f; |
nargetdev | 13:ed92d45dc9ab | 139 | if (!(rgb & 0x4)) |
nargetdev | 13:ed92d45dc9ab | 140 | bout = sin(in + 3.92*(in+HPI) ) + 1.0; |
nargetdev | 10:fc61583e9e8f | 141 | else |
nargetdev | 10:fc61583e9e8f | 142 | bout = 0.0f; |
nargetdev | 10:fc61583e9e8f | 143 | |
nargetdev | 18:7c05ef6a639c | 144 | pc.printf("%f\t%f\t%f\n",rout,gout, bout); |
nargetdev | 18:7c05ef6a639c | 145 | |
nargetdev | 9:1bd0f7a2ee71 | 146 | |
nargetdev | 10:fc61583e9e8f | 147 | red.write(rout/2.0); |
nargetdev | 10:fc61583e9e8f | 148 | green.write(gout/2.0); |
nargetdev | 10:fc61583e9e8f | 149 | blue.write(bout/2.0); |
nargetdev | 10:fc61583e9e8f | 150 | |
nargetdev | 10:fc61583e9e8f | 151 | if (in > hysteresis){ |
nargetdev | 10:fc61583e9e8f | 152 | if (rout < 0.01) |
nargetdev | 10:fc61583e9e8f | 153 | rgb |= 0x1; |
nargetdev | 10:fc61583e9e8f | 154 | if (gout < 0.01) |
nargetdev | 10:fc61583e9e8f | 155 | rgb |= 0x2; |
nargetdev | 10:fc61583e9e8f | 156 | if (bout < 0.01) |
nargetdev | 10:fc61583e9e8f | 157 | rgb |= 0x4; |
nargetdev | 10:fc61583e9e8f | 158 | } |
nargetdev | 13:ed92d45dc9ab | 159 | wait(0.0001f); |
nargetdev | 9:1bd0f7a2ee71 | 160 | } |
nargetdev | 11:2d3cc5459d5b | 161 | pc.printf("\n%X\n", rgb); |
nargetdev | 8:5ed23e276c8a | 162 | } |
nargetdev | 9:1bd0f7a2ee71 | 163 | |
nargetdev | 14:e8a1fe69b048 | 164 | void channel_check(){ |
nargetdev | 14:e8a1fe69b048 | 165 | |
nargetdev | 14:e8a1fe69b048 | 166 | red.write(1.0f); |
nargetdev | 14:e8a1fe69b048 | 167 | wait(.5); |
nargetdev | 14:e8a1fe69b048 | 168 | red.write(0.0f); |
nargetdev | 14:e8a1fe69b048 | 169 | |
nargetdev | 14:e8a1fe69b048 | 170 | green.write(1.0f); |
nargetdev | 14:e8a1fe69b048 | 171 | wait(.5); |
nargetdev | 14:e8a1fe69b048 | 172 | green.write(0.0f); |
nargetdev | 14:e8a1fe69b048 | 173 | |
nargetdev | 14:e8a1fe69b048 | 174 | blue.write(1.0f); |
nargetdev | 14:e8a1fe69b048 | 175 | wait(.5); |
nargetdev | 14:e8a1fe69b048 | 176 | blue.write(0.0f); |
nargetdev | 14:e8a1fe69b048 | 177 | } |
nargetdev | 14:e8a1fe69b048 | 178 | |
nargetdev | 9:1bd0f7a2ee71 | 179 | int main() { |
nargetdev | 18:7c05ef6a639c | 180 | red.period(0.01f); |
nargetdev | 18:7c05ef6a639c | 181 | green.period(0.01f); |
nargetdev | 18:7c05ef6a639c | 182 | blue.period(0.01f); |
nargetdev | 18:7c05ef6a639c | 183 | // |
nargetdev | 18:7c05ef6a639c | 184 | // ble.init(); |
nargetdev | 18:7c05ef6a639c | 185 | // ble.onDisconnection(disconnectionCallback); |
nargetdev | 18:7c05ef6a639c | 186 | // |
nargetdev | 18:7c05ef6a639c | 187 | // uartServicePtr = new UARTService(ble); |
nargetdev | 18:7c05ef6a639c | 188 | // |
nargetdev | 18:7c05ef6a639c | 189 | // /* setup advertising */ |
nargetdev | 18:7c05ef6a639c | 190 | // ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
nargetdev | 18:7c05ef6a639c | 191 | // ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
nargetdev | 18:7c05ef6a639c | 192 | // ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
nargetdev | 18:7c05ef6a639c | 193 | // (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
nargetdev | 18:7c05ef6a639c | 194 | // ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
nargetdev | 18:7c05ef6a639c | 195 | // (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
nargetdev | 18:7c05ef6a639c | 196 | // |
nargetdev | 18:7c05ef6a639c | 197 | // ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
nargetdev | 18:7c05ef6a639c | 198 | // ble.startAdvertising(); |
nargetdev | 18:7c05ef6a639c | 199 | // |
nargetdev | 14:e8a1fe69b048 | 200 | |
nargetdev | 18:7c05ef6a639c | 201 | |
nargetdev | 9:1bd0f7a2ee71 | 202 | |
nargetdev | 9:1bd0f7a2ee71 | 203 | while(1){ |
nargetdev | 18:7c05ef6a639c | 204 | #ifdef MKIT |
nargetdev | 18:7c05ef6a639c | 205 | motion.rise(&show); |
nargetdev | 18:7c05ef6a639c | 206 | #endif |
nargetdev | 18:7c05ef6a639c | 207 | #ifdef NRFDK |
nargetdev | 18:7c05ef6a639c | 208 | motion.fall(&show); |
nargetdev | 18:7c05ef6a639c | 209 | #endif |
nargetdev | 18:7c05ef6a639c | 210 | // red.write(0.0f); |
nargetdev | 18:7c05ef6a639c | 211 | // green.write(0.0f); |
nargetdev | 18:7c05ef6a639c | 212 | // blue.write(0.0f); |
nargetdev | 18:7c05ef6a639c | 213 | ble.waitForEvent(); |
nargetdev | 9:1bd0f7a2ee71 | 214 | } |
nargetdev | 8:5ed23e276c8a | 215 | } |