From 059d4c9e392fb2188fdecc4c7daee2a87bbaeed4 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Mon, 27 Sep 2021 18:39:33 -0700 Subject: [PATCH] Start to implement i2c support --- .../driver/keyboardio/Model100Side.cpp | 26 ++++++++++++------- .../driver/keyboardio/Model100Side.h | 1 + 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/plugins/Kaleidoscope-Hardware-Keyboardio-Model100/src/kaleidoscope/driver/keyboardio/Model100Side.cpp b/plugins/Kaleidoscope-Hardware-Keyboardio-Model100/src/kaleidoscope/driver/keyboardio/Model100Side.cpp index 636ef257..28b9a349 100644 --- a/plugins/Kaleidoscope-Hardware-Keyboardio-Model100/src/kaleidoscope/driver/keyboardio/Model100Side.cpp +++ b/plugins/Kaleidoscope-Hardware-Keyboardio-Model100/src/kaleidoscope/driver/keyboardio/Model100Side.cpp @@ -24,7 +24,8 @@ #include #include "Model100Side.h" - +#include +#include #include "kaleidoscope/driver/color/GammaCorrection.h" @@ -69,8 +70,7 @@ uint8_t Model100Side::controllerAddress() { // https://www.arduino.cc/en/Reference/WireEndTransmission byte Model100Side::setKeyscanInterval(byte delay) { uint8_t data[] = {TWI_CMD_KEYSCAN_INTERVAL, delay}; - uint8_t result ; // TODO = twi_writeTo(addr, data, ELEMENTS(data), 1, 0); - + uint8_t result = writeData( data, ELEMENTS(data)); return result; } @@ -100,19 +100,25 @@ int Model100Side::readLEDSPIFrequency() { // https://www.arduino.cc/en/Reference/WireEndTransmission byte Model100Side::setLEDSPIFrequency(byte frequency) { uint8_t data[] = {TWI_CMD_LED_SPI_FREQUENCY, frequency}; - uint8_t result ; // TODO = twi_writeTo(addr, data, ELEMENTS(data), 1, 0); + uint8_t result = writeData(data, ELEMENTS(data)); return result; } +uint8_t Model100Side::writeData(uint8_t *data,uint8_t length) { + Wire.beginTransmission(addr); + Wire.write( data, length); + uint8_t result = Wire.endTransmission(); + return result; +} int Model100Side::readRegister(uint8_t cmd) { byte return_value = 0; uint8_t data[] = {cmd}; - uint8_t result ; // TODO = twi_writeTo(addr, data, ELEMENTS(data), 1, 0); + uint8_t result = writeData(data, ELEMENTS(data)); @@ -121,7 +127,7 @@ int Model100Side::readRegister(uint8_t cmd) { // sized pull-ups on both the left and right // hands' i2c SDA and SCL lines - uint8_t rxBuffer[1]; + uint8_t rxBuffer[1] = {0}; // perform blocking read into buffer uint8_t read ; // TODO = twi_readFrom(addr, rxBuffer, ELEMENTS(rxBuffer), true); @@ -137,7 +143,7 @@ int Model100Side::readRegister(uint8_t cmd) { // gives information on the key that was just pressed or released. bool Model100Side::readKeys() { - uint8_t rxBuffer[5]; + uint8_t rxBuffer[5] = {0}; // perform blocking read into buffer uint8_t read ; // TODO = twi_readFrom(addr, rxBuffer, ELEMENTS(rxBuffer), true); @@ -181,7 +187,7 @@ void Model100Side::sendLEDBank(byte bank) { data[i + 1] = pgm_read_byte(&gamma8[c]); } - uint8_t result ; // TODO = twi_writeTo(addr, data, ELEMENTS(data), 1, 0); + uint8_t result = writeData(data, ELEMENTS(data)); } void Model100Side::setAllLEDsTo(cRGB color) { @@ -190,7 +196,7 @@ void Model100Side::setAllLEDsTo(cRGB color) { pgm_read_byte(&gamma8[color.g]), pgm_read_byte(&gamma8[color.r]) }; - uint8_t result ; // TODO = twi_writeTo(addr, data, ELEMENTS(data), 1, 0); + uint8_t result = writeData(data, ELEMENTS(data)); } void Model100Side::setOneLEDTo(byte led, cRGB color) { @@ -200,7 +206,7 @@ void Model100Side::setOneLEDTo(byte led, cRGB color) { pgm_read_byte(&gamma8[color.g]), pgm_read_byte(&gamma8[color.r]) }; - uint8_t result ; // TODO = twi_writeTo(addr, data, ELEMENTS(data), 1, 0); + uint8_t result = writeData(data, ELEMENTS(data)); } diff --git a/plugins/Kaleidoscope-Hardware-Keyboardio-Model100/src/kaleidoscope/driver/keyboardio/Model100Side.h b/plugins/Kaleidoscope-Hardware-Keyboardio-Model100/src/kaleidoscope/driver/keyboardio/Model100Side.h index 1841095b..bc10680e 100644 --- a/plugins/Kaleidoscope-Hardware-Keyboardio-Model100/src/kaleidoscope/driver/keyboardio/Model100Side.h +++ b/plugins/Kaleidoscope-Hardware-Keyboardio-Model100/src/kaleidoscope/driver/keyboardio/Model100Side.h @@ -96,6 +96,7 @@ class Model100Side { byte nextLEDBank = 0; void sendLEDBank(byte bank); int readRegister(uint8_t cmd); + uint8_t writeData(uint8_t* data, uint8_t length); }; #else // ifndef KALEIDOSCOPE_VIRTUAL_BUILD class Model100Side;