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:
- 13:b0ffdf2012b9
- Parent:
- 10:1aed2481a743
- Child:
- 14:3a8c126b7834
diff -r 333944daa29d -r b0ffdf2012b9 main.cpp
--- a/main.cpp Wed Aug 17 00:58:34 2016 +0000
+++ b/main.cpp Sun Aug 21 05:13:45 2016 +0000
@@ -26,6 +26,8 @@
I2C& i2c;
MCP23017 gpio1;
MCP23017 gpio2;
+ bool gpio1_ready;
+ bool gpio2_ready;
static const uint8_t GPIO1_SLAVE_ADDRESS = 0b0100000;
static const uint8_t GPIO2_SLAVE_ADDRESS = 0b0100001;
@@ -35,8 +37,9 @@
* ROW=GPIOB (input pulled-up)
*/
- int setupGpio(MCP23017& gpio) {
+ bool setupGpio(MCP23017& gpio) {
int ok;
+ printf("SET IOCON\r\n");
ok = gpio.write8(
MCP23017::IOCON,
0<<MCP23017::BANK |
@@ -45,49 +48,64 @@
0<<MCP23017::DISSLW |
1<<MCP23017::ODR // int pin is open drain
);
+ if (!ok) return false;
// IODIR
// 1: input
// 0: output
+ printf("SET IODIRA\r\n");
ok = gpio.write16(
MCP23017::IODIRA,
0b0000000011111111
);
+ if (!ok) return false;
// INPUT POLARITY
// 1: inverse polarity
// 0: raw
+ printf("SET IPOLB\r\n");
ok = gpio.write8(
MCP23017::IPOLB,
0b11111111
);
+ if (!ok) return false;
// INTERRUPT-ON-CHANGE Enable
+ printf("SET GPINTENB\r\n");
ok = gpio.write8(
MCP23017::GPINTENB,
0b11111111
);
+ if (!ok) return false;
+
// INTERRUPT-ON-CHANGE Control
// 1: compared with DEFVAL
// 0: compared to previous value
+ printf("SET INTCONB\r\n");
ok = gpio.write8(
MCP23017::INTCONB,
0b00000000
);
+ if (!ok) return false;
+
// PULL-UP (for input pin)
// 1: pull-up enabled
// 0: pull-up disabled
+ printf("SET GPPUB\r\n");
ok = gpio.write8(
MCP23017::GPPUB,
0b11111111
);
+ if (!ok) return false;
+ printf("SET GPIOA\r\n");
ok = gpio1.write8(
MCP23017::GPIOA,
0b00000000
);
+ if (!ok) return false;
- return ok;
+ return true;
}
public:
@@ -99,49 +117,96 @@
}
void init() {
- setupGpio(gpio1);
- // setupGpio(gpio2);
+ printf("init gpio1\r\n");
+ gpio1_ready = setupGpio(gpio1);
+ printf("gpio1 initialized: %s\r\n", gpio1_ready ? "success" : "failed");
+
+ printf("init gpio2\r\n");
+ gpio2_ready = setupGpio(gpio2);
+ printf("gpio2 initialized: %s\r\n", gpio2_ready ? "success" : "failed");
+
}
void scanKeyboard(uint8_t* keys) {
int ok;
+
+ disableInterrupt();
- for (int i = 0; i < 8; i++) {
+ if (gpio1_ready) {
+ for (int i = 0; i < 8; i++) {
+ ok = gpio1.write8(
+ MCP23017::GPIOA,
+ ~(1<<i)
+ );
+ keys[i] = gpio1.read8(MCP23017::GPIOB, ok);
+ }
+
+ // set all output to negative for interrupt
ok = gpio1.write8(
MCP23017::GPIOA,
- ~(1<<i)
+ 0b00000000
);
- keys[i] = gpio1.read8(MCP23017::GPIOB, ok);
}
- // set all output to negative for interrupt
- ok = gpio1.write8(
- MCP23017::GPIOA,
- 0b00000000
- );
- // TODO gpio2
+ if (gpio2_ready) {
+ for (int i = 0; i < 8; i++) {
+ ok = gpio2.write8(
+ MCP23017::GPIOA,
+ ~(1<<i)
+ );
+ keys[i+8] = gpio2.read8(MCP23017::GPIOB, ok);
+ }
+
+ // set all output to negative for interrupt
+ ok = gpio2.write8(
+ MCP23017::GPIOA,
+ 0b00000000
+ );
+ }
+
+ enableInterrupt();
}
-
+
void disableInterrupt() {
int ok;
- // Disable interrupt
- ok = gpio1.write8(
- MCP23017::GPINTENB,
- 0b00000000
- );
+ if (gpio1_ready) {
+ // Disable interrupt
+ ok = gpio1.write8(
+ MCP23017::GPINTENB,
+ 0b00000000
+ );
+ }
+
+ if (gpio2_ready) {
+ // Disable interrupt
+ ok = gpio2.write8(
+ MCP23017::GPINTENB,
+ 0b00000000
+ );
+ }
}
void enableInterrupt() {
int ok;
- // Enable interrupt
- ok = gpio1.write8(
- MCP23017::GPINTENB,
- 0b11111111
- );
+ if (gpio1_ready) {
+ // Enable interrupt
+ ok = gpio1.write8(
+ MCP23017::GPINTENB,
+ 0b11111111
+ );
+ }
+
+ if (gpio2_ready) {
+ // Enable interrupt
+ ok = gpio2.write8(
+ MCP23017::GPINTENB,
+ 0b11111111
+ );
+ }
// Clear interrupt
- gpio1.read8(MCP23017::GPIOB, ok);
+ // gpio1.read8(MCP23017::GPIOB, ok);
}
};
@@ -158,7 +223,7 @@
static uint8_t keysB[COLS];
static bool state = 0;
// delay for interrupt
-static int8_t pollCount = 0;
+static volatile int8_t pollCount = 0;
void buttonIntCallback() {
// just for wakeup
@@ -206,9 +271,9 @@
state = !state;
if (queue) HIDController::queueCurrentReportData();
-
wait_ms(5);
} else {
+ printf("wait for events...\r\n");
HIDController::waitForEvent();
}
}