f/keyboardio-model-100
Jesse Vincent 3 years ago
parent b44edf476b
commit 9d9b16dc93
No known key found for this signature in database
GPG Key ID: 122F5DF7108E4046

@ -48,12 +48,12 @@ driver::keyboardio::Model100Side Model100Hands::leftHand(0);
driver::keyboardio::Model100Side Model100Hands::rightHand(3); driver::keyboardio::Model100Side Model100Hands::rightHand(3);
void Model100Hands::setup(void) { void Model100Hands::setup(void) {
delay(100); delay(100);
pinMode(PB9, OUTPUT_OPEN_DRAIN); pinMode(PB9, OUTPUT_OPEN_DRAIN);
digitalWrite(PB9, LOW); digitalWrite(PB9, LOW);
delay(105); // TODO remove this when we remove it from the attiny code delay(105); // TODO remove this when we remove it from the attiny code
Wire.begin(); Wire.begin();
Wire.setClock(400000); Wire.setClock(400000);
} }

@ -107,49 +107,51 @@ byte Model100Side::setLEDSPIFrequency(byte frequency) {
// This method will verify that the device is around and ready to talk. // This method will verify that the device is around and ready to talk.
bool Model100Side::isDeviceAvailable() { bool Model100Side::isDeviceAvailable() {
// if we dont know the device is around, // if we dont know the device is around,
return true; return true;
// if the counter is zero, that's the special value that means "we know it's there" // if the counter is zero, that's the special value that means "we know it's there"
if (unavailable_device_check_countdown_ == 0) { if (unavailable_device_check_countdown_ == 0) {
return true; return true;
} }
// if the time to check counter is 1, check for the device
// if the time to check counter is 1, check for the device else if (--unavailable_device_check_countdown_ == 0) {
uint8_t wire_result;
else if ( --unavailable_device_check_countdown_ == 0 ) { Wire.beginTransmission(addr);
uint8_t wire_result; wire_result = Wire.endTransmission();
Wire.beginTransmission(addr); //if the check succeeds
wire_result = Wire.endTransmission(); if (wire_result == 0) {
//if the check succeeds unavailable_device_check_countdown_ = 0; // TODO this is already true
if (wire_result == 0) { return true;
unavailable_device_check_countdown_ = 0; // TODO this is already true
return true;
} else {
Wire.beginTransmission (wire_result);
wire_result = Wire.endTransmission();
// set the time to check counter to max
unavailable_device_check_countdown_ = UNAVAILABLE_DEVICE_COUNTDOWN_MAX;
return false;
}
} else { } else {
// we've decremented the counter, but it's not time to probe for the device yet. Wire.beginTransmission(wire_result);
return false; wire_result = Wire.endTransmission();
// set the time to check counter to max
unavailable_device_check_countdown_ = UNAVAILABLE_DEVICE_COUNTDOWN_MAX;
return false;
} }
} else {
// we've decremented the counter, but it's not time to probe for the device yet.
return false;
}
} }
void Model100Side::markDeviceUnavailable() { void Model100Side::markDeviceUnavailable() {
unavailable_device_check_countdown_ = 1; // We think there was a comms problem. Check on the next cycle unavailable_device_check_countdown_ = 1; // We think there was a comms problem. Check on the next cycle
} }
uint8_t Model100Side::writeData(uint8_t *data, uint8_t length) { uint8_t Model100Side::writeData(uint8_t *data, uint8_t length) {
if (isDeviceAvailable() == false ) { if (isDeviceAvailable() == false) {
return 1; return 1;
} }
Wire.beginTransmission(addr); Wire.beginTransmission(addr);
Wire.write(data, length); Wire.write(data, length);
uint8_t result = Wire.endTransmission(); uint8_t result = Wire.endTransmission();
if (result) { markDeviceUnavailable(); } if (result) {
markDeviceUnavailable();
}
return result; return result;
} }
@ -160,7 +162,7 @@ int Model100Side::readRegister(uint8_t cmd) {
// If the setup failed, return. This means there was a problem asking for the register // If the setup failed, return. This means there was a problem asking for the register
if (result) { if (result) {
return -1; return -1;
} }
delayMicroseconds(15); // TODO We may be able to drop this in the future delayMicroseconds(15); // TODO We may be able to drop this in the future
@ -185,18 +187,18 @@ int Model100Side::readRegister(uint8_t cmd) {
// gives information on the key that was just pressed or released. // gives information on the key that was just pressed or released.
bool Model100Side::readKeys() { bool Model100Side::readKeys() {
if (isDeviceAvailable() == false ) { if (isDeviceAvailable() == false) {
return false; return false;
} }
uint8_t row_counter = 0; uint8_t row_counter = 0;
// perform blocking read into buffer // perform blocking read into buffer
uint8_t read = 0; uint8_t read = 0;
uint8_t bytes_returned =0; uint8_t bytes_returned = 0;
bytes_returned = Wire.requestFrom(addr, 5); // request 5 bytes from the keyscanner bytes_returned = Wire.requestFrom(addr, 5); // request 5 bytes from the keyscanner
if (bytes_returned < 5) { if (bytes_returned < 5) {
return false; return false;
} }
if (Wire.available()) { if (Wire.available()) {
read = Wire.read(); read = Wire.read();
if (TWI_REPLY_KEYDATA == read) { if (TWI_REPLY_KEYDATA == read) {

@ -96,7 +96,7 @@ class Model100Side {
int addr; int addr;
int ad01; int ad01;
keydata_t keyData; keydata_t keyData;
// a value of 0 is "device seen" - anything else is how many cycles before we should // a value of 0 is "device seen" - anything else is how many cycles before we should
// check for the device // check for the device
uint16_t unavailable_device_check_countdown_ = 1; uint16_t unavailable_device_check_countdown_ = 1;
static const uint16_t UNAVAILABLE_DEVICE_COUNTDOWN_MAX = 0xFFFFU; static const uint16_t UNAVAILABLE_DEVICE_COUNTDOWN_MAX = 0xFFFFU;

Loading…
Cancel
Save