mirror of
https://github.com/Architeuthis-Flux/Jumperless.git
synced 2024-11-28 01:11:00 +01:00
Continously sample ADCs and expose them via USB HID
This commit is contained in:
parent
7ad7f2ce57
commit
ca2d0cc526
@ -65,14 +65,14 @@ extern "C" {
|
|||||||
// Device Configuration
|
// Device Configuration
|
||||||
//--------------------------------------------------------------------
|
//--------------------------------------------------------------------
|
||||||
|
|
||||||
#define CFG_TUD_ENDOINT0_SIZE 64
|
#define CFG_TUD_ENDOINT0_SIZE 256
|
||||||
|
|
||||||
#define CFG_TUD_CDC 2
|
#define CFG_TUD_CDC 2
|
||||||
|
|
||||||
#define CFG_TUD_MSC 1
|
#define CFG_TUD_MSC 0
|
||||||
#define CFG_TUD_HID 1
|
#define CFG_TUD_HID 1
|
||||||
#define CFG_TUD_MIDI 1
|
#define CFG_TUD_MIDI 0
|
||||||
#define CFG_TUD_VENDOR 1
|
#define CFG_TUD_VENDOR 0
|
||||||
|
|
||||||
// CDC FIFO size of TX and RX
|
// CDC FIFO size of TX and RX
|
||||||
#define CFG_TUD_CDC_RX_BUFSIZE 256
|
#define CFG_TUD_CDC_RX_BUFSIZE 256
|
||||||
|
69
JumperlessNano/src/AdcHid.cpp
Normal file
69
JumperlessNano/src/AdcHid.cpp
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
#ifdef USE_TINYUSB
|
||||||
|
#include <Adafruit_TinyUSB.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef CFG_TUSB_CONFIG_FILE
|
||||||
|
#include CFG_TUSB_CONFIG_FILE
|
||||||
|
#else
|
||||||
|
#include "tusb_config.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <ADCInput.h>
|
||||||
|
|
||||||
|
// Other parts of the code can just read from here instead of using `analogRead`
|
||||||
|
volatile uint16_t adcReadings[4] = { 0, 0, 0, 0 };
|
||||||
|
|
||||||
|
Adafruit_USBD_HID USBHID;
|
||||||
|
|
||||||
|
uint8_t desc_hid_report[] = {
|
||||||
|
TUD_HID_REPORT_DESC_GENERIC_INOUT(sizeof(adcReadings))
|
||||||
|
};
|
||||||
|
|
||||||
|
// samples all four pins, round-robin
|
||||||
|
ADCInput adc(A0, A1, A2, A3);
|
||||||
|
|
||||||
|
static uint16_t getReportCallback(uint8_t report_id, hid_report_type_t report_type,
|
||||||
|
uint8_t *buffer, uint16_t reqlen) {
|
||||||
|
// ignore this.
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void setReportCallback(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
|
||||||
|
{
|
||||||
|
// ignore this as well.
|
||||||
|
}
|
||||||
|
|
||||||
|
// called when the ADC buffer has data
|
||||||
|
static void adcCallback() {
|
||||||
|
if (adc.available() < 4) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// need a separate buffer here, since `sendReport` can't accept a volatile buffer.
|
||||||
|
uint16_t buf[4];
|
||||||
|
buf[0] = adcReadings[0] = adc.read();
|
||||||
|
buf[1] = adcReadings[1] = adc.read();
|
||||||
|
buf[2] = adcReadings[2] = adc.read();
|
||||||
|
buf[3] = adcReadings[3] = adc.read();
|
||||||
|
|
||||||
|
// only send readings if HID is "ready". Otherwise `sendReport` just hangs.
|
||||||
|
if (tud_hid_n_ready(0)) {
|
||||||
|
USBHID.sendReport(0, buf, sizeof(buf));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setupAdcHidStuff() {
|
||||||
|
USBHID.setPollInterval(1);
|
||||||
|
USBHID.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
|
||||||
|
USBHID.setStringDescriptor("Jumperless USB Analog");
|
||||||
|
USBHID.setReportCallback(getReportCallback, setReportCallback);
|
||||||
|
USBHID.begin();
|
||||||
|
|
||||||
|
adc.setBuffers(4, 32);
|
||||||
|
adc.onReceive(adcCallback);
|
||||||
|
// the sample rate passed here is already adjusted for the number of pins.
|
||||||
|
// in other words: passing 1000 actually samples at a rate of 4000, such that
|
||||||
|
// values for all 4 pins are available every millisecond.
|
||||||
|
adc.begin(1000);
|
||||||
|
}
|
11
JumperlessNano/src/AdcHid.h
Normal file
11
JumperlessNano/src/AdcHid.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
#ifndef JUMPERLESS_HID_H
|
||||||
|
#define JUMPERLESS_HID_H
|
||||||
|
|
||||||
|
// if `setupAdcHidStuff` was called, this always contains somewhat up-to-date ADC readings
|
||||||
|
extern uint16_t adcReadings[4];
|
||||||
|
|
||||||
|
void setupAdcHidStuff();
|
||||||
|
|
||||||
|
#endif
|
@ -45,6 +45,8 @@
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "AdcHid.h"
|
||||||
|
|
||||||
Adafruit_USBD_CDC USBSer1;
|
Adafruit_USBD_CDC USBSer1;
|
||||||
|
|
||||||
int supplySwitchPosition = 0;
|
int supplySwitchPosition = 0;
|
||||||
@ -77,13 +79,15 @@ void setup()
|
|||||||
|
|
||||||
USBSer1.begin(115200);
|
USBSer1.begin(115200);
|
||||||
|
|
||||||
|
setupAdcHidStuff();
|
||||||
|
|
||||||
#ifdef EEPROMSTUFF
|
#ifdef EEPROMSTUFF
|
||||||
EEPROM.begin(256);
|
EEPROM.begin(256);
|
||||||
debugFlagInit();
|
debugFlagInit();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
initADC();
|
//initADC();
|
||||||
delay(1);
|
delay(1);
|
||||||
initDAC(); // also sets revisionNumber
|
initDAC(); // also sets revisionNumber
|
||||||
delay(1);
|
delay(1);
|
||||||
|
Loading…
Reference in New Issue
Block a user