Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed-dev nRF51822
Diff: main.cpp
- Revision:
- 43:4de3870b39cb
- Parent:
- 42:2c3be8694896
- Child:
- 45:f4be69c936f6
--- a/main.cpp Sat Aug 27 08:43:53 2016 +0000
+++ b/main.cpp Sat Aug 27 17:52:28 2016 +0900
@@ -32,11 +32,12 @@
// Interrupt from MCP23017
// (pulled-up and two MCP23017 is configured with open drain INT)
-InterruptIn buttonInt(P0_5);
+InterruptIn keyboardInterruptIn(P0_5);
#define PIN_STATUS_LED P0_4
DigitalOut statusLed(PIN_STATUS_LED, 0);
+// timeout for status led and wakeup from sleep
Timeout timeout;
// ROWS=8
@@ -50,7 +51,7 @@
// delay for interrupt
static volatile int8_t pollCount = 50;
-void buttonIntCallback() {
+void keyboardInterrupt() {
// just for wakeup
pollCount = 25;
}
@@ -61,7 +62,7 @@
}
void tickerStatus() {
- statusLed = !statusLed;
+ statusLed = !statusLed;
}
static bool updateStatudLedEnabled = false;
@@ -93,14 +94,15 @@
{
uint32_t reason = NRF_POWER->RESETREAS;
NRF_POWER->RESETREAS = 0xffffffff; // clear reason
+ // reset cause should be shown everytime
serial.printf("init [%x]\r\n", reason);
}
-
+
float battery = BatteryLevel::readBatteryVoltage();
if (battery < BatteryLevel::BATTERY_LOW) {
powerOff();
}
-
+
// Enable Pin-reset on DEBUG mode
// This makes possiable booting without normal mode easily.
NRF_POWER->RESET = 1;
@@ -110,12 +112,12 @@
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 WITHOUT PRIOR CONSENT!
NRF_UART0->PSELRTS = 0xFFFFFFFFUL;
NRF_UART0->PSELCTS = 0xFFFFFFFFUL;
-
+
// Set LED Pin as HIGH Current mode
NRF_GPIO->PIN_CNF[PIN_STATUS_LED] =
(NRF_GPIO->PIN_CNF[PIN_STATUS_LED] & ~GPIO_PIN_CNF_DRIVE_Msk) |
@@ -184,38 +186,38 @@
// only 100kHz/250khz/400khz
i2c.frequency(250000);
- buttonInt.mode(PullUp);
- buttonInt.fall(buttonIntCallback);
+ keyboardInterruptIn.mode(PullUp);
+ keyboardInterruptIn.fall(keyboardInterrupt);
keyboardMatrixController.init();
pollCount = 10;
HIDController::init();
-
+
// STOP UART RX for power consumption
NRF_UART0->TASKS_STOPRX = 1;
-
+
// Disable TWI by default.
NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
-
+
while (1) {
WatchDog::reload();
-
+
if (pollCount > 0) {
DEBUG_PRINTF("scan keys\r\n");
-
+
while (pollCount-- > 0) {
WatchDog::reload();
-
+
uint8_t (&keysCurr)[COLS] = state ? keysA : keysB;
uint8_t (&keysPrev)[COLS] = state ? keysB : keysA;
-
+
NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
keyboardMatrixController.scanKeyboard(keysCurr);
NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
-
+
bool queue = false;
-
+
for (int col = 0; col < COLS; col++) {
uint8_t changed = keysPrev[col] ^ keysCurr[col];
if (changed) queue = true;
@@ -228,27 +230,27 @@
}
}
state = !state;
-
+
if (HIDController::status() == DISCONNECTED ||
HIDController::status() == TIMEOUT) {
-
- if (
- is_pressed(keysCurr, 0, 0) && // left top 1
- is_pressed(keysCurr, 1, 0) && // left top 2
- is_pressed(keysCurr, 0, 15) // right top
- ) {
- DEBUG_PRINTF("re-init connection\r\n");
- HIDController::initializeConnection(true);
- } else {
- if (HIDController::status() == TIMEOUT) {
- DEBUG_PRINTF("wakeup\r\n");
- HIDController::initializeConnection(false);
- }
- }
+
+ if (
+ is_pressed(keysCurr, 0, 0) && // left top 1
+ is_pressed(keysCurr, 1, 0) && // left top 2
+ is_pressed(keysCurr, 0, 15) // right top
+ ) {
+ DEBUG_PRINTF("re-init connection\r\n");
+ HIDController::initializeConnection(true);
+ } else {
+ if (HIDController::status() == TIMEOUT) {
+ DEBUG_PRINTF("wakeup\r\n");
+ HIDController::initializeConnection(false);
+ }
+ }
}
-
+
if (queue) HIDController::queueCurrentReportData();
-
+
// wait_ms(5); is busy loop
// use timer1 to use wait 5ms
timeout.attach_us(wakeupKeyIntervalSleep, 5000);
@@ -257,7 +259,7 @@
}
} else {
if (!updateStatudLedEnabled) updateStatusLed();
-
+
float batteryVoltage = BatteryLevel::readBatteryVoltage();
uint8_t batteryPercentage = BatteryLevel::readBatteryPercentage(batteryVoltage);
bool isLowBattery = batteryVoltage < BatteryLevel::BATTERY_LOW;
@@ -268,37 +270,36 @@
HIDController::statusString(),
isLowBattery ? "LOWBAT" : "WFE"
);
-
+
HIDController::updateBatteryLevel(batteryPercentage);
-
+
if (isLowBattery) {
powerOff();
}
-
-
+
if (DEBUG_BLE_INTERRUPT) {
HIDController::waitForEvent();
} else {
// disable internal HFCLK RC Clock surely. It consume 1mA constantly
// TWI / SPI / UART must be disabled and boot without debug mode
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;
// dummy send to wakeup...
NRF_UART0->PSELTXD = 0xFFFFFFFF;
- NRF_UART0->EVENTS_TXDRDY = 0;
- NRF_UART0->TXD = 0;
- while (NRF_UART0->EVENTS_TXDRDY != 1);
+ NRF_UART0->EVENTS_TXDRDY = 0;
+ NRF_UART0->TXD = 0;
+ while (NRF_UART0->EVENTS_TXDRDY != 1);
NRF_UART0->PSELTXD = tx;
}
}
}
-}
\ No newline at end of file
+}