here is the wiring. I used 3.7k resistors instead of 220.
This is my code. I used the HIDkeyboard as a basis.
Code: Select all
#include <Arduino.h>
#include <Adafruit_MCP23X17.h>
#include <SPI.h>
#include <Wire.h>
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined(ARDUINO_ARCH_SAMD)
#include <SoftwareSerial.h>
#endif
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#include "BluefruitConfig.h"
#include "keycode.h"
#define FACTORYRESET_ENABLE 0
// Create the bluefruit object, either software serial...uncomment these lines
/*
SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);
Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,
BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);
/* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */
//Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN);
/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
/* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */
//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO,
// BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS,
// BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}
typedef struct
{
uint8_t modifier; /**< Keyboard modifier keys */
uint8_t reserved; /**< Reserved for OEM use, always set to 0. */
uint8_t keycode[6]; /**< Key codes of the currently pressed keys. */
} hid_keyboard_report_t;
// Report that send to Central every scanning period
hid_keyboard_report_t keyReport = { 0, 0, {0 }};
// Report sent previously. This is used to prevent sending the same report over time.
// Notes: HID Central intepretes no new report as no changes, which is the same as
// sending very same report multiple times. This will help to reduce traffic especially
// when most of the time there is no keys pressed.
// - Init to different with keyReport
hid_keyboard_report_t previousReport = { 0, 0, { 1 }};
Adafruit_MCP23X17 mcp;
// GPIO corresponding to HID keycode (not Ancii Character)
int COLinputPins[17] =
{ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0 };
int ROWinputPins[6] =
{ 13,12,11,10,9,6 };
int y = 1;
int modders[4] =
{0, 0, 0, 0};
int modders2[4] =
{0, 0, 0, 0};
//keyboard matrix
int inputKeycodes[6] [17] = {
{HID_KEY_ESCAPE, HID_KEY_NONE, HID_KEY_F1, HID_KEY_F2, HID_KEY_F3, HID_KEY_F4, HID_KEY_F5, HID_KEY_F6, HID_KEY_F7, HID_KEY_F8, HID_KEY_F9, HID_KEY_F10, HID_KEY_F11, HID_KEY_F12},
{HID_KEY_GRAVE, HID_KEY_1, HID_KEY_2, HID_KEY_3, HID_KEY_4, HID_KEY_5, HID_KEY_6, HID_KEY_7, HID_KEY_8, HID_KEY_9, HID_KEY_0, HID_KEY_MINUS, HID_KEY_EQUAL, HID_KEY_BACKSPACE},
{HID_KEY_TAB, HID_KEY_Q, HID_KEY_W, HID_KEY_E, HID_KEY_R, HID_KEY_T, HID_KEY_Y, HID_KEY_U, HID_KEY_I, HID_KEY_O, HID_KEY_P, HID_KEY_BRACKET_LEFT, HID_KEY_BRACKET_RIGHT, HID_KEY_BACKSLASH},
{HID_KEY_CAPS_LOCK, HID_KEY_A, HID_KEY_S, HID_KEY_D, HID_KEY_F, HID_KEY_G, HID_KEY_H, HID_KEY_J, HID_KEY_K, HID_KEY_L, HID_KEY_SEMICOLON, HID_KEY_APOSTROPHE, HID_KEY_RETURN, HID_KEY_NONE},
{HID_KEY_SHIFT_LEFT, HID_KEY_Z, HID_KEY_X, HID_KEY_C, HID_KEY_V, HID_KEY_B, HID_KEY_N, HID_KEY_M, HID_KEY_COMMA, HID_KEY_PERIOD, HID_KEY_SLASH, HID_KEY_NONE, HID_KEY_SHIFT_RIGHT, HID_KEY_NONE},
{HID_KEY_CONTROL_LEFT, HID_KEY_ALT_LEFT, HID_KEY_GUI_LEFT, HID_KEY_NONE, HID_KEY_NONE, HID_KEY_SPACE, HID_KEY_NONE, HID_KEY_NONE, HID_KEY_NONE, HID_KEY_NONE, HID_KEY_GUI_RIGHT, HID_KEY_ALT_RIGHT, HID_KEY_CONTROL_RIGHT, HID_KEY_NONE}
};
int inputMacro [6][3] = {
{HID_KEY_MUTE, DIM, MACRO},
{COPY, PASTE, NEW_LAYER},
{DEL, END, GDN},
{PENCIL, PEN, NORMAL},
{HID_KEY_NONE, HID_KEY_ARROW_UP, HID_KEY_NONE},
{ HID_KEY_ARROW_LEFT, HID_KEY_ARROW_DOWN, HID_KEY_ARROW_RIGHT}
};
int MO = 0;
/**************************************************************************/
/*!
@brief Sets up the HW an the BLE module (this function is called
automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
//while (!Serial); // required for Flora & Micro
long oldPosition = -999;
Serial.begin(115200);
Serial.println(F("Adafruit Bluefruit HID Keyboard Example"));
Serial.println(F("---------------------------------------"));
/* Initialise the module */
Serial.print(F("Initialising the Bluefruit LE module: "));
if ( !ble.begin(VERBOSE_MODE) )
{
error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
}
Serial.println( F("OK!") );
if ( FACTORYRESET_ENABLE )
{
/* Perform a factory reset to make sure everything is in a known state */
Serial.println(F("Performing a factory reset: "));
ble.factoryReset();
}
/* Disable command echo from Bluefruit */
ble.echo(false);
Serial.println("Requesting Bluefruit info:");
/* Print Bluefruit information */
ble.info();
/* Enable HID Service if not enabled */
int32_t hid_en = 0;
ble.sendCommandWithIntReply( F("AT+BleHIDEn"), &hid_en);
if (!mcp.begin_I2C()) {
//if (!mcp.begin_SPI(CS_PIN)) {
Serial.println("Error.");
while (1);
}
if ( !hid_en )
{
Serial.println(F("Enable HID Service (including Keyboard): "));
ble.sendCommandCheckOK(F( "AT+BleHIDEn=On" ));
/* Add or remove service requires a reset */
Serial.println(F("Performing a SW reset (service changes require a reset): "));
!ble.reset();
}
Serial.println();
Serial.println(F("Go to your phone's Bluetooth settings to pair your device"));
Serial.println(F("then open an application that accepts keyboard input"));
Serial.println();
// Set up input Pins
for (int i = 0; i < 18; i++) {
mcp.pinMode(COLinputPins[i], INPUT_PULLUP);
}
pinMode(0, INPUT_PULLUP);
mcp.pinMode(2, INPUT_PULLUP);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
keyReport.keycode[1] = 0;
keyReport.keycode[2] = 0;
keyReport.keycode[3] = 0;
keyReport.keycode[4] = 0;
}
/**************************************************************************/
/*!
@brief Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
//testing one key for now
if ( ble.isConnected() )
{
digitalWrite(ROWinputPins[0], HIGH);
digitalWrite(12, LOW);
digitalWrite(ROWinputPins[2], HIGH);
digitalWrite(ROWinputPins[3], HIGH);
digitalWrite(ROWinputPins[4], HIGH);
digitalWrite(ROWinputPins[5], HIGH);
digitalWrite(ROWinputPins[6], HIGH);
if ( mcp.digitalRead(0) == LOW ) {
keyReport.keycode[y] = HID_KEY_KEYPAD_1;
if ( memcmp(&previousReport, &keyReport, 8) )
{
// Send keyboard report
ble.atcommand("AT+BLEKEYBOARDCODE", (uint8_t*) &keyReport, 8);
// copy to previousReport
memcpy(&previousReport, &keyReport, 8);
}
}
else {
keyReport.modifier = 0 ;
keyReport.keycode[y] = 0;
}
}
}