-
Notifications
You must be signed in to change notification settings - Fork 767
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2805 from adafruit/ch552_examples
CH552 examples
- Loading branch information
Showing
5 changed files
with
373 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
#define NEOPIXEL_PIN 10 | ||
#define A0 11 | ||
#define A1 14 | ||
#define A2 15 | ||
#define MOSI A2 | ||
#define MISO 16 | ||
#define SCK 17 | ||
#define RX 30 | ||
#define TX 31 | ||
#define A3 32 | ||
#define SCL 33 | ||
#define SDA 34 | ||
|
||
int led = MISO; | ||
|
||
void setup() { | ||
pinMode(led, OUTPUT); | ||
} | ||
|
||
void loop() { | ||
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) | ||
delay(1000); // wait for a second | ||
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW | ||
delay(1000); // wait for a second | ||
} |
35 changes: 35 additions & 0 deletions
35
QT_Py_CH552_Examples/CapTouch_QTPyCH552/CapTouch_QTPyCH552.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <TouchKey.h> | ||
|
||
uint8_t count = 0; | ||
uint8_t state = 0; | ||
|
||
void setup() { | ||
while (!USBSerial()); // wait for serial port to connect. Needed for native USB port only | ||
delay(100); | ||
USBSerial_println("QT Py CH552 Cap Touch Test"); | ||
USBSerial_println("Uses pin A0 (P1.1)"); | ||
TouchKey_begin((1 << 1)); //Enable channel P1.1/A0 | ||
} | ||
|
||
void loop() { | ||
// put your main code here, to run repeatedly: | ||
TouchKey_Process(); | ||
uint8_t touchResult = TouchKey_Get(); | ||
if (touchResult) { | ||
if (state == 0) { | ||
count += 1; | ||
state = 1; | ||
USBSerial_print("TIN1.1 touched "); | ||
USBSerial_print(count); | ||
USBSerial_println(" times"); | ||
} | ||
} else { | ||
state = 0; | ||
} | ||
delay(1000); | ||
|
||
} |
165 changes: 165 additions & 0 deletions
165
QT_Py_CH552_Examples/I2C_AHT20_QTPyCH552/I2C_AHT20_QTPyCH552.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// https://chat.openai.com/share/5dddee44-3196-4a6b-b445-58ac6ef18501 | ||
|
||
#include <SoftI2C.h> | ||
|
||
extern uint8_t scl_pin; | ||
extern uint8_t sda_pin; | ||
|
||
void Wire_begin(uint8_t scl, uint8_t sda); | ||
bool Wire_scan(uint8_t i2caddr); | ||
bool Wire_writeBytes(uint8_t i2caddr, uint8_t *data, uint8_t bytes); | ||
bool Wire_readBytes(uint8_t i2caddr, uint8_t *data, uint8_t bytes); | ||
bool Wire_readRegister(uint8_t i2caddr, uint8_t regaddr, uint8_t *data, uint8_t bytes); | ||
|
||
bool readAHT20(float *temperature, float *humidity); | ||
#define AHTX0_I2CADDR_DEFAULT 0x38 | ||
|
||
void setup() { | ||
while (!USBSerial()); // wait for serial port to connect. Needed for native USB port only | ||
delay(100); | ||
|
||
USBSerial_println("CH552 QT Py I2C sensor test"); | ||
Wire_begin(33, 34); // set up I2C on CH552 QT Py | ||
|
||
USBSerial_print("I2C Scan: "); | ||
for (uint8_t a=0; a<=0x7F; a++) { | ||
if (!Wire_scan(a)) continue; | ||
USBSerial_print("0x"); | ||
USBSerial_print(a, HEX); | ||
USBSerial_print(", "); | ||
} | ||
USBSerial_println(); | ||
|
||
if (! Wire_scan(AHTX0_I2CADDR_DEFAULT)) { | ||
USBSerial_println("No AHT20 found!"); | ||
while (1); | ||
} | ||
} | ||
|
||
void loop() { | ||
delay(100); | ||
|
||
float t, h; | ||
if (!readAHT20(&t, &h)) { | ||
USBSerial_println("Failed to read from AHT20"); | ||
} | ||
USBSerial_print("Temp: "); | ||
USBSerial_print(t); | ||
USBSerial_print(" *C, Hum: "); | ||
USBSerial_print(h); | ||
USBSerial_println(" RH%"); | ||
} | ||
|
||
/*********************** AHT20 'driver */ | ||
|
||
#define AHTX0_CMD_TRIGGER 0xAC | ||
#define AHTX0_STATUS_BUSY 0x80 | ||
|
||
bool AHT20_getStatus(uint8_t *status) { | ||
return Wire_readBytes(AHTX0_I2CADDR_DEFAULT, status, 1); | ||
} | ||
|
||
bool readAHT20(float *temperature, float *humidity) { | ||
uint8_t cmd[3] = {AHTX0_CMD_TRIGGER, 0x33, 0x00}; | ||
uint8_t data[6], status; | ||
uint32_t rawHumidity, rawTemperature; | ||
|
||
// Trigger AHT20 measurement | ||
if (!Wire_writeBytes(AHTX0_I2CADDR_DEFAULT, cmd, 3)) { | ||
return false; | ||
} | ||
|
||
// Wait until the sensor is no longer busy | ||
do { | ||
if (!AHT20_getStatus(&status)) { | ||
return false; | ||
} | ||
delay(10); // Delay 10ms to wait for measurement | ||
} while (status & AHTX0_STATUS_BUSY); | ||
|
||
// Read the measurement data | ||
if (!Wire_readBytes(AHTX0_I2CADDR_DEFAULT, data, 6)) { | ||
return false; | ||
} | ||
|
||
// Parse humidity data | ||
rawHumidity = data[1]; | ||
rawHumidity = (rawHumidity << 8) | data[2]; | ||
rawHumidity = (rawHumidity << 4) | (data[3] >> 4); | ||
*humidity = ((float)rawHumidity * 100.0) / 0x100000; | ||
|
||
// Parse temperature data | ||
rawTemperature = (data[3] & 0x0F); | ||
rawTemperature = (rawTemperature << 8) | data[4]; | ||
rawTemperature = (rawTemperature << 8) | data[5]; | ||
*temperature = ((float)rawTemperature * 200.0 / 0x100000) - 50.0; | ||
|
||
return true; | ||
} | ||
|
||
/**************************** Wire I2C interface */ | ||
|
||
void Wire_begin(uint8_t scl, uint8_t sda) { | ||
scl_pin = scl; //extern variable in SoftI2C.h | ||
sda_pin = sda; | ||
I2CInit(); | ||
} | ||
|
||
bool Wire_scan(uint8_t i2caddr) { | ||
return Wire_writeBytes(i2caddr, NULL, 0); | ||
} | ||
|
||
bool Wire_readRegister(uint8_t i2caddr, uint8_t regaddr, uint8_t *data, uint8_t bytes) { | ||
if (!Wire_writeBytes(i2caddr, ®addr, 1)) { | ||
return false; | ||
} | ||
|
||
return Wire_readBytes(i2caddr, data, bytes); | ||
} | ||
|
||
|
||
bool Wire_writeBytes(uint8_t i2caddr, uint8_t *data, uint8_t bytes) { | ||
uint8_t ack_bit; | ||
|
||
I2CStart(); | ||
ack_bit = I2CSend(i2caddr << 1 | 0); // Shift address and append write bit | ||
if (ack_bit != 0) { | ||
I2CStop(); | ||
return false; | ||
} | ||
|
||
for (uint8_t i = 0; i < bytes; i++) { | ||
if (I2CSend(data[i]) != 0) { | ||
I2CStop(); | ||
return false; | ||
} | ||
} | ||
I2CStop(); | ||
return true; | ||
} | ||
|
||
bool Wire_readBytes(uint8_t i2caddr, uint8_t *data, uint8_t bytes) { | ||
uint8_t ack_bit; | ||
|
||
I2CStart(); | ||
ack_bit = I2CSend(i2caddr << 1 | 1); // Shift address and append read bit | ||
if (ack_bit != 0) { | ||
I2CStop(); | ||
return false; | ||
} | ||
|
||
for (uint8_t i = 0; i < bytes; i++) { | ||
data[i] = I2CRead(); | ||
if (i == bytes - 1) { | ||
I2CNak(); // NAK on last byte | ||
} else { | ||
I2CAck(); // ACK on other bytes | ||
} | ||
} | ||
|
||
I2CStop(); | ||
return true; | ||
} |
97 changes: 97 additions & 0 deletions
97
QT_Py_CH552_Examples/Neopixel_QTPyCH552/Neopixel_QTPyCH552.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <WS2812.h> | ||
|
||
#define NEOPIXEL_PIN P1_0 | ||
#define NUM_LEDS 1 | ||
|
||
#define COLOR_PER_LEDS 3 | ||
#define NUM_BYTES (NUM_LEDS*COLOR_PER_LEDS) | ||
#if NUM_BYTES > 255 | ||
#error "NUM_BYTES can not be larger than 255." | ||
#endif | ||
__xdata uint8_t ledData[NUM_BYTES]; | ||
|
||
/***********************************************************************/ | ||
uint8_t neopixel_brightness = 255; | ||
uint32_t Wheel(byte WheelPos); | ||
void rainbowCycle(uint8_t wait); | ||
|
||
#define NEOPIXEL_SHOW_FUNC CONCAT(neopixel_show_, NEOPIXEL_PIN) | ||
|
||
void neopixel_begin() { | ||
pinMode(NEOPIXEL_PIN, OUTPUT); //Possible to use other pins. | ||
} | ||
|
||
void neopixel_show() { | ||
NEOPIXEL_SHOW_FUNC(ledData, NUM_BYTES); //Possible to use other pins. | ||
} | ||
|
||
void neopixel_setPixelColor(uint8_t i, uint32_t c) { | ||
uint16_t r, g, b; | ||
r = (((c >> 16) & 0xFF) * neopixel_brightness) >> 8; | ||
g = (((c >> 8) & 0xFF) * neopixel_brightness) >> 8; | ||
b = ((c & 0xFF) * neopixel_brightness) >> 8; | ||
|
||
set_pixel_for_GRB_LED(ledData, i, r, g, b); | ||
} | ||
|
||
void neopixel_setBrightness(uint8_t b) { | ||
neopixel_brightness = b; | ||
} | ||
/***********************************************************************/ | ||
|
||
|
||
void setup() { | ||
neopixel_begin(); | ||
neopixel_setBrightness(50); | ||
} | ||
|
||
void loop() { | ||
rainbowCycle(5); | ||
} | ||
|
||
|
||
void rainbowCycle(uint8_t wait) { | ||
uint8_t i, j; | ||
|
||
for (j=0; j<255; j++) { | ||
for (i=0; i < NUM_LEDS; i++) { | ||
neopixel_setPixelColor(i, Wheel(((i * 256 / NUM_LEDS) + j) & 255)); | ||
} | ||
neopixel_show(); | ||
delay(wait); | ||
} | ||
} | ||
|
||
|
||
// Input a value 0 to 255 to get a color value. | ||
// The colours are a transition r - g - b - back to r. | ||
uint32_t Wheel(byte WheelPos) { | ||
uint8_t r, g, b; | ||
uint32_t c; | ||
|
||
if(WheelPos < 85) { | ||
r = WheelPos * 3; | ||
g = 255 - WheelPos * 3 ; | ||
b = 0; | ||
} else if(WheelPos < 170) { | ||
WheelPos -= 85; | ||
r = 255 - WheelPos * 3; | ||
g = 0; | ||
b = WheelPos * 3; | ||
} else { | ||
WheelPos -= 170; | ||
r = 0; | ||
g = WheelPos * 3; | ||
b = 255 - WheelPos * 3; | ||
} | ||
c = r; | ||
c <<= 8; | ||
c |= g; | ||
c <<= 8; | ||
c |= b; | ||
return c; | ||
} |
47 changes: 47 additions & 0 deletions
47
QT_Py_CH552_Examples/analogIn_QTPyCH552/analogIn_QTPyCH552.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
/* | ||
ReadAnalogVoltage | ||
Reads an analog input on pin P1.1, converts it to voltage, and prints the result to the Serial Monitor. | ||
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu). | ||
Attach the center pin of a potentiometer to pin P1.1, and the outside pins to +5V and ground. | ||
This example code is in the public domain. | ||
http://www.arduino.cc/en/Tutorial/ReadAnalogVoltage | ||
*/ | ||
|
||
#include <Serial.h> | ||
|
||
#define A0 11 | ||
#define A1 14 | ||
#define A2 15 // also MISO! | ||
#define A3 32 | ||
|
||
#define ANALOG_IN A3 | ||
#define VREF 3.3 | ||
|
||
// the setup routine runs once when you press reset: | ||
void setup() { | ||
// No need to init USBSerial | ||
|
||
// By default 8051 enable every pin's pull up resistor. Disable pull-up to get full input range. | ||
pinMode(ANALOG_IN, INPUT); | ||
} | ||
|
||
// the loop routine runs over and over again forever: | ||
void loop() { | ||
// read the input on analog pin 0, P1.1: | ||
int sensorValue = analogRead(ANALOG_IN); | ||
// Convert the analog reading (which goes from 0 - 255) to VREF: | ||
float voltage = sensorValue * (VREF / 255.0); | ||
// print out the value you read: | ||
USBSerial_println(voltage); | ||
// or with precision: | ||
//USBSerial_println(voltage,1); | ||
|
||
delay(10); | ||
} |