Hiroh Satoh / keyboard Featured

Dependencies:   BLE_API mbed-dev nRF51822

Revision:
29:ec548c473d50
Parent:
28:1f843a3daab0
Child:
30:f9ebc769118d
--- a/main.cpp	Tue Aug 23 15:25:30 2016 +0000
+++ b/main.cpp	Wed Aug 24 01:27:46 2016 +0000
@@ -253,83 +253,6 @@
 	NRF_POWER->SYSTEMOFF = 1;
 }
 
-/*
-class MyTimerEvent {
-public:
-	static void irq(uint32_t id) {
-	    MyTimerEvent *timer_event = (MyTimerEvent*)id;
-	    timer_event->handler();
-	}
-	
-	MyTimerEvent() : event(), _ticker_data(get_us_ticker_data()) {
-	    ticker_set_handler(_ticker_data, (&MyTimerEvent::irq));
-	}
-    virtual ~MyTimerEvent() {
-    	remove();
-	}
-
-protected:
-    // The handler called to service the timer event of the derived class
-    virtual void handler() = 0;
-
-    // insert in to linked list
-    void insert(timestamp_t timestamp) {
-        ticker_insert_event(_ticker_data, &event, timestamp, (uint32_t)this);
-	}
-
-    // remove from linked list, if in it
-    void remove() {
-        ticker_remove_event(_ticker_data, &event);
-	}
-
-    ticker_event_t event;
-
-    const ticker_data_t *_ticker_data;
-};
-
-class MyTicker : public MyTimerEvent {
-
-public:
-    MyTicker() : MyTimerEvent() {
-    }
-    
-    void attach(void (*fptr)(void), float t) {
-        attach_us(fptr, t * 1000000.0f);
-    }
-    
-    void attach_us(void (*fptr)(void), timestamp_t t) {
-        _function.attach(fptr);
-        setup(t);
-    }
-    
-    virtual ~MyTicker() {
-        detach();
-    }
-
-    void detach() {
-    	remove();
-    	_function.attach(0);
-    }
-
-protected:
-    void setup(timestamp_t t) {
-    	remove();
-	    _delay = t;
-	    insert(_delay + ticker_read(_ticker_data));
-    }
-    virtual void handler() {
-	    insert(event.timestamp + _delay);
-	    _function.call();
-    }
-
-protected:
-    timestamp_t     _delay;    
-    FunctionPointer _function; 
-};
-
-MyTicker ticker;
-*/
-
 void tickerStatus() {
     statusLed = !statusLed;
 }
@@ -337,7 +260,13 @@
 
 int main(void) {
 	printf("init\r\n");
-
+	
+	NRF_POWER->RESET = 1; // Enable Pin-reset on DEBUG mode
+	NRF_POWER->DCDCEN = 0;
+	NRF_POWER->POFCON = 
+		POWER_POFCON_POF_Enabled << POWER_POFCON_POF_Pos |
+		POWER_POFCON_THRESHOLD_V21 << POWER_POFCON_THRESHOLD_Pos;
+    
 	// mbed's Serial of TARGET_RBLAB_BLENANO sucks
 	// DO NOT CONNECT RTS/CTS AUTOMATICALY!
 	NRF_UART0->PSELRTS = 0xFFFFFFFFUL;
@@ -402,30 +331,28 @@
 				wait_ms(5);
 			}
 		} else {
-			printf("wait for events...\r\n");
-			
-			/*// save 50uA
-			while (NRF_UART0->EVENTS_TXDRDY != 1);
-			
-			uint32_t tx = NRF_UART0->PSELTXD;
-			
-			NRF_UART0->TASKS_STOPTX = 1;
-			NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Disabled << UART_ENABLE_ENABLE_Pos);
-			/**/
-
-			printf("COUNTER %x\r\n", NRF_RTC1->COUNTER);
-			HIDController::waitForEvent();
-			
-						
-			/*/
-			NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
-			NRF_UART0->TASKS_STARTTX = 1;
-			NRF_UART0->PSELTXD = 0xFFFFFFFF;
-		    NRF_UART0->EVENTS_TXDRDY = 0;
-		    NRF_UART0->TXD = 0;
-		    while (NRF_UART0->EVENTS_TXDRDY != 1);
-			NRF_UART0->PSELTXD = tx;
-			/**/
+			printf("[%s] wait for events...\r\n", HIDController::connected() ? "connected" : "disconnected");
+    
+    		if (HIDController::connected()) {
+				while (NRF_UART0->EVENTS_TXDRDY != 1);
+				
+				uint32_t tx = NRF_UART0->PSELTXD;
+				
+				NRF_UART0->TASKS_STOPTX = 1;
+				NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Disabled << UART_ENABLE_ENABLE_Pos);
+	
+				HIDController::waitForEvent();
+							
+				NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
+				NRF_UART0->TASKS_STARTTX = 1;
+				NRF_UART0->PSELTXD = 0xFFFFFFFF;
+			    NRF_UART0->EVENTS_TXDRDY = 0;
+			    NRF_UART0->TXD = 0;
+			    while (NRF_UART0->EVENTS_TXDRDY != 1);
+				NRF_UART0->PSELTXD = tx;
+			} else {
+				HIDController::waitForEvent();
+			}
 		}
 	}
 }