![](/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@25:d48f46d753fd, 2016-02-01 (annotated)
- Committer:
- nargetdev
- Date:
- Mon Feb 01 01:02:46 2016 +0000
- Revision:
- 25:d48f46d753fd
- Parent:
- 24:52319c0a14b8
- Child:
- 26:8bc9984c4600
alpha done refactoring to object oriented
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 | 25:d48f46d753fd | 8 | #include "sinusoid.h" |
nargetdev | 25:d48f46d753fd | 9 | #include "light_show.h" |
nargetdev | 24:52319c0a14b8 | 10 | |
nargetdev | 25:d48f46d753fd | 11 | #include "utility.h" |
nargetdev | 24:52319c0a14b8 | 12 | |
nargetdev | 25:d48f46d753fd | 13 | #include "macros.h" |
nargetdev | 25:d48f46d753fd | 14 | #include "config.h" |
nargetdev | 25:d48f46d753fd | 15 | #include "typedef.h" |
nargetdev | 11:2d3cc5459d5b | 16 | |
nargetdev | 18:7c05ef6a639c | 17 | |
nargetdev | 24:52319c0a14b8 | 18 | Serial pc(USBTX, USBRX); // tx, rx |
nargetdev | 18:7c05ef6a639c | 19 | |
nargetdev | 24:52319c0a14b8 | 20 | Rgb strip(RED_PIN, GREEN_PIN, BLUE_PIN, &pc); |
nargetdev | 11:2d3cc5459d5b | 21 | |
nargetdev | 19:fabe9521b0a1 | 22 | //UARTService *uartServicePtr; |
nargetdev | 19:fabe9521b0a1 | 23 | const static char DEVICE_NAME[] = "Bathroom"; |
nargetdev | 19:fabe9521b0a1 | 24 | static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; |
nargetdev | 18:7c05ef6a639c | 25 | |
nargetdev | 19:fabe9521b0a1 | 26 | uint8_t motionState = 0; |
nargetdev | 19:fabe9521b0a1 | 27 | ButtonService *buttonServicePtr; |
nargetdev | 18:7c05ef6a639c | 28 | |
nargetdev | 19:fabe9521b0a1 | 29 | |
nargetdev | 24:52319c0a14b8 | 30 | InterruptIn motion(MOTION_PIN); |
nargetdev | 9:1bd0f7a2ee71 | 31 | |
nargetdev | 18:7c05ef6a639c | 32 | BLEDevice ble; |
nargetdev | 18:7c05ef6a639c | 33 | |
nargetdev | 25:d48f46d753fd | 34 | /** Reconnect if bluetooth disconnects **/ |
nargetdev | 25:d48f46d753fd | 35 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params); |
nargetdev | 9:1bd0f7a2ee71 | 36 | |
nargetdev | 25:d48f46d753fd | 37 | /** chill for 10 seconds for FIR sensor calibration **/ |
nargetdev | 25:d48f46d753fd | 38 | void calibrate(); |
nargetdev | 14:e8a1fe69b048 | 39 | |
nargetdev | 25:d48f46d753fd | 40 | /** Light weight interrupt handler **/ |
nargetdev | 25:d48f46d753fd | 41 | void motionIRQ(); |
nargetdev | 24:52319c0a14b8 | 42 | |
nargetdev | 19:fabe9521b0a1 | 43 | |
nargetdev | 24:52319c0a14b8 | 44 | int main() |
nargetdev | 24:52319c0a14b8 | 45 | { |
nargetdev | 21:3960e3b8ca7b | 46 | DEBUG("Start Main.\r\n"); |
nargetdev | 25:d48f46d753fd | 47 | strip.init(); |
nargetdev | 25:d48f46d753fd | 48 | strip.channel_check(); |
nargetdev | 25:d48f46d753fd | 49 | strip.quiet(); |
nargetdev | 25:d48f46d753fd | 50 | |
nargetdev | 25:d48f46d753fd | 51 | // Sinusoid s(0.0, 1.0); |
nargetdev | 25:d48f46d753fd | 52 | // for (float ff;; ff+=0.00005) |
nargetdev | 25:d48f46d753fd | 53 | // strip.write(RED, s.get_y(ff)); |
nargetdev | 25:d48f46d753fd | 54 | |
nargetdev | 25:d48f46d753fd | 55 | LightShow show_obj(&strip); /* object that runs the light show */ |
nargetdev | 25:d48f46d753fd | 56 | |
nargetdev | 25:d48f46d753fd | 57 | #define BLE |
nargetdev | 25:d48f46d753fd | 58 | #ifdef BLE |
nargetdev | 21:3960e3b8ca7b | 59 | |
nargetdev | 25:d48f46d753fd | 60 | //calibrate(); |
nargetdev | 19:fabe9521b0a1 | 61 | |
nargetdev | 19:fabe9521b0a1 | 62 | ble.init(); |
nargetdev | 19:fabe9521b0a1 | 63 | ble.gap().onDisconnection(disconnectionCallback); |
nargetdev | 19:fabe9521b0a1 | 64 | |
nargetdev | 19:fabe9521b0a1 | 65 | ButtonService buttonService(ble, false /* initial value for button pressed */); |
nargetdev | 19:fabe9521b0a1 | 66 | buttonServicePtr = &buttonService; |
nargetdev | 19:fabe9521b0a1 | 67 | |
nargetdev | 19:fabe9521b0a1 | 68 | /* setup advertising */ |
nargetdev | 19:fabe9521b0a1 | 69 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
nargetdev | 19:fabe9521b0a1 | 70 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
nargetdev | 19:fabe9521b0a1 | 71 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
nargetdev | 19:fabe9521b0a1 | 72 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
nargetdev | 19:fabe9521b0a1 | 73 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
nargetdev | 19:fabe9521b0a1 | 74 | ble.gap().startAdvertising(); |
nargetdev | 24:52319c0a14b8 | 75 | |
nargetdev | 18:7c05ef6a639c | 76 | |
nargetdev | 24:52319c0a14b8 | 77 | |
nargetdev | 24:52319c0a14b8 | 78 | while(1) { |
nargetdev | 18:7c05ef6a639c | 79 | #ifdef MKIT |
nargetdev | 19:fabe9521b0a1 | 80 | motion.rise(&motionIRQ); |
nargetdev | 24:52319c0a14b8 | 81 | if (motionState) { |
nargetdev | 21:3960e3b8ca7b | 82 | pc.printf("Motion detected.\r\n"); |
nargetdev | 19:fabe9521b0a1 | 83 | buttonServicePtr->updateButtonState(motionState); |
nargetdev | 25:d48f46d753fd | 84 | show_obj.show(); |
nargetdev | 19:fabe9521b0a1 | 85 | motionState = 0; |
nargetdev | 19:fabe9521b0a1 | 86 | buttonServicePtr->updateButtonState(motionState); |
nargetdev | 24:52319c0a14b8 | 87 | strip.quiet(); |
nargetdev | 19:fabe9521b0a1 | 88 | } |
nargetdev | 25:d48f46d753fd | 89 | #elif defined NRFDK |
nargetdev | 19:fabe9521b0a1 | 90 | motion.fall(&strip.show()); |
nargetdev | 18:7c05ef6a639c | 91 | #endif |
nargetdev | 24:52319c0a14b8 | 92 | |
nargetdev | 18:7c05ef6a639c | 93 | ble.waitForEvent(); |
nargetdev | 9:1bd0f7a2ee71 | 94 | } |
nargetdev | 25:d48f46d753fd | 95 | #endif//BLE |
nargetdev | 25:d48f46d753fd | 96 | } // END main() |
nargetdev | 25:d48f46d753fd | 97 | |
nargetdev | 25:d48f46d753fd | 98 | |
nargetdev | 25:d48f46d753fd | 99 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
nargetdev | 25:d48f46d753fd | 100 | { |
nargetdev | 25:d48f46d753fd | 101 | DEBUG("Disconnected!\n\r"); |
nargetdev | 25:d48f46d753fd | 102 | DEBUG("Restarting the advertising process\n\r"); |
nargetdev | 25:d48f46d753fd | 103 | ble.startAdvertising(); |
nargetdev | 25:d48f46d753fd | 104 | } |
nargetdev | 25:d48f46d753fd | 105 | |
nargetdev | 25:d48f46d753fd | 106 | void motionIRQ() |
nargetdev | 25:d48f46d753fd | 107 | { |
nargetdev | 25:d48f46d753fd | 108 | motionState = 1; |
nargetdev | 25:d48f46d753fd | 109 | } |
nargetdev | 25:d48f46d753fd | 110 | |
nargetdev | 25:d48f46d753fd | 111 | void calibrate() |
nargetdev | 25:d48f46d753fd | 112 | { |
nargetdev | 25:d48f46d753fd | 113 | |
nargetdev | 25:d48f46d753fd | 114 | //give the sensor some time to calibrate |
nargetdev | 25:d48f46d753fd | 115 | pc.printf("calibrating sensor\n\r"); |
nargetdev | 25:d48f46d753fd | 116 | for(int i = 0; i < CALIBRATION_TIME; i++) { |
nargetdev | 25:d48f46d753fd | 117 | pc.printf("."); |
nargetdev | 25:d48f46d753fd | 118 | identify(CALIBRATION_TIME, strip); |
nargetdev | 25:d48f46d753fd | 119 | wait(.5); |
nargetdev | 25:d48f46d753fd | 120 | } |
nargetdev | 25:d48f46d753fd | 121 | DEBUG(" done\n\r"); |
nargetdev | 25:d48f46d753fd | 122 | strip.quiet(); |
nargetdev | 25:d48f46d753fd | 123 | |
nargetdev | 25:d48f46d753fd | 124 | DEBUG("SENSOR ACTIVE\n\r"); |
nargetdev | 25:d48f46d753fd | 125 | wait(0.05); |
nargetdev | 25:d48f46d753fd | 126 | } |
nargetdev | 25:d48f46d753fd | 127 | |
nargetdev | 25:d48f46d753fd | 128 | |
nargetdev | 25:d48f46d753fd | 129 |