From 2102b2a6087fa21bb3dde37421ac4ddd0390a510 Mon Sep 17 00:00:00 2001 From: TrevKnows Date: Mon, 25 Nov 2024 12:28:52 -0500 Subject: [PATCH 1/2] Color detection project This is a program for a color detection project using the APD-9960 and Feather ESP32-S2 & S3 --- itsaSNAP Color Detection/code.py | 80 ++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100755 itsaSNAP Color Detection/code.py diff --git a/itsaSNAP Color Detection/code.py b/itsaSNAP Color Detection/code.py new file mode 100755 index 000000000..1ecf45ffe --- /dev/null +++ b/itsaSNAP Color Detection/code.py @@ -0,0 +1,80 @@ +# SPDX-FileCopyrightText: 2021 Anne Barela for Adafruit Industries +# +# SPDX-License-Identifier: MIT +'''This is a program for a color detection project using the APD-9960 and Feather ESP32-S2 & S3''' + +import os +import ssl +import time +import wifi +import board +import busio +import socketpool +import adafruit_requests +from adafruit_apds9960.apds9960 import APDS9960 +from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError + +# WiFi and Adafruit IO setup +aio_username = os.getenv("aio_username") +aio_key = os.getenv("aio_key") + +wifi.radio.connect( + os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD") +) +print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!") + +# Initialize network pool and Adafruit IO HTTP +pool = socketpool.SocketPool(wifi.radio) +requests = adafruit_requests.Session(pool, ssl.create_default_context()) +io = IO_HTTP(aio_username, aio_key, requests) + +# Set up I2C connection and APDS9960 sensor +i2c = busio.I2C(board.SCL, board.SDA) +apds = APDS9960(i2c) +apds.enable_proximity = True +apds.enable_color = True + +# Adafruit IO feed setup +try: + hue_feed = io.get_feed("hue") +except AdafruitIO_RequestError: + hue_feed = io.create_new_feed("hue") + + +# Function to convert RGB to Hue +def rgb_to_hue(r: int, g: int, b: int) -> float: + r_norm = r / 255.0 + g_norm = g / 255.0 + b_norm = b / 255.0 + c_max = max(r_norm, g_norm, b_norm) + c_min = min(r_norm, g_norm, b_norm) + delta = c_max - c_min + + if delta == 0: + calculated_hue = 0 + elif c_max == r_norm: + calculated_hue = (60 * ((g_norm - b_norm) / delta) + 360) % 360 + elif c_max == g_norm: + calculated_hue = (60 * ((b_norm - r_norm) / delta) + 120) % 360 + elif c_max == b_norm: + calculated_hue = (60 * ((r_norm - g_norm) / delta) + 240) % 360 + else: + calculated_hue = 0 # Fallback case + + return calculated_hue + + +# Main loop +while True: + color_data = apds.color_data + red_value, green_value, blue_value = color_data[0], color_data[1], color_data[2] + hue_value = round(rgb_to_hue(red_value, green_value, blue_value)) + print(f"Detected Hue: {hue_value}") + + try: + io.send_data(hue_feed["key"], hue_value) + print(f"Sent Hue value {hue_value} to Adafruit IO feed 'hue'") + except AdafruitIO_RequestError as e: + print(f"Error sending data: {e}") + + time.sleep(3) From 5532a6e33e8d6aa3eeb1b5ea20e67e7bcd2be4d1 Mon Sep 17 00:00:00 2001 From: TrevKnows Date: Mon, 25 Nov 2024 13:50:42 -0500 Subject: [PATCH 2/2] Folder name change --- .../code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename {itsaSNAP Color Detection => ESP32-S2_TFT_Color_Detection}/code.py (97%) diff --git a/itsaSNAP Color Detection/code.py b/ESP32-S2_TFT_Color_Detection/code.py similarity index 97% rename from itsaSNAP Color Detection/code.py rename to ESP32-S2_TFT_Color_Detection/code.py index 1ecf45ffe..d3ca9fae8 100755 --- a/itsaSNAP Color Detection/code.py +++ b/ESP32-S2_TFT_Color_Detection/code.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2021 Anne Barela for Adafruit Industries +# SPDX-FileCopyrightText: 2024 Trevor Beaton for Adafruit Industries # # SPDX-License-Identifier: MIT '''This is a program for a color detection project using the APD-9960 and Feather ESP32-S2 & S3'''