Hiroh Satoh / keyboard Featured

Dependencies:   BLE_API mbed-dev nRF51822

main.cpp

Committer:
cho45
Date:
2016-08-22
Revision:
17:3233ee19f716
Parent:
16:345eebc4f259
Child:
20:d8840ac38434

File content as of revision 17:3233ee19f716:

/* mbed Microcontroller Library
 * Copyright (c) 2015 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"

#include "HIDController_BLE.h"

#include "mcp23017.h"
#include "keymap.h"


class KeyboardMatrixController {
	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 = 0b0100100;

	/**
	 * COL=GPIOA (output normaly positive)
	 * ROW=GPIOB (input pulled-up)
	 */

	bool setupGpio(MCP23017& gpio) {
		int ok;
		printf("SET IOCON\r\n");
		ok = gpio.write8(
			MCP23017::IOCON,
			0<<MCP23017::BANK |
			1<<MCP23017::MIRROR |
			1<<MCP23017::SEQOP |
			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 true;
	}

public:
	KeyboardMatrixController(I2C& _i2c) :
		i2c(_i2c),
		gpio1(i2c, GPIO1_SLAVE_ADDRESS),
		gpio2(i2c, GPIO2_SLAVE_ADDRESS)
	{
	}

	void init() {
		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();

		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,
				0b00000000
			);
		}


		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;
		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;
		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);
	}
};

I2C i2c(I2C_SDA0, I2C_SCL0);
// Serial serial(USBTX, USBRX);
KeyboardMatrixController keyboardMatrixController(i2c);
Keymap keymap;

// Interrupt from MCP23017
// (pulled-up and two MCP23017 is configured with open drain INT)
InterruptIn buttonInt(P0_5);
DigitalIn buttonIntIn(P0_5);

// Unsed pins. Set to output for power consumption
DigitalOut unused_p0_4(P0_4, 0);
DigitalOut unused_p0_7(P0_7, 0);
DigitalOut unused_p0_6(P0_6, 0);
DigitalOut unused_p0_15(P0_15, 0);
DigitalOut unused_p0_29(P0_29, 0);
DigitalOut unused_p0_28(P0_28, 0);
DigitalOut unused_p0_19(P0_19, 0); // This is on board LED


// ROWS=8
// COLS=16
// 列ごとに1バイトにパックしてキーの状態を保持する
static uint8_t keysA[COLS];
static uint8_t keysB[COLS];
static bool state = 0;
// delay for interrupt
static volatile int8_t pollCount = 0;

void buttonIntCallback() {
	// just for wakeup
	pollCount = 100;
}

int main(void) {
	printf("init\r\n");

	// mbed's Serial of TARGET_RBLAB_BLENANO sucks
	// DO NOT CONNECT RTS/CTS AUTOMATICALY!
	NRF_UART0->PSELRTS = 0xFFFFFFFFUL;
	NRF_UART0->PSELCTS = 0xFFFFFFFFUL;


	// 100kHz
	i2c.frequency(100000);

	buttonInt.mode(PullUp);
	buttonInt.fall(buttonIntCallback);

	keyboardMatrixController.init();
	buttonIntCallback();

	HIDController::init();
		
	// STOP UART RX for power consumption
	// NRF_UART0->TASKS_STOPRX = 1;
	
	NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
				
	while (1) {
		if (pollCount-- > 0) {
			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;
				for (int row = 0; row < ROWS; row++) {
					if (changed & (1<<row)) {
						bool pressed = keysCurr[col] & (1<<row);
						// printf("changed: col=%d, row=%d / pressed=%d\r\n", col, row, pressed);
						keymap.execute(col, row, pressed);
					}
				}
			}
			state = !state;
			

			if (queue) HIDController::queueCurrentReportData();
			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->TASKS_STOPRX = 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;
			*/
		}
	}
}