From 163a3a9f4e111ccebc55eaf938aa30fe23750240 Mon Sep 17 00:00:00 2001 From: Liz Date: Mon, 20 May 2024 14:51:59 -0400 Subject: [PATCH] picowbell tripler demos CircuitPython and Arduino demos for the PiCowbell Tripler --- .../Arduino_PiCowbell_Tripler_Demo.ino | 71 +++++++++++++++++++ Tripler_PiCowbell_Demos/CircuitPython/code.py | 54 ++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 Tripler_PiCowbell_Demos/Arduino_PiCowbell_Tripler_Demo/Arduino_PiCowbell_Tripler_Demo.ino create mode 100644 Tripler_PiCowbell_Demos/CircuitPython/code.py diff --git a/Tripler_PiCowbell_Demos/Arduino_PiCowbell_Tripler_Demo/Arduino_PiCowbell_Tripler_Demo.ino b/Tripler_PiCowbell_Demos/Arduino_PiCowbell_Tripler_Demo/Arduino_PiCowbell_Tripler_Demo.ino new file mode 100644 index 000000000..5dace8e43 --- /dev/null +++ b/Tripler_PiCowbell_Demos/Arduino_PiCowbell_Tripler_Demo/Arduino_PiCowbell_Tripler_Demo.ino @@ -0,0 +1,71 @@ +// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include +#include // Core graphics library +#include // Hardware-specific library for ST7789 +#include +#include + +#define LED LED_BUILTIN +#define TFT_CS 21 +#define TFT_RST -1 +#define TFT_DC 20 +#define NEO_PIN A2 + +uint32_t Wheel(byte WheelPos); + +Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST); + +Adafruit_NeoPixel pixel = Adafruit_NeoPixel(1, NEO_PIN, NEO_GRB + NEO_KHZ800); + +unsigned long lastMillis = 0; +uint8_t j = 0; + +void setup() { + Serial.begin(115200); + //while (!Serial) delay(1); // wait for serial port + tft.init(240, 240); + pinMode(LED, OUTPUT); + delay (100); + Serial.println("PiCowbell Tripler Demo"); + tft.setFont(&FreeSansBold24pt7b); + pixel.begin(); + pixel.setBrightness(50); + pixel.show(); // Initialize all pixels to 'off' + lastMillis = millis(); + +} + +void loop() { + if (millis() > (lastMillis + 5000)) { + digitalWrite(LED, HIGH); + // get the on-board voltage + float vsys = analogRead(A3) * 3 * 3.3 / 1023.0; + Serial.printf("Vsys: %0.1f V", vsys); + Serial.println(); + digitalWrite(LED, LOW); + tft.fillScreen(ST77XX_BLACK); + tft.setCursor(240 / 4, 240 / 2); + tft.setTextColor(ST77XX_WHITE); + tft.printf("%0.1f V", vsys); + lastMillis = millis(); + } + pixel.setPixelColor(0, Wheel(j++)); + pixel.show(); + delay(20); +} + +uint32_t Wheel(byte WheelPos) { + WheelPos = 255 - WheelPos; + if(WheelPos < 85) { + return pixel.Color(255 - WheelPos * 3, 0, WheelPos * 3); + } + if(WheelPos < 170) { + WheelPos -= 85; + return pixel.Color(0, WheelPos * 3, 255 - WheelPos * 3); + } + WheelPos -= 170; + return pixel.Color(WheelPos * 3, 255 - WheelPos * 3, 0); +} diff --git a/Tripler_PiCowbell_Demos/CircuitPython/code.py b/Tripler_PiCowbell_Demos/CircuitPython/code.py new file mode 100644 index 000000000..989d17894 --- /dev/null +++ b/Tripler_PiCowbell_Demos/CircuitPython/code.py @@ -0,0 +1,54 @@ +# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +import busio +import board +from analogio import AnalogIn +import adafruit_st7789 +import displayio +import neopixel +from rainbowio import colorwheel +from adafruit_display_text import label +from adafruit_ticks import ticks_ms, ticks_add, ticks_diff +from font_orbitron_bold_webfont_36 import FONT as orbitron_font + +displayio.release_displays() +spi = busio.SPI(clock=board.GP18, MOSI=board.GP19) +display_bus = displayio.FourWire(spi, command=board.GP20, chip_select=board.GP21, reset=None) +display = adafruit_st7789.ST7789(display_bus, width=240, height=240, rowstart=80, rotation=0) + +group = displayio.Group() +text = label.Label(orbitron_font, text="0V", color=0xFF0000) +text.anchor_point = (0.5, 0.5) +text.anchored_position = (display.width / 2, display.height / 2) +group.append(text) +display.root_group = group + +analog_in = AnalogIn(board.A3) + +def get_vsys(pin): + return ((pin.value * 3) * 3.3) / 65535 + +pixel_pin = board.A2 +num_pixels = 1 +pixel = neopixel.NeoPixel(pixel_pin, num_pixels, + brightness=0.1, auto_write=True) +hue = 0 +pixel.fill(colorwheel(hue)) + +bat_clock = ticks_ms() +bat_timer = 5000 +neo_clock = ticks_ms() +neo_timer = 100 + +while True: + if ticks_diff(ticks_ms(), bat_clock) >= bat_timer: + print(f"The battery level is: {get_vsys(analog_in):.1f}V") + text.text = f"{get_vsys(analog_in):.1f}V" + text.color = colorwheel(hue) + bat_clock = ticks_add(bat_clock, bat_timer) + if ticks_diff(ticks_ms(), neo_clock) >= neo_timer: + hue = (hue + 7) % 256 + pixel.fill(colorwheel(hue)) + neo_clock = ticks_add(neo_clock, neo_timer)