|
|
@ -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 don’t know the device is around,
|
|
|
|
// if we don’t 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) {
|
|
|
|