HID/avr/libraries/HIDBridge/HIDBridge.cpp

192 lines
5 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"
//================================================================================
2015-02-17 19:15:23 +00:00
// HIDBridge TX
2015-02-11 15:55:39 +00:00
//================================================================================
2015-02-17 19:15:23 +00:00
#ifdef HIDBRIDGE_TX
2015-02-11 15:55:39 +00:00
HIDBridge_ HIDBridge;
HIDBridge_::HIDBridge_(void){
// empty
}
2015-02-17 19:15:23 +00:00
void HIDBridge_::begin(void)
2015-02-11 15:55:39 +00:00
{
2015-02-17 19:15:23 +00:00
// start the serial at our own baud rate
HIDBRIDGE_TX_SERIAL.begin(HIDBRIDGE_BAUD);
// wait for the first request to see if usb device is connected
available();
2015-02-16 15:07:31 +00:00
}
2015-02-11 15:55:39 +00:00
2015-02-17 19:15:23 +00:00
void HIDBridge_::end(void)
2015-02-16 15:07:31 +00:00
{
2015-02-17 19:15:23 +00:00
// end the serial transmission and reset our helper values
HIDBRIDGE_TX_SERIAL.end();
nhp_read.mode = NHP_RESET;
isReady = false;
isConnected = false;
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;
2015-02-17 19:15:23 +00:00
debug->print("Bridge Err TX: ");
2015-02-16 15:07:31 +00:00
debug->println(error);
2015-02-11 15:55:39 +00:00
}
2015-02-17 19:15:23 +00:00
void HIDBridge_::read(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++){
2015-02-17 19:15:23 +00:00
2015-02-16 15:07:31 +00:00
// read in new Serial byte
2015-02-17 19:15:23 +00:00
int b = HIDBRIDGE_TX_SERIAL.read();
2015-02-16 15:07:31 +00:00
if (b < 0)
break;
// process with NHP protocol
2015-02-17 19:15:23 +00:00
bool newInput = readNHP(b, &nhp_read);
// proceed new valid NHP input
if (newInput) {
2015-02-17 19:15:23 +00:00
// NHP address contains control data or out report data
2015-02-16 15:07:31 +00:00
if (nhp_read.mode == NHP_ADDRESS) {
2015-02-17 19:15:23 +00:00
// received a control address command
if (nhp_read.address == HIDBRIDGE_ADDRESS_CONTROL) {
// acknowledge/request
2015-02-17 19:15:23 +00:00
if (nhp_read.data == HIDBRIDGE_CONTROL_ISREADY){
2015-02-11 15:55:39 +00:00
isReady = true;
2015-02-17 19:15:23 +00:00
isConnected = true;
}
// pause
2015-02-17 19:15:23 +00:00
else if (nhp_read.data == HIDBRIDGE_CONTROL_NOTREADY){
isReady = false;
2015-02-17 19:15:23 +00:00
isConnected = true;
}
2015-02-17 19:15:23 +00:00
// usb device detached
else if (nhp_read.data == HIDBRIDGE_CONTROL_NOTCONNECTED){
isReady = false;
isConnected = false;
}
// not defined control command
2015-02-11 15:55:39 +00:00
else
2015-02-16 15:07:31 +00:00
err(HIDBRIDGE_ERR_CONTROL);
2015-02-17 19:15:23 +00:00
}
2015-02-17 19:15:23 +00:00
// received HID out report TODO
else
2015-02-16 15:07:31 +00:00
err(HIDBRIDGE_ERR_ADDRESS);
2015-02-11 15:55:39 +00:00
}
2015-02-17 19:15:23 +00:00
// 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
}
2015-02-17 19:15:23 +00:00
bool HIDBridge_::available(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
2015-02-17 19:15:23 +00:00
read();
2015-02-17 19:15:23 +00:00
// check for timeout, do not wait longer if usb device is not connected
if (!isConnected || (millis() - currentMillis) > HIDBRIDGE_TX_TIMEOUT) {
2015-02-16 15:07:31 +00:00
err(HIDBRIDGE_ERR_TIMEOUT);
break;
}
2015-02-17 19:15:23 +00:00
} while (!isReady);
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-17 19:15:23 +00:00
// check the latest request/acknowledge, pause or error
if (!available()){
2015-02-16 15:07:31 +00:00
err(HIDBRIDGE_ERR_NOT_RDY);
2015-02-11 15:55:39 +00:00
return;
}
// begin transfer with reportID as command
2015-02-17 19:15:23 +00:00
HIDBRIDGE_TX_SERIAL.write(writeNHPCommand(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) {
2015-02-17 19:15:23 +00:00
writeNHPAddress(reportID, UINT32_AT_OFFSET(data, i), &n);
HIDBRIDGE_TX_SERIAL.write(n.writeBuffer, n.writeLength);
2015-02-11 15:55:39 +00:00
}
// end transfer with zero command
2015-02-17 19:15:23 +00:00
HIDBRIDGE_TX_SERIAL.write(writeNHPCommand(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-17 19:15:23 +00:00
}
#endif // #ifdef HIDBRIDGE_TX
//================================================================================
// HIDBridge RX
//================================================================================
#ifdef HIDBRIDGE_RX
#endif // #ifdef HIDBRIDGE_RX