Knight Rider PIO sample for teckBASIC, konashi.js

Dependencies:   BLE_API_Native_IRC mbed

Fork of BLE_konashi_PIO_test by Michio Ono

konashi.js mbed HRM1017でもナイトライダー!: http://jsdo.it/micutil/g1Hn

サンプル動画: https://www.youtube.com/watch?v=HSLdzS3sGLw

techBASICサンプル: https://www.dropbox.com/s/c1k25jlen7x3vqo/KnightRider.txt

Revision:
1:6b1c4adfe165
Parent:
0:8c643bfe55b7
Child:
2:6a3257fffa8c
diff -r 8c643bfe55b7 -r 6b1c4adfe165 main.cpp
--- a/main.cpp	Thu Jul 10 14:21:52 2014 +0000
+++ b/main.cpp	Sun Jul 13 01:36:46 2014 +0000
@@ -1,32 +1,57 @@
 #include "mbed.h"
 #include "nRF51822n.h"
-#include "RCBController.h"
 
-#define DBG 0
+#define DBG 1
 
 nRF51822n   nrf;
 Serial  pc(USBTX, USBRX);
 /* LEDs for indication: */
+DigitalOut knsPio[]={P0_0,P0_1,P0_2,P0_3,P0_4,P0_5,P0_6,P0_7};
 DigitalOut  ConnectStateLed(LED1);
-PwmOut  ControllerStateLed(LED2);
+//PwmOut  ControllerStateLed(LED2);
+
+static const uint16_t KONASHI_SERVICE_UUID = 0xFF00;
+//static const uint16_t KONASHI_PIO_SETTING_UUID = 0x3000;
+static const uint16_t KONASHI_PIO_OUTPUT_UUID = 0x3002;
 
+GattService         knsService (KONASHI_SERVICE_UUID);
+/*
+GattCharacteristic  knsPioSetting (KONASHI_PIO_SETTING_UUID,1, 1,
+								GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 
+								GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); */
+GattCharacteristic  knsPioOut (KONASHI_PIO_OUTPUT_UUID,1, 1,
+								GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 
+								GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
 
 /* RCBController Service */
+/*
 static const uint16_t RCBController_service_uuid = 0xFFF0;
 static const uint16_t RCBController_Characteristic_uuid = 0xFFF1;
 GattService         RCBControllerService (RCBController_service_uuid);
 GattCharacteristic  Controller (RCBController_Characteristic_uuid,10, 10,
 								GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 
 								GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
+*/
 
 /* Advertising data and parameters */
 GapAdvertisingData   advData;
 GapAdvertisingData   scanResponse;
 GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
 
-uint16_t	uuid16_list[] = { RCBController_service_uuid };
+uint16_t	uuid16_list[] = { KONASHI_SERVICE_UUID };
+//RCBController controller;
+uint8_t pioSetting=0xFF;
+uint8_t pioOut=0;
 
-RCBController controller;
+void DoPio(void) {
+	uint8_t b=1,vs,vo;
+	for(int i=0;i<8;i++) {
+		vs=(pioSetting & b);
+		vo=(pioOut & b);
+		knsPio[i]=(vs>0 && vo>0);
+		b=(b<<1);
+	}
+}
 
 // GapEvent
 class GapEventHandler : public GapEvents
@@ -35,6 +60,9 @@
     virtual void onConnected(void)
     {
         ConnectStateLed = 0;
+        knsPio[0]=0;
+		knsPio[1]=0;
+
 #if DBG
 		pc.printf("Connected\n\r");
 #endif
@@ -44,6 +72,8 @@
     {
         nrf.getGap().startAdvertising(advParams);
 		ConnectStateLed = 1;
+		knsPio[0]=1;
+		knsPio[1]=1;
 #if DBG
 		pc.printf("Disconnected\n\r");
 #endif
@@ -55,6 +85,26 @@
 {
 	virtual void onDataWritten(uint16_t charHandle)
 	{
+   		/*if (charHandle == knsPioSetting.handle) {
+   			uint8_t getPioSetting;
+	        nrf.getGattServer().readValue(knsPioSetting.handle, &getPioSetting, 1);
+	        if(getPioSetting!=pioSetting) {
+	        	//DoPio();
+	        	pioSetting=getPioSetting;
+	        }
+	        
+		} else */ if (charHandle == knsPioOut.handle) {
+	        uint8_t getPioOut;
+	        nrf.getGattServer().readValue(knsPioOut.handle, &getPioOut, 1);
+	        #if DBG
+	        	pc.printf("DATA: %d %d\n\r",getPioOut,pioOut);
+	        #endif
+	        if(getPioOut!=pioOut) {
+	        	pioOut=getPioOut;
+	        	DoPio();
+	        }
+		}
+/*
    		if (charHandle == Controller.handle) {
 	        nrf.getGattServer().readValue(Controller.handle, &controller.data[0], sizeof(controller));
 #if DBG
@@ -64,7 +114,7 @@
 			ControllerStateLed = (float)controller.status.LeftAnalogLR / 255.0;;
 			
 		}
-		 
+*/		 
 	}
 };
 
@@ -95,14 +145,17 @@
                    (uint8_t*)uuid16_list, sizeof(uuid16_list));
     nrf.getGap().setAdvertisingData(advData, scanResponse);
 	
-	/* RCBController Service */
-	RCBControllerService.addCharacteristic(Controller);
-    nrf.getGattServer().addService(RCBControllerService);
+	/* Service */
+	//knsService.addCharacteristic(knsPioSetting);
+	knsService.addCharacteristic(knsPioOut);
+    nrf.getGattServer().addService(knsService);
 
     /* Start advertising (make sure you've added all your data first) */
     nrf.getGap().startAdvertising(advParams);
     ConnectStateLed = 1;
-    ControllerStateLed = 1;
+		knsPio[0]=1;
+		knsPio[1]=1;
+    //ControllerStateLed = 1;
 
     for (;;)
     {