fix bInterval

This commit is contained in:
CrazyRedMachine
2020-07-09 20:58:53 +02:00
parent 6e566ed73f
commit d2ed154cde
3 changed files with 37 additions and 35 deletions
+358
View File
@@ -0,0 +1,358 @@
/* Arduino-HID-Lighting-Library
*
* This Arduino-HID-Lighting-Library is derived from Arduino-HID-Lighting, whose copyriht owner is mon.
* More information about Arduino-HID-Lighting you can find under:
*
* mon's Arduino-HID-Lighting
* https://github.com/mon/Arduino-HID-Lighting
*
* 2018 (C) Arduino-HID-Lighting-Library, Knuckleslee
*/
#include "POPNHID.h"
byte extern LightPins[];
/* HID DESCRIPTOR */
static const byte PROGMEM _hidReportPOPN[] = {
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
0x09, 0x05, /* USAGE (Game Pad) */
0xa1, 0x01, /* COLLECTION (Application) */
/*Buttons */
0x85, 0x04, /* REPORT_ID 4 */
0x05, 0x09, /* USAGE_PAGE (Button) */
0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
0x29, 0x0c, /* USAGE_MAXIMUM (Button 12)*/
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
#if defined(ARDUINO_ARCH_SAM)
0x95, 0x0c, /* REPORT_COUNT (12) */
#else
0x95, 0x0b, /* REPORT_COUNT (11) */
#endif
0x75, 0x01, /* REPORT_SIZE (1) */
0x81, 0x02, /* INPUT (Data,Var,Abs) */
/* Reserved bits */
0x95, 0x01, /* REPORT_COUNT (1) */
#if defined(ARDUINO_ARCH_SAM)
0x75, 0x04, /* REPORT_SIZE (4) */
#else
0x75, 0x05, /* REPORT_SIZE (5) */
#endif
0x81, 0x03, /* INPUT (Cnst,Var,Abs) */
/*Lights */
0x85, 0x05, /* REPORT_ID 5*/
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
/*Led 1 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x01, /* USAGE (Instance 1) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 2 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x02, /* USAGE (Instance 2) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 3 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x03, /* USAGE (Instance 3) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 4 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x04, /* USAGE (Instance 4) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 5 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x05, /* USAGE (Instance 5) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 6 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x06, /* USAGE (Instance 6) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 7 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x07, /* USAGE (Instance 7) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 8 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x08, /* USAGE (Instance 8) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 9 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x09, /* USAGE (Instance 9) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
#if defined(ARDUINO_ARCH_SAM)
/*Led 10 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x0a, /* USAGE (Instance 10) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 11 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x0b, /* USAGE (Instance 11) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 12 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x0c, /* USAGE (Instance 12) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 13 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x0d, /* USAGE (Instance 13) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 14 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x0e, /* USAGE (Instance 14) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 15 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x0f, /* USAGE (Instance 15) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 16 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x10, /* USAGE (Instance 16) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 17 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x11, /* USAGE (Instance 17) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/*Led 18 */
0x05, 0x0a, /* USAGE_PAGE (Ordinals) */
0x09, 0x12, /* USAGE (Instance 18) */
0xa1, 0x02, /* COLLECTION (Logical) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x09, 0x4b, /* USAGE (Generic Indicator 1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0xc0, /* END_COLLECTION */
/* Reserved 14 bits */
0x95, 0x01, /* REPORT_COUNT (1) */
0x75, 0x0E, /* REPORT_SIZE (14) */
0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */
#else
/* Reserved 23 bits */
0x95, 0x01, /* REPORT_COUNT (1) */
0x75, 0x17, /* REPORT_SIZE (23) */
0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */
#endif
/*Footer */
0xc0 /* END_COLLECTION */
};
/* PluggableUSBModule IMPLEMENTATION */
POPNHID_::POPNHID_(void) : PluggableUSBModule(1, 1, epType) {
epType[0] = EP_TYPE_INTERRUPT_IN;
PluggableUSB().plug(this);
}
int POPNHID_::getInterface(byte* 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(_hidReportPOPN)),
D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01) // this last parameter is the bInterval (requested polling rate)
};
return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
}
int POPNHID_::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 contains the interface number
if (setup.wIndex != pluggedInterface) { return 0; }
return USB_SendControl(TRANSFER_PGM, _hidReportPOPN, sizeof(_hidReportPOPN));
}
bool POPNHID_::setup(USBSetup& setup)
{
if (pluggedInterface != setup.wIndex) {
return false;
}
byte request = setup.bRequest;
byte requestType = setup.bmRequestType;
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
{
return true;
}
if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE) {
if (request == HID_SET_REPORT) {
if(setup.wValueH == HID_REPORT_TYPE_OUTPUT && setup.wLength == 5){
USB_RecvControl(led_data, 5);
return true;
}
}
}
return false;
}
uint8_t POPNHID_::getShortName(char *name)
{
name[0] = 'P';
name[1] = 'O';
name[2] = 'P';
name[3] = 'N';
return 4;
}
/* CUSTOM POPN FUNCTIONS */
uint8_t POPNHID_::getLightMode(){
return lightMode;
}
void POPNHID_::setLightMode(uint8_t mode){
if ((mode > 3) || (mode < 0)) {
lightMode = 2;
return;
}
lightMode = mode;
}
void POPNHID_::updateLightMode(){
uint32_t* bitfield = (uint32_t*)&(led_data[1]);
if (*bitfield>>28&1){
uint8_t mode = (*bitfield>>24) & 0x0F;
setLightMode(mode);
*bitfield &= ~((uint32_t)0xFF<<24);
}
}
void POPNHID_::updateLeds(uint32_t buttonsState, bool invert){
uint32_t* bitfield = (uint32_t*)&(led_data[1]);
uint32_t leds = (*bitfield|buttonsState);
if (invert)
leds = ~leds;
for(int i = 0; i < 9; i++) {
if (leds>>i&1)
digitalWrite(LightPins[i],HIGH);
else
digitalWrite(LightPins[i],LOW);
}
#if defined(ARDUINO_ARCH_SAM)
for(int i = 9; i < 18; i++) {
if (leds>>i&1)
digitalWrite(LightPins[i],HIGH);
else
digitalWrite(LightPins[i],LOW);
}
#endif
}
int POPNHID_::sendState(uint32_t buttonsState){
uint8_t data[3];
data[0] = (uint8_t) 4; //report id
data[1] = (uint8_t) (buttonsState & 0xFF);
data[2] = (uint8_t) (buttonsState >> 8) & 0xFF;
return USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, 3);
}
+72
View File
@@ -0,0 +1,72 @@
#include "HID.h"
#if defined(ARDUINO_ARCH_AVR)
#define EPTYPE_DESCRIPTOR_SIZE uint8_t
#elif defined(ARDUINO_ARCH_SAM)
#define EPTYPE_DESCRIPTOR_SIZE uint32_t
#define USB_EP_SIZE 64
#define TRANSFER_PGM 0x80
#define USB_SendControl USBD_SendControl
#define USB_RecvControl USBD_RecvControl
#define USB_Recv USBD_Recv
#define USB_Send USBD_Send
#define USB_Flush USBD_Flush
#define HID_REPORT_TYPE_OUTPUT 2
#define HID_REPORT_TYPE_INPUT 1
#else
#error "Unsupported architecture"
#endif
class POPNHID_ : public PluggableUSBModule {
public:
POPNHID_(void);
/**
* Updates the led status based on led_data (HID report received) and button states
* param[in] buttonState bitfield with currently pressed buttons (used to force additional lights for mixed mode)
* param[in] invert set to true to invert on/off status (used for invert lightmode)
*/
void updateLeds(uint32_t buttonsState, bool invert);
/**
* Sends the gamepad button states to the PC as an HID report
* param[in] buttonsState bitfield with currently pressed buttons
* return USB_Send() return value
*/
int sendState(uint32_t buttonsState);
/**
* Changes the lightMode if a received HID report asks for it
*/
void updateLightMode();
/**
* getter and setter for lightMode protected field.
*/
uint8_t getLightMode();
void setLightMode(uint8_t mode);
protected:
/* current lightMode (0 = reactive, 1 = HID only, 2 = mixed (HID+button presses), 3 = mixed invert) */
uint8_t lightMode = 2;
/* byte array to receive HID reports from the PC */
byte led_data[5];
/* Implementation of the PUSBListNode */
EPTYPE_DESCRIPTOR_SIZE epType[1];
uint8_t protocol;
uint8_t idle;
int getInterface(uint8_t* interfaceCount);
int getDescriptor(USBSetup& setup);
bool setup(USBSetup& setup);
uint8_t getShortName(char *name);
};
extern POPNHID_ POPNHID;
+324
View File
@@ -0,0 +1,324 @@
#define BOUNCE_WITH_PROMPT_DETECTION
#include <Bounce2.h>
#if defined(ARDUINO_ARCH_SAM)
#include <Keypad.h>
#include <Keyboard.h>
#else
#include <EEPROM.h>
#endif
#include "POPNHID.h"
/* 1 frame (as declared in POPNHID.cpp) on highspeed USB spec is 125µs */
#define REPORT_DELAY 125
#define MILLIDEBOUNCE 15
POPNHID_ POPNHID;
/* Buttons + Lights declarations */
#if defined(ARDUINO_ARCH_SAM)
byte LightPins[] = {36, 38, 40, 42, 44, 46, 48, 50, 52, 37, 39, 41, 43, 45, 47, 49, 51, 53};
byte ButtonPins[] = {5, 6, 7, 8, 9, 10, 11, 12, 13, 4, 3, 2};
#else
uint8_t LightPins[] = {11,12,13,23,22,21,20,19,18};
uint8_t ButtonPins[] = {0,1,2,3,4,5,6,7,8,9,10};
#endif
const byte ButtonCount = sizeof(ButtonPins) / sizeof(ButtonPins[0]);
const byte LightCount = sizeof(LightPins) / sizeof(LightPins[0]);
Bounce buttons[ButtonCount];
#if defined(ARDUINO_ARCH_SAM)
/* Keypad declarations */
const byte ROWS = 4;
const byte COLS = 3;
/*
// To use the keypad as the numpad keys (will require to send numlock for it to work)
char numpad[ROWS][COLS] = {
{'\347', '\350', '\351'},
{'\344', '\345', '\346'},
{'\341', '\342', '\343'},
{'\352', ',', '\337'}
};
*/
/* This is to use the toprow keys instead */
char numpad[ROWS][COLS] = {
{'7', '8', '9'},
{'4', '5', '6'},
{'1', '2', '3'},
{'0', ',', '\337'}
};
/* This follows the Pop'n Music cabinet numpad pins order */
byte rowPins[ROWS] = {A3, A2, A1, A0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A4, A5, A6}; //connect to the column pinouts of the keypad
/* For mini keypad
byte rowPins[ROWS] = {A5, A0, A1, A3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A4, A6, A2}; //connect to the column pinouts of the keypad
*/
Keypad kpd = Keypad( makeKeymap(numpad), rowPins, colPins, ROWS, COLS );
#endif
/* SETUP */
void setup() {
// setup I/O for pins
for (int i = 0; i < ButtonCount; i++) {
buttons[i] = Bounce();
buttons[i].attach(ButtonPins[i], INPUT_PULLUP);
buttons[i].interval(MILLIDEBOUNCE);
}
for (int i = 0; i < LightCount; i++) {
pinMode(LightPins[i], OUTPUT);
}
#if defined(ARDUINO_ARCH_SAM)
kpd.setDebounceTime(30);
Keyboard.begin();
/* activate numlock if you are not using the toprow keys */
/* delay(2000);
Keyboard.press(136 + 83);
delay(500);
Keyboard.release(136+83);
*/
#else
uint8_t lightMode;
EEPROM.get(0, lightMode);
if (lightMode < 0 || lightMode > 3)
lightMode = 2;
POPNHID.setLightMode(lightMode);
#endif
//boot animation
uint16_t anim[] = {1, 4, 16, 64, 256, 128, 32, 8, 2};
animate(anim, 9, 100);
animate(anim, 9, 100);
uint16_t anim2[] = {1 + 4 + 16 + 64 + 256, 2 + 8 + 32 + 128};
animate(anim2, 2, 500);
animate(anim2, 2, 500);
}
/* LOOP */
unsigned long lastReport = 0;
uint32_t prevButtonsState = 0;
bool modeChanged = false;
void loop() {
/* BUTTONS */
uint32_t buttonsState = 0;
for (int i = 0; i < ButtonCount; i++) {
buttons[i].update();
int value = buttons[i].read();
if (value != HIGH){
buttonsState |= (uint32_t)1 << i;
} else {
buttonsState &= ~((uint32_t)1 << i);
}
}
/* USB DATA */
if ( ( (micros() - lastReport) >= REPORT_DELAY) )
{
POPNHID.sendState(buttonsState);
lastReport = micros();
prevButtonsState = buttonsState;
//check for HID-requested lightmode change
POPNHID.updateLightMode();
}
/* LAMPS */
switch (POPNHID.getLightMode())
{
/* Reactive mode, locally determined lamp data */
case 0:
but_lights(buttonsState & 0x1ff);
#if defined(ARDUINO_ARCH_SAM)
reactive_neon(buttonsState & 0x1ff);
#endif
break;
/* HID mode, only based on received HID data */
case 1:
POPNHID.updateLeds(0, false);
break;
/* Mixed inverse mode, received HID data and button state are combined then inverted */
case 3:
POPNHID.updateLeds(buttonsState & 0x1ff, true);
break;
/* Mixed mode, received HID data and button state are combined */
default:
POPNHID.updateLeds(buttonsState & 0x1ff, false);
break;
}
#if defined(ARDUINO_ARCH_SAM)
/* KEYPAD */
if (kpd.getKeys())
{
for (int i = 0; i < LIST_MAX; i++) // Scan the whole key list.
{
if ( kpd.key[i].stateChanged ) // Only find keys that have changed state.
{
switch (kpd.key[i].kstate) { // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
case PRESSED:
Keyboard.press(kpd.key[i].kchar);
break;
case HOLD:
break;
case RELEASED:
Keyboard.release(kpd.key[i].kchar);
break;
case IDLE:
break;
}
}
}
}
#endif
/* MANUAL LIGHTMODE UPDATE */
if ( buttonsState & 1024 ) {
if ( (buttonsState & 2) && (modeChanged == false)) {
modeChanged = true;
uint8_t mode = POPNHID.getLightMode()+1;
if (mode > 3) mode = 0;
POPNHID.setLightMode(mode);
#if defined(ARDUINO_ARCH_AVR)
EEPROM.put(0, mode);
#endif
}
else if (!(buttonsState&2)) {
modeChanged = false;
}
}
}
/* Light up button lights according to bitfield */
void but_lights(uint16_t lightDesc) {
for (int i = 0; i < 9; i++) {
if ((lightDesc >> i) & 1) {
digitalWrite(LightPins[i], HIGH);
} else {
digitalWrite(LightPins[i], LOW);
}
}
}
/* Light up pillars and top neons according to bitfield */
void neon_lights(uint16_t lightDesc) {
for (int i = 0; i < 9; i++) {
if ((lightDesc >> i) & 1) {
digitalWrite(LightPins[i + 9], HIGH);
} else {
digitalWrite(LightPins[i + 9], LOW);
}
}
}
/* Display animation on the cab according to a bitfield array */
void animate(uint16_t* tab, uint8_t n, int mswait) {
for (int i = 0; i < n; i++) {
but_lights(tab[i]);
#if defined(ARDUINO_ARCH_SAM)
neon_lights(tab[i]);
#endif
delay(mswait);
}
}
/* ARDUINO DUE ONLY FUNCTIONS */
#if defined(ARDUINO_ARCH_SAM)
/* Manage pillars and top neons in reactive mode */
uint16_t neon_anim[] = {16, 24, 28, 30, 31, 30, 28, 24};
int neon_anim_index = 0;
uint16_t pillar_state[] = {0, 0x140, 0xA0, 0x1E0};
int pillar_state_index = 0;
bool pillar_lit = false;
uint16_t prevState = 0;
unsigned long lastBlink = 0;
unsigned long lastNeonUpdate = 0;
unsigned long lastButtonAction = 0;
unsigned long actionRate = 0;
unsigned long neonRate = 200;
void reactive_neon(uint16_t buttonsState) {
uint16_t neons = 0;
unsigned long currTime = millis();
/*
* SIDE PILLARS
* When pressing any button the side pillars will blink for half a second
* The color is randomly chosen with blue being predominant, red rare and purple super rare
*/
if ( buttonsState != prevState )
{
if ( buttonsState != 0 ){
long randNumber = random(21);
if (randNumber == 0)
{
pillar_state_index = 3;
}
else if (randNumber < 3)
{
pillar_state_index = 1;
} else {
pillar_state_index = 2;
}
pillar_lit = true;
if (buttonsState != 0) lastButtonAction = currTime;
}
prevState = buttonsState;
} else { /* no state change, continue to blink for 50ms */
if (currTime - lastBlink > 50) {
pillar_lit = !pillar_lit;
lastBlink = currTime;
}
if (currTime - lastButtonAction > 500) {
pillar_state_index = 0;
}
}
if (pillar_lit)
neons |= pillar_state[pillar_state_index];
/*
* Adjusting top neon animation speed (should go faster when you hit buttons quickly)
*/
actionRate = currTime - lastButtonAction;
if (15*actionRate < neonRate)
neonRate *= 0.99995;
else if (actionRate > 10*neonRate){
neonRate = neonRate*1.01;
if (neonRate < 100) neonRate++;
}
if (neonRate > 400)
neonRate = 400;
if (neonRate < 40)
neonRate = 40;
/*
* Cycling through the top neon animation
*/
if ((currTime - lastNeonUpdate) > neonRate)
{
neon_anim_index++;
if (neon_anim_index > 7) neon_anim_index = 0;
lastNeonUpdate = currTime;
}
neons |= neon_anim[neon_anim_index];
/*
* Light the leds
*/
neon_lights(neons);
}
#endif