Light Show library for organic, calm, light display.

Dependencies:   BLE_API mbed nRF51822

Fork of mbed_blinky by Mbed

Revision:
18:7c05ef6a639c
Parent:
17:76c74851442f
Child:
19:fabe9521b0a1
--- a/main.cpp	Sun Oct 11 07:11:19 2015 +0000
+++ b/main.cpp	Fri Oct 30 02:41:18 2015 +0000
@@ -1,25 +1,67 @@
-#include "mbed.h"
- 
- 
+#include "mbed.h" 
+#include "BLE.h"
+#include "UARTService.h"
+#include <string>
+
+#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
+                               * it will have an impact on code-size and power consumption. */
 
-#define calibrationTime 3
+#if NEED_CONSOLE_OUTPUT
+//#define DEBUG(...) { printf(__VA_ARGS__); }
+#define DEBUG(STR) { if (uartServicePtr) uartServicePtr->write(STR, strlen(STR)); }
+#else
+#define DEBUG(...) /* nothing */
+#endif /* #if NEED_CONSOLE_OUTPUT */
+
+//#define NRFDK
+#define MKIT
+
+#define calibrationTime 2
 
 #define HPI 1.571
 #define PI 3.1416
-#define HYSTERESIS_QUANTITY  PI
+#define HYSTERESIS_QUANTITY  PI/4
 
-unsigned long seed = 157;
+#define RWAIT 0
+#define GWAIT 0
+#define BWAIT 0
+
+UARTService *uartServicePtr;
+
+unsigned long seed = 113;
 
  typedef unsigned char byte;
  typedef unsigned int uint;
  
+#ifdef NRFDK
+InterruptIn motion(p20);
+PwmOut red(p21);
+PwmOut green(p22);
+PwmOut blue(p23);
+#endif
+
+#ifdef MKIT
 PwmOut red(p18);
 PwmOut green(p19);
 PwmOut blue(p20);
-DigitalIn motion(p1);
+InterruptIn motion(p1);
+#endif
+
 
 Serial pc(USBTX, USBRX); // tx, rx
 
+
+BLEDevice  ble;
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    DEBUG("Disconnected!\n\r");
+    DEBUG("Restarting the advertising process\n\r");
+    ble.startAdvertising();
+}
+
+
+
 unsigned int hash(unsigned int x) {
     x = ((x >> 16) ^ x) * 0x45d9f3b;
     x = ((x >> 16) ^ x) * 0x45d9f3b;
@@ -36,7 +78,9 @@
     hashable = hash(m + seed);
     write_me = hashable/100.0;
     red.write(hashable >= 50);
-    pc.printf("r, g, b: %f\t", write_me);
+    char* STR;
+    sprintf(STR, "r, g, b: %f\t", write_me);
+    DEBUG(STR);
 
     hashable = hash(m + seed);
     write_me = hashable/100.0;
@@ -58,8 +102,8 @@
       identify(calibrationTime);
       wait(.5);
       }
-    pc.printf(" done\n");
-    pc.printf("SENSOR ACTIVE\n");
+    DEBUG(" done\n");
+    DEBUG("SENSOR ACTIVE\n");
     wait(0.05);
 }
 
@@ -74,8 +118,12 @@
 
   for (in = -HPI; in < hysteresis || rgb != 0x7; in = in + 0.00628)
   {
-    unsigned int i = motion;
-    if (motion){
+#ifdef MKIT
+    bool i = motion;
+#else
+    bool i = !motion;
+#endif
+    if (i){
         hysteresis = in + HYSTERESIS_QUANTITY;
         pc.printf("hyst ||");
     }
@@ -93,8 +141,8 @@
     else
         bout = 0.0f;
         
-    if (tit)
-        pc.printf("%f\t%f\t%f\n",rout,gout, bout);
+    pc.printf("%f\t%f\t%f\n",rout,gout, bout);
+
 
     red.write(rout/2.0);
     green.write(gout/2.0);
@@ -129,30 +177,39 @@
 }
 
 int main() {
-    red.period(0.001f);
-    green.period(0.001f);
-    blue.period(0.001f);
+    red.period(0.01f);
+    green.period(0.01f);
+    blue.period(0.01f);
+//
+//    ble.init();
+//    ble.onDisconnection(disconnectionCallback);
+//    
+//    uartServicePtr = new UARTService(ble);
+//
+//    /* setup advertising */
+//    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+//    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+//    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
+//                                     (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
+//    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
+//                                     (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
+//
+//    ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
+//    ble.startAdvertising();
+//    
     
-//    channel_check();
-    pc.printf("Hello World!\n");
-// Calibrate FIR and identify.
-    calibrate();
-    
-//    Clear the lights.  Wait for victim.
-    red.write(0.00f);
-    green.write(0.00f);   
-    blue.write(0.00f); 
+
 
     while(1){
-        unsigned int i = motion;
-        if (i){
-            pc.printf("Motion detected.\n");
-            show();
-            pc.printf("End show.\n");
-        }
-        else
-            red.write(0.0f);
-            green.write(0.0f);
-            blue.write(0.0f);
+#ifdef MKIT
+        motion.rise(&show);
+#endif
+#ifdef NRFDK
+        motion.fall(&show);
+#endif
+//        red.write(0.0f);
+//        green.write(0.0f);
+//        blue.write(0.0f);
+        ble.waitForEvent();
     }
 }
\ No newline at end of file