diff --git a/Neo_Trinkey_Examples/Python_AdafruitIO_Color_Picker/Arduino_Serial_Color_Picker/Arduino_Serial_Color_Picker.ino b/Neo_Trinkey_Examples/Python_AdafruitIO_Color_Picker/Arduino_Serial_Color_Picker/Arduino_Serial_Color_Picker.ino new file mode 100644 index 000000000..2c53cb65b --- /dev/null +++ b/Neo_Trinkey_Examples/Python_AdafruitIO_Color_Picker/Arduino_Serial_Color_Picker/Arduino_Serial_Color_Picker.ino @@ -0,0 +1,55 @@ +// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include + +#define NUMPIXELS 30 +Adafruit_NeoPixel neostrip(NUMPIXELS, PIN_DATA, NEO_GRB + NEO_KHZ800); + +char colorBuffer[9]; // Buffer to hold the incoming color data (0x000000) +int bufferIndex = 0; // Index for the buffer + +void setup() { + Serial.begin(115200); + + neostrip.begin(); + neostrip.setBrightness(25); + neostrip.show(); + memset(colorBuffer, 0, sizeof(colorBuffer)); // Clear the buffer +} + +void loop() { + // Check if data is available on the serial port + while (Serial.available() > 0) { + char incomingByte = Serial.read(); + + // Check if the incoming byte is part of the color data + if (isxdigit(incomingByte) || incomingByte == 'x') { + colorBuffer[bufferIndex++] = incomingByte; // Add the byte to the buffer + } + + // If the buffer is full, process the color data + if (bufferIndex == 8) { + colorBuffer[8] = '\0'; // Null-terminate the string + + // Convert the hex string to a 32-bit integer + uint32_t color = strtoul(colorBuffer, NULL, 16); + + // Extract RGB values from the color + uint8_t r = (color >> 16) & 0xFF; + uint8_t g = (color >> 8) & 0xFF; + uint8_t b = color & 0xFF; + + // Set the NeoPixels to the received color + for (int i = 0; i < NUMPIXELS; i++) { + neostrip.setPixelColor(i, neostrip.Color(r, g, b)); + } + neostrip.show(); + + // Clear the buffer and reset the bufferIndex + memset(colorBuffer, 0, sizeof(colorBuffer)); + bufferIndex = 0; + } + } +} diff --git a/Neo_Trinkey_Examples/Python_AdafruitIO_Color_Picker/Arduino_Serial_Color_Picker/Arduino_Serial_Color_Picker.uf2 b/Neo_Trinkey_Examples/Python_AdafruitIO_Color_Picker/Arduino_Serial_Color_Picker/Arduino_Serial_Color_Picker.uf2 new file mode 100644 index 000000000..04c8a82f4 Binary files /dev/null and b/Neo_Trinkey_Examples/Python_AdafruitIO_Color_Picker/Arduino_Serial_Color_Picker/Arduino_Serial_Color_Picker.uf2 differ diff --git a/Neo_Trinkey_Examples/Python_AdafruitIO_Color_Picker/code.py b/Neo_Trinkey_Examples/Python_AdafruitIO_Color_Picker/code.py new file mode 100644 index 000000000..a59997b33 --- /dev/null +++ b/Neo_Trinkey_Examples/Python_AdafruitIO_Color_Picker/code.py @@ -0,0 +1,68 @@ +# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +import time +import serial +from Adafruit_IO import MQTTClient + +# Configuration +com_port = 'COM123' # Adjust this to your COM port +baud_rate = 115200 +FEED_ID = 'pixel-feed' +# Set to your Adafruit IO key. +# Remember, your key is a secret, +# so make sure not to publish it when you publish this code! +ADAFRUIT_IO_KEY = 'your-aio-key' + +# Set to your Adafruit IO username. +# (go to https://accounts.adafruit.com to find your username) +ADAFRUIT_IO_USERNAME = 'your-aio-username' + +print("Connecting to Adafruit IO...") + +# Initialize the serial connection +ser = serial.Serial(com_port, baud_rate) + +# Define callback functions +def connected(client): + client.subscribe(FEED_ID) +# pylint: disable=unused-argument, consider-using-sys-exit +def subscribe(client, userdata, mid, granted_qos): + # This method is called when the client subscribes to a new feed. + print(f"Subscribed to {FEED_ID}") + +def disconnected(client): + print('Disconnected from Adafruit IO!') + exit(1) + +def message(client, feed_id, payload): + print(f'Feed {feed_id} received new value: {payload}') + color_value = payload.strip().replace('#', '0x') # Replace # with 0x + ser.write(color_value.encode()) # Send the color value to the serial port + +# Initialize the Adafruit IO MQTT client +mqtt_client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) + +# Setup the callback functions +mqtt_client.on_connect = connected +mqtt_client.on_disconnect = disconnected +mqtt_client.on_message = message +mqtt_client.on_subscribe = subscribe + +# Connect to Adafruit IO +mqtt_client.connect() + +# Start a background thread to listen for MQTT messages +mqtt_client.loop_blocking() + +# Keep the script running +while True: + try: + time.sleep(1) + except KeyboardInterrupt: + print('Script interrupted by user') + break + +# Close the serial connection when done +ser.close()