Added Single Consumer API

This commit is contained in:
NicoHood 2015-11-06 21:25:14 +01:00
parent b626e00b9a
commit 9c4eee90ed
5 changed files with 180 additions and 0 deletions

View file

@ -5,6 +5,8 @@
Consumer example
Press a button to play/pause music player
You may also use SingleConsumer to use a single report.
See HID Project documentation for more Consumer keys.
https://github.com/NicoHood/HID/wiki/Consumer-API
*/

View file

@ -65,6 +65,7 @@ BootMouse KEYWORD1
RawHID KEYWORD1
System KEYWORD1
Consumer KEYWORD1
SingleConsumer KEYWORD1
Gamepad KEYWORD1
Gamepad1 KEYWORD1
Gamepad2 KEYWORD1

View file

@ -42,6 +42,7 @@ THE SOFTWARE.
#include "MultiReport/AbsoluteMouse.h"
#include "SingleReport/BootMouse.h"
#include "MultiReport/ImprovedMouse.h"
#include "SingleReport/SingleConsumer.h"
#include "MultiReport/Consumer.h"
#include "SingleReport/SingleGamepad.h"
#include "MultiReport/Gamepad.h"

View file

@ -0,0 +1,121 @@
/*
Copyright (c) 2014-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 "SingleConsumer.h"
static const uint8_t _hidSingleReportDescriptorConsumer[] PROGMEM = {
/* Consumer Control (Sound/Media keys) */
0x05, 0x0C, /* usage page (consumer device) */
0x09, 0x01, /* usage -- consumer control */
0xA1, 0x01, /* collection (application) */
/* 4 Media Keys */
0x15, 0x00, /* logical minimum */
0x26, 0xFF, 0x03, /* logical maximum (3ff) */
0x19, 0x00, /* usage minimum (0) */
0x2A, 0xFF, 0x03, /* usage maximum (3ff) */
0x95, 0x04, /* report count (4) */
0x75, 0x10, /* report size (16) */
0x81, 0x00, /* input */
0xC0 /* end collection */
};
SingleConsumer_::SingleConsumer_(void) : PluggableUSBModule(1, 1, epType), protocol(HID_REPORT_PROTOCOL), idle(1)
{
epType[0] = EP_TYPE_INTERRUPT_IN;
PluggableUSB().plug(this);
}
int SingleConsumer_::getInterface(uint8_t* interfaceCount)
{
*interfaceCount += 1; // uses 1
HIDDescriptor hidInterface = {
D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE),
D_HIDREPORT(sizeof(_hidSingleReportDescriptorConsumer)),
D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01)
};
return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
}
int SingleConsumer_::getDescriptor(USBSetup& setup)
{
// Check if this is a HID Class Descriptor request
if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; }
if (setup.wValueH != HID_REPORT_DESCRIPTOR_TYPE) { return 0; }
// In a HID Class Descriptor wIndex cointains the interface number
if (setup.wIndex != pluggedInterface) { return 0; }
// Reset the protocol on reenumeration. Normally the host should not assume the state of the protocol
// due to the USB specs, but Windows and Linux just assumes its in report mode.
protocol = HID_REPORT_PROTOCOL;
return USB_SendControl(TRANSFER_PGM, _hidSingleReportDescriptorConsumer, sizeof(_hidSingleReportDescriptorConsumer));
}
bool SingleConsumer_::setup(USBSetup& setup)
{
if (pluggedInterface != setup.wIndex) {
return false;
}
uint8_t request = setup.bRequest;
uint8_t requestType = setup.bmRequestType;
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
{
if (request == HID_GET_REPORT) {
// TODO: HID_GetReport();
return true;
}
if (request == HID_GET_PROTOCOL) {
// TODO: Send8(protocol);
return true;
}
}
if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE)
{
if (request == HID_SET_PROTOCOL) {
protocol = setup.wValueL;
return true;
}
if (request == HID_SET_IDLE) {
idle = setup.wValueL;
return true;
}
if (request == HID_SET_REPORT)
{
}
}
return false;
}
void SingleConsumer_::SendReport(void* data, int length)
{
USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, length);
}
SingleConsumer_ SingleConsumer;

View file

@ -0,0 +1,55 @@
/*
Copyright (c) 2014-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 guard
#pragma once
#include <Arduino.h>
#include "PluggableUSB.h"
#include "HID.h"
#include "HID-Settings.h"
#include "../HID-APIs/ConsumerAPI.h"
class SingleConsumer_ : public PluggableUSBModule, public ConsumerAPI
{
public:
SingleConsumer_(void);
uint8_t getLeds(void);
uint8_t getProtocol(void);
protected:
// Implementation of the PUSBListNode
int getInterface(uint8_t* interfaceCount);
int getDescriptor(USBSetup& setup);
bool setup(USBSetup& setup);
uint8_t epType[1];
uint8_t protocol;
uint8_t idle;
virtual inline void SendReport(void* data, int length) override;
};
extern SingleConsumer_ SingleConsumer;