Skip to content

QT Py CH32V203 examples #2878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Binary file not shown.
22 changes: 22 additions & 0 deletions QT_Py_CH32_Examples/Blink_QTPyCH32/Blink_QTPyCH32.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2024 Ha Thach for Adafruit Industries
//
// SPDX-License-Identifier: MIT

#include <Arduino.h>
#include <Adafruit_TinyUSB.h> // required for USB Serial

const int led = PA6;

void setup () {
pinMode(led, OUTPUT);
}

void loop () {
Serial.println("LED on");
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second

Serial.println("LED off");
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Empty file.
Binary file not shown.
174 changes: 174 additions & 0 deletions QT_Py_CH32_Examples/HID_Keyboard_QTPyCH32/HID_Keyboard_QTPyCH32.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// SPDX-FileCopyrightText: 2024 Ha Thach for Adafruit Industries
//
// SPDX-License-Identifier: MIT

/*********************************************************************
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

MIT license, check LICENSE for more information
Copyright (c) 2019 Ha Thach for Adafruit Industries
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/

#include <Arduino.h>
#include "Adafruit_TinyUSB.h"
#include <Adafruit_NeoPixel.h>

const int neopixel_pin = PA4;
#define LED_COUNT 1
Adafruit_NeoPixel pixels(LED_COUNT, neopixel_pin, NEO_GRB + NEO_KHZ800);

//------------- Input Pins -------------//
// Array of pins and its keycode.
// Notes: these pins can be replaced by PIN_BUTTONn if defined in setup()

uint8_t pins[] = {PB1, PB0, PA1, PA0}; //A0, A1, A2, A3

// HID report descriptor using TinyUSB's template
// Single Report (no ID) descriptor
uint8_t const desc_hid_report[] = {
TUD_HID_REPORT_DESC_KEYBOARD()
};

// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid;


// number of pins
uint8_t pincount = sizeof(pins) / sizeof(pins[0]);

// For keycode definition check out https://github.com/hathach/tinyusb/blob/master/src/class/hid/hid.h
uint8_t hidcode[] = {HID_KEY_0, HID_KEY_1, HID_KEY_2, HID_KEY_3};

bool activeState = false;

// the setup function runs once when you press reset or power the board
void setup() {
pixels.begin();
// Manual begin() is required on core without built-in support e.g. mbed rp2040
if (!TinyUSBDevice.isInitialized()) {
TinyUSBDevice.begin(0);
}

// Setup HID
usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
usb_hid.setStringDescriptor("TinyUSB Keyboard");

// Set up output report (on control endpoint) for Capslock indicator
usb_hid.setReportCallback(NULL, hid_report_callback);

usb_hid.begin();

// overwrite input pin with PIN_BUTTONx
#ifdef PIN_BUTTON1
pins[0] = PIN_BUTTON1;
#endif

#ifdef PIN_BUTTON2
pins[1] = PIN_BUTTON2;
#endif

#ifdef PIN_BUTTON3
pins[2] = PIN_BUTTON3;
#endif

#ifdef PIN_BUTTON4
pins[3] = PIN_BUTTON4;
#endif

// Set up pin as input
for (uint8_t i = 0; i < pincount; i++) {
pinMode(pins[i], activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
}
}

void process_hid() {
// used to avoid send multiple consecutive zero report for keyboard
static bool keyPressedPreviously = false;

uint8_t count = 0;
uint8_t keycode[6] = {0};

// scan normal key and send report
for (uint8_t i = 0; i < pincount; i++) {
if (activeState == digitalRead(pins[i])) {
// if pin is active (low), add its hid code to key report
keycode[count++] = hidcode[i];

// 6 is max keycode per report
if (count == 6) break;
}
}

if (TinyUSBDevice.suspended() && count) {
// Wake up host if we are in suspend mode
// and REMOTE_WAKEUP feature is enabled by host
TinyUSBDevice.remoteWakeup();
}

// skip if hid is not ready e.g still transferring previous report
if (!usb_hid.ready()) return;

if (count) {
// Send report if there is key pressed
uint8_t const report_id = 0;
uint8_t const modifier = 0;

keyPressedPreviously = true;
usb_hid.keyboardReport(report_id, modifier, keycode);
} else {
// Send All-zero report to indicate there is no keys pressed
// Most of the time, it is, though we don't need to send zero report
// every loop(), only a key is pressed in previous loop()
if (keyPressedPreviously) {
keyPressedPreviously = false;
usb_hid.keyboardRelease(0);
}
}
}

void loop() {
#ifdef TINYUSB_NEED_POLLING_TASK
// Manual call tud_task since it isn't called by Core's background
TinyUSBDevice.task();
#endif

// not enumerated()/mounted() yet: nothing to do
if (!TinyUSBDevice.mounted()) {
return;
}

// poll gpio once each 2 ms
static uint32_t ms = 0;
if (millis() - ms > 2) {
ms = millis();
process_hid();
}
}

void setLED(bool state) {
pixels.setPixelColor(0, pixels.Color(0, state ? 150 : 0, 0));
pixels.show();
}

// Output report callback for LED indicator such as Caplocks
void hid_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) {
(void) report_id;
(void) bufsize;

// LED indicator is output report with only 1 byte length
if (report_type != HID_REPORT_TYPE_OUTPUT) return;

// The LED bit map is as follows: (also defined by KEYBOARD_LED_* )
// Kana (4) | Compose (3) | ScrollLock (2) | CapsLock (1) | Numlock (0)
uint8_t ledIndicator = buffer[0];

// turn on LED if capslock is set
setLED(ledIndicator & KEYBOARD_LED_CAPSLOCK);
}
Empty file.
Binary file added QT_Py_CH32_Examples/I2C_Scan/I2C_Scan.bin
Binary file not shown.
81 changes: 81 additions & 0 deletions QT_Py_CH32_Examples/I2C_Scan/I2C_Scan.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// SPDX-FileCopyrightText: 2023 Carter Nelson for Adafruit Industries
//
// SPDX-License-Identifier: MIT

#include <Adafruit_TestBed.h>
extern Adafruit_TestBed TB;

#define DEFAULT_I2C_PORT &Wire

// Some boards have TWO I2C ports, how nifty. We should scan both
#if defined(ARDUINO_ARCH_RP2040) \
|| defined(ARDUINO_ADAFRUIT_QTPY_ESP32S2) \
|| defined(ARDUINO_ADAFRUIT_QTPY_ESP32S3_NOPSRAM) \
|| defined(ARDUINO_ADAFRUIT_QTPY_ESP32S3) \
|| defined(ARDUINO_ADAFRUIT_QTPY_ESP32_PICO) \
|| defined(ARDUINO_SAM_DUE) \
|| defined(ARDUINO_ARCH_RENESAS_UNO)
#define SECONDARY_I2C_PORT &Wire1
#endif

void setup() {
Serial.begin(115200);

// Wait for Serial port to open
while (!Serial) {
delay(10);
}
delay(500);
Serial.println("Adafruit I2C Scanner");

#if defined(ARDUINO_ADAFRUIT_QTPY_ESP32S2) || \
defined(ARDUINO_ADAFRUIT_QTPY_ESP32S3_NOPSRAM) || \
defined(ARDUINO_ADAFRUIT_QTPY_ESP32S3) || \
defined(ARDUINO_ADAFRUIT_QTPY_ESP32_PICO)
// ESP32 is kinda odd in that secondary ports must be manually
// assigned their pins with setPins()!
Wire1.setPins(SDA1, SCL1);
#endif

#if defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2)
// turn on the I2C power by setting pin to opposite of 'rest state'
pinMode(PIN_I2C_POWER, INPUT);
delay(1);
bool polarity = digitalRead(PIN_I2C_POWER);
pinMode(PIN_I2C_POWER, OUTPUT);
digitalWrite(PIN_I2C_POWER, !polarity);
#endif

#if defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2_TFT)
pinMode(TFT_I2C_POWER, OUTPUT);
digitalWrite(TFT_I2C_POWER, HIGH);
#endif

#if defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2_REVTFT)
pinMode(TFT_I2C_POWER, OUTPUT);
digitalWrite(TFT_I2C_POWER, HIGH);
#endif

#if defined(ADAFRUIT_FEATHER_ESP32_V2)
// Turn on the I2C power by pulling pin HIGH.
pinMode(NEOPIXEL_I2C_POWER, OUTPUT);
digitalWrite(NEOPIXEL_I2C_POWER, HIGH);
#endif
}

void loop() {
Serial.println("");
Serial.println("");

Serial.print("Default port (Wire) ");
TB.theWire = DEFAULT_I2C_PORT;
TB.printI2CBusScan();

#if defined(SECONDARY_I2C_PORT)
Serial.print("Secondary port (Wire1) ");
TB.theWire = SECONDARY_I2C_PORT;
TB.printI2CBusScan();
#endif

delay(3000); // wait 3 seconds
}
Empty file.
Binary file not shown.
29 changes: 29 additions & 0 deletions QT_Py_CH32_Examples/Neopixel_QTPyCH32/Neopixel_QTPyCH32.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
//
// SPDX-License-Identifier: MIT

#include <Arduino.h>
#include <Adafruit_TinyUSB.h> // required for USB Serial
#include <Adafruit_NeoPixel.h>

const int neopixel_pin = PA4;
#define LED_COUNT 1
Adafruit_NeoPixel pixels(LED_COUNT, neopixel_pin, NEO_GRB + NEO_KHZ800);

void setup () {
pixels.begin();
}

uint16_t firstPixelHue = 0;

void loop() {
firstPixelHue += 256;
for(int i=0; i<pixels.numPixels(); i++) {
int pixelHue = firstPixelHue + (i * 65536L / pixels.numPixels());
pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(pixelHue)));
}
pixels.show();

delay(10);

}
Loading