HID/avr/libraries/HIDBridge/HIDBridge.cpp

166 lines
4.2 KiB
C++
Raw Normal View History

2015-02-11 15:55:39 +00:00
/*
Copyright (c) 2015 NicoHood
See the readme for credit to other people.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "HIDBridge.h"
//================================================================================
// HIDBridge
//================================================================================
HIDBridge_ HIDBridge;
HIDBridge_::HIDBridge_(void){
// empty
}
2015-02-16 15:07:31 +00:00
bool HIDBridge_::begin(Stream &s)
2015-02-11 15:55:39 +00:00
{
2015-02-16 15:07:31 +00:00
begin((Stream*)&s);
}
2015-02-11 15:55:39 +00:00
2015-02-16 15:07:31 +00:00
bool HIDBridge_::begin(Stream* s)
{
HIDStream = s;
2015-02-11 15:55:39 +00:00
}
2015-02-16 15:07:31 +00:00
2015-02-11 15:55:39 +00:00
void HIDBridge_::err(uint8_t error)
{
2015-02-16 15:07:31 +00:00
if (!debug)
return;
debug->println("Softserial");
debug->println(error);
2015-02-11 15:55:39 +00:00
}
void HIDBridge_::readSerial(void)
2015-02-11 15:55:39 +00:00
{
// read as long as the Serial is available
// but do not block forever
2015-02-16 15:07:31 +00:00
for (rx_buffer_index_t i = 0; i < SERIAL_RX_BUFFER_SIZE; i++){
// read in new Serial byte
int b = Serial.read();
if (b < 0)
break;
// process with NHP protocol
bool newInput = NHPread(b, &nhp_read);
// proceed new valid NHP input
if (newInput) {
2015-02-16 15:07:31 +00:00
if (nhp_read.mode == NHP_ADDRESS) {
switch (nhp_read.address) {
2015-02-11 15:55:39 +00:00
// received a control address command
case HIDBRIDGE_ADDRESS_CONTROL:
// acknowledge/request
2015-02-16 15:07:31 +00:00
if (nhp_read.data == HIDBRIDGE_CONTROL_ISREADY)
2015-02-11 15:55:39 +00:00
isReady = true;
// pause
2015-02-16 15:07:31 +00:00
else if (nhp_read.data == HIDBRIDGE_CONTROL_NOTREADY)
isReady = false;
// not
2015-02-11 15:55:39 +00:00
else
2015-02-16 15:07:31 +00:00
err(HIDBRIDGE_ERR_CONTROL);
2015-02-11 15:55:39 +00:00
break;
// received HID out report TODO
2015-02-11 15:55:39 +00:00
default:
2015-02-16 15:07:31 +00:00
err(HIDBRIDGE_ERR_ADDRESS);
2015-02-11 15:55:39 +00:00
break;
}
}
// received HID out report TODO
2015-02-16 15:07:31 +00:00
else if (nhp_read.mode == NHP_COMMAND) {
err(HIDBRIDGE_ERR_COMMAND);
2015-02-11 15:55:39 +00:00
}
}
2015-02-11 15:55:39 +00:00
// NHP reading error
2015-02-16 15:07:31 +00:00
else if (nhp_read.errorLevel) {
err(HIDBRIDGE_ERR_NHP_ERR);
// do not change isReady state because of a possible full buffer
// which causes NHP corruption
2015-02-11 15:55:39 +00:00
}
}
2015-02-11 15:55:39 +00:00
}
bool HIDBridge_::waitForReady(void)
2015-02-11 15:55:39 +00:00
{
2015-02-16 15:07:31 +00:00
// try to wait for a new request/acknowledge
uint32_t currentMillis = millis();
do{
// check for new state information
// maybe the host sended a pause signal
readSerial();
2015-02-16 15:07:31 +00:00
// check for timeout
if ((millis() - currentMillis) > HIDBRIDGE_TX_TIMEOUT) {
2015-02-16 15:07:31 +00:00
err(HIDBRIDGE_ERR_TIMEOUT);
break;
}
2015-02-16 15:07:31 +00:00
} while (!isReady); //TODO andn no error in readSerial?
return isReady;
2015-02-11 15:55:39 +00:00
}
2015-02-16 15:07:31 +00:00
void HIDBridge_::SendReport(uint8_t reportID, const void* data, int len)
2015-02-11 15:55:39 +00:00
{
2015-02-16 15:07:31 +00:00
// check if stream pointer is set
if (!HIDStream){
err(HIDBRIDGE_ERR_NO_SPTR);
return;
}
// check the latest request/acknowledge,a pause, error
if (!waitForReady()){
err(HIDBRIDGE_ERR_NOT_RDY);
2015-02-11 15:55:39 +00:00
return;
}
// begin transfer with reportID as command
2015-02-16 15:07:31 +00:00
HIDStream->write(NHPwriteCommand(reportID));
2015-02-11 15:55:39 +00:00
// send data in 4 byte packets with the address of the reportID
// the rest (if any, e.g. with 2 byte) is filled with random bytes
NHP_Write_Data_t n;
for (int i = 0; i < len; i += 4) {
NHPwriteAddress(reportID, UINT32_AT_OFFSET(data, i), &n);
2015-02-16 15:07:31 +00:00
HIDStream->write(n.writeBuffer, n.writeLength);
2015-02-11 15:55:39 +00:00
}
// end transfer with zero command
2015-02-16 15:07:31 +00:00
HIDStream->write(NHPwriteCommand(0));
// need a request/acknowledge next time again
2015-02-16 15:07:31 +00:00
isReady = false;
err(42);
}
// overwrites the HID_SendReport function which is empty/not used on a 328/2560
void HID_SendReport(uint8_t reportID, const void* data, int len)
{
HIDBridge.SendReport(reportID, data, len);
2015-02-11 15:55:39 +00:00
}