TinyUVC 0.1.0
Header-only USB Video Class (UVC) device: turn an MCU into a USB webcam
Loading...
Searching...
No Matches
UVCPlatformTinyUSB.h
Go to the documentation of this file.
1#pragma once
11#include <Arduino.h>
12#include <Adafruit_TinyUSB.h>
13#include <stdlib.h>
14
15#include "../UVCDevice.h"
16
17namespace tinyuvc {
18namespace platform {
19
20inline uint16_t maxPacketSize() { return TUD_OPT_HIGH_SPEED ? 512 : 64; }
21
23class UVCTinyUSBInterface : public Adafruit_USBD_Interface {
24 public:
25 uint16_t getInterfaceDescriptor(uint8_t itfnum_deprecated, uint8_t *buf,
26 uint16_t bufsize) override {
27 (void)itfnum_deprecated;
29 if (dev == nullptr) return 0;
30 const uint16_t len = dev->descriptorLength();
31 if (buf == nullptr) return len; // length query
32 if (bufsize < len) {
33 TINYUVC_LOGE("configuration buffer too small (TINYUVC_CONFIG_BUFFER_SIZE)");
34 return 0;
35 }
36 const uint8_t itf = TinyUSBDevice.allocInterface(2);
37 const uint8_t ep = TinyUSBDevice.allocEndpoint(TUSB_DIR_IN);
38 return dev->buildDescriptor(buf, bufsize, itf, ep, maxPacketSize(), _strid);
39 }
40};
41
43 static UVCTinyUSBInterface itf;
44 return itf;
45}
46
47inline bool begin(UVCDevice &dev) {
48 // cores without built in TinyUSB support (e.g. STM32) do not start it
49 if (!TinyUSBDevice.isInitialized()) TinyUSBDevice.begin(0);
50 // the default 256 byte buffer is too small for UVC + CDC
51 static uint8_t config[TINYUVC_CONFIG_BUFFER_SIZE];
52 TinyUSBDevice.setConfigurationBuffer(config, sizeof(config));
53 UVCTinyUSBInterface &itf = tinyusbInterface();
54 itf.setStringDescriptor(dev.name());
55 if (!TinyUSBDevice.addInterface(itf)) return false;
56 // if the host already enumerated the device it does not know the camera:
57 // re-enumerate
58 if (TinyUSBDevice.mounted()) {
59 TinyUSBDevice.detach();
60 delay(10);
61 TinyUSBDevice.attach();
62 }
63 return true;
64}
65
66inline void end(UVCDevice &) {} // interfaces can not be removed
67
68inline void idle() {
69 yield(); // runs tud_task() on STM32
70 delay(1);
71}
72inline uint32_t millis() { return ::millis(); }
73inline uint32_t micros() { return ::micros(); }
74
75inline uint8_t *allocBuffer(size_t size) {
76 // 4 byte alignment is required by the USB DMA of the supported MCUs
77 void *p = malloc((size + 3) & ~(size_t)3);
78 return (uint8_t *)p;
79}
80inline void freeBuffer(uint8_t *buf) { free(buf); }
81
82inline AppLock::AppLock() {}
83inline AppLock::~AppLock() {}
84
85} // namespace platform
86} // namespace tinyuvc
#define TINYUVC_CONFIG_BUFFER_SIZE
Definition UVCConfig.h:38
#define TINYUVC_LOGE(...)
Definition UVCLogger.h:75
USB Video Class (UVC 1.1) device - a USB webcam.
Definition UVCDevice.h:95
static UVCDevice *& instance()
The active device.
Definition UVCDevice.h:346
const char * name() const
Definition UVCDevice.h:176
uint16_t descriptorLength() const
Length of the function descriptors.
Definition UVCDevice.h:352
uint16_t buildDescriptor(uint8_t *buf, uint16_t bufLen, uint8_t controlItf, uint8_t endpointIn, uint16_t maxPacketSize, uint8_t stringIndex)
Definition UVCDevice.h:361
The camera as interface of Adafruit_USBD_Device.
Definition UVCPlatformTinyUSB.h:23
uint16_t getInterfaceDescriptor(uint8_t itfnum_deprecated, uint8_t *buf, uint16_t bufsize) override
Definition UVCPlatformTinyUSB.h:25
void freeBuffer(uint8_t *buf)
Definition UVCPlatformDesktop.h:56
bool begin(UVCDevice &)
Registers the UVC function with the USB stack (and starts it if needed)
Definition UVCPlatformDesktop.h:29
uint8_t * allocBuffer(size_t size)
Allocates the transfer buffer (DMA capable, aligned)
Definition UVCPlatformDesktop.h:53
void end(UVCDevice &)
Unregisters the UVC function, where the USB stack supports that.
Definition UVCPlatformDesktop.h:30
uint16_t maxPacketSize()
Max packet size of the bulk endpoint (64 full speed, 512 high speed)
Definition UVCPlatformDesktop.h:31
uint32_t millis()
Definition UVCPlatformDesktop.h:41
void idle()
Called while waiting: lets the USB stack make progress.
Definition UVCPlatformDesktop.h:33
uint32_t micros()
Definition UVCPlatformDesktop.h:47
UVCTinyUSBInterface & tinyusbInterface()
Definition UVCPlatformTinyUSB.h:42
TinyUVC: USB Video Class (UVC) device for Arduino and the desktop.
~AppLock()
Definition UVCPlatformDesktop.h:59
AppLock()
Definition UVCPlatformDesktop.h:58