diff --git a/Firefly.py b/Firefly.py new file mode 100644 index 0000000..29aa88a --- /dev/null +++ b/Firefly.py @@ -0,0 +1,34 @@ +import time +import board +import busio +from adafruit_apds9960.apds9960 import APDS9960 +import neopixel + +pixels = neopixel.NeoPixel(board.NEOPIXEL, 1) + +i2c = board.I2C() +apds = APDS9960(i2c) +apds.enable_color = True + +print("apds_time: ", apds.color_integration_time) +apds.color_integration_time = 64 + +while True: + # wait for color data to be ready + while not apds.color_data_ready: + time.sleep(0.005) + + # get the data and print the different channels + r, g, b, c = apds.color_data + print("red: ", r) + print("green: ", g) + print("blue: ", b) + print("clear: ", c) + print("apds_time_updated: ", apds.color_integration_time) + + # scale the measured 'c' to adjust the neopixel output + pixels.fill((c / 6, c / 6, 0)) + + time.sleep(0.5) + + diff --git a/README.md b/README.md index e61ff2f..8f7f48e 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,58 @@ University of Pennsylvania, ESE 5190: Intro to Embedded Systems, Lab 1 + + Dvisha Kishore Bakarania +
+ LinkedIn: [Dvisha Bakarania](https://www.linkedin.com/in/dvisha-bakarania-9370b2146?lipi=urn%3Ali%3Apage%3Ad_flagship3_profile_view_base_contact_details%3BNtJBfZpDTEKluhukjP7uqg%3D%3D) +
+ Tested on: Windows 11 Home Version 21H2 + +**Adafruit QT Py RP2040** and **Adafruit APDS9960 Proximity, Light, RGB, and Gesture Sensor** when come together, can help newbies create their own cool embedded system projects. Let's start! This project uses the sensor data (color, proximity, brightness) to implement a "firefly" and a custom real-time visualizer. All you will need is an Adafruit dev board with RP2040 and a sensor. + +**Requirements:** + +Adafruit QT Py RP2040 + + + +Now, let's implement a **firefly**! +
+So, the gist here is: the Neopixel led on the QT Py board should blink in synchronization with an actual firefly video. For this, we have used the brightness measurement feature of the sensor. Using the APDS9960, the brightness of the flashing firefly in the video is measured and this reading is used to control the pixels of the neopixel led by scaling the measured sensor output data to adjust the brightness/blinking of the neopixel so that it behaves just like the firefly in the video. + +Below is the snippet of the code: +
+
+ +
+
+
+![firefly](https://user-images.githubusercontent.com/114099174/192074256-795b89fb-5ea0-4ec2-806f-01ca170b0bf9.gif) +
+
+
+Code can also be found at: [Firefly](https://github.com/dvishab/ese5190-2022-lab1-firefly/blob/3a8a65675cf863e95c0b865e0bfb4c73537ea037/Firefly.py) +
+
+
+Up-next is the implementation of a **real-time visualizer** using keyboard emulator to get real time update of the detected sensor data. This can be done using HID keyboard library. The intention here is to get a real time update of any change in brightness level as sensed by the APDS9600. +
+We will be using two keys: 'O' and 'Backspace' to indicate any change at the input to the sensor. As and when there is an increase in the brightness level at the input to the sensor, any text editor in your laptop should start typing 'O's. If the brightness at the sensor input decreases then the 'Backspace' key should come into action and start erasing the 'O's so that the user is updated of the change(increase/decrease) at the sensor input. This can be done by adjusting the threshold so that an increase in brightness leads to typing 'O' and decrease leads to the erasing of 'O's using Backspace. + + + +Also, one important point to note here is that you might probably be using a keyboard to program your RP2040, so if the RP2040 is also sending keystrokes to the +laptop this can make it quite annoying to reprogram. So, there needs to be a way to stop it from endlessly writing the letter'O' or erasing, and that is to use an 'escape hatch'. The intention here is to make the system braek out of the main loop on sensing a partivular color. We will be using **'blue'** color. So, if I need to stop the program from endlessly writing/erasing, I should point my sensor to 'blue' color and it will break from the main loop as illustrated through the gif below: + +
+ +![real_time_visualizer](https://user-images.githubusercontent.com/114099174/192076211-7a4b6884-7763-4f30-9867-19a6dc39a080.gif) +
+
+
+Code snippet for implementing this: + + + +Code can also be found at: [Real Time Visualizer](https://github.com/dvishab/ese5190-2022-lab1-firefly/blob/c1ee2a235d8a2e01a7725ff37c65071b9b8b2d7a/Real_Time_Visualizer.py) - (TODO) YOUR NAME HERE - (TODO) LinkedIn, personal website, twitter, etc. - Tested on: (TODO) MacBook Pro (14-inch, 2021), macOS Monterey 12.5.1 -(TODO: Your README) -Include lab questions, screenshots, analysis, etc. (Remember, this is public, so don't put anything here you don't want to share with the world.) diff --git a/Real_Time_Visualizer.py b/Real_Time_Visualizer.py new file mode 100644 index 0000000..e554804 --- /dev/null +++ b/Real_Time_Visualizer.py @@ -0,0 +1,48 @@ +import time +import board +import busio +from adafruit_apds9960.apds9960 import APDS9960 +import digitalio +import usb_hid +from adafruit_hid.keyboard import Keyboard +from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS +from adafruit_hid.keycode import Keycode + +i2c = board.I2C() +apds = APDS9960(i2c) +apds.enable_color = True + +keys_pressed = [Keycode.O, Keycode.BACKSPACE] +control_key = Keycode.SHIFT +time.sleep(1) # Sleep for a bit to avoid a race condition on some systems +keyboard = Keyboard(usb_hid.devices) +keyboard_layout = KeyboardLayoutUS(keyboard) + +while True: + while not apds.color_data_ready: + time.sleep(0.005) + r, g, b, c = apds.color_data + print("red: ", r) + print("green: ", g) + print("blue: ", b) + print("clear: ", c) + + if b>50: + if c>1400: + j=0 + key = keys_pressed[j] + if isinstance(key, str): + keyboard_layout.write(key) + else: + keyboard.press(control_key, key) + keyboard.release_all() + else: + j=1 + key = keys_pressed[j] + if isinstance(key, str): + keyboard_layout.write(key) + else: + keyboard.press(control_key, key) + keyboard.release_all() + +