Skip to content

Commit 163a3a9

Browse files
committed
picowbell tripler demos
CircuitPython and Arduino demos for the PiCowbell Tripler
1 parent 0a1fb9e commit 163a3a9

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_NeoPixel.h>
6+
#include <Adafruit_GFX.h> // Core graphics library
7+
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
8+
#include <SPI.h>
9+
#include <Fonts/FreeSansBold24pt7b.h>
10+
11+
#define LED LED_BUILTIN
12+
#define TFT_CS 21
13+
#define TFT_RST -1
14+
#define TFT_DC 20
15+
#define NEO_PIN A2
16+
17+
uint32_t Wheel(byte WheelPos);
18+
19+
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
20+
21+
Adafruit_NeoPixel pixel = Adafruit_NeoPixel(1, NEO_PIN, NEO_GRB + NEO_KHZ800);
22+
23+
unsigned long lastMillis = 0;
24+
uint8_t j = 0;
25+
26+
void setup() {
27+
Serial.begin(115200);
28+
//while (!Serial) delay(1); // wait for serial port
29+
tft.init(240, 240);
30+
pinMode(LED, OUTPUT);
31+
delay (100);
32+
Serial.println("PiCowbell Tripler Demo");
33+
tft.setFont(&FreeSansBold24pt7b);
34+
pixel.begin();
35+
pixel.setBrightness(50);
36+
pixel.show(); // Initialize all pixels to 'off'
37+
lastMillis = millis();
38+
39+
}
40+
41+
void loop() {
42+
if (millis() > (lastMillis + 5000)) {
43+
digitalWrite(LED, HIGH);
44+
// get the on-board voltage
45+
float vsys = analogRead(A3) * 3 * 3.3 / 1023.0;
46+
Serial.printf("Vsys: %0.1f V", vsys);
47+
Serial.println();
48+
digitalWrite(LED, LOW);
49+
tft.fillScreen(ST77XX_BLACK);
50+
tft.setCursor(240 / 4, 240 / 2);
51+
tft.setTextColor(ST77XX_WHITE);
52+
tft.printf("%0.1f V", vsys);
53+
lastMillis = millis();
54+
}
55+
pixel.setPixelColor(0, Wheel(j++));
56+
pixel.show();
57+
delay(20);
58+
}
59+
60+
uint32_t Wheel(byte WheelPos) {
61+
WheelPos = 255 - WheelPos;
62+
if(WheelPos < 85) {
63+
return pixel.Color(255 - WheelPos * 3, 0, WheelPos * 3);
64+
}
65+
if(WheelPos < 170) {
66+
WheelPos -= 85;
67+
return pixel.Color(0, WheelPos * 3, 255 - WheelPos * 3);
68+
}
69+
WheelPos -= 170;
70+
return pixel.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
71+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import busio
6+
import board
7+
from analogio import AnalogIn
8+
import adafruit_st7789
9+
import displayio
10+
import neopixel
11+
from rainbowio import colorwheel
12+
from adafruit_display_text import label
13+
from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
14+
from font_orbitron_bold_webfont_36 import FONT as orbitron_font
15+
16+
displayio.release_displays()
17+
spi = busio.SPI(clock=board.GP18, MOSI=board.GP19)
18+
display_bus = displayio.FourWire(spi, command=board.GP20, chip_select=board.GP21, reset=None)
19+
display = adafruit_st7789.ST7789(display_bus, width=240, height=240, rowstart=80, rotation=0)
20+
21+
group = displayio.Group()
22+
text = label.Label(orbitron_font, text="0V", color=0xFF0000)
23+
text.anchor_point = (0.5, 0.5)
24+
text.anchored_position = (display.width / 2, display.height / 2)
25+
group.append(text)
26+
display.root_group = group
27+
28+
analog_in = AnalogIn(board.A3)
29+
30+
def get_vsys(pin):
31+
return ((pin.value * 3) * 3.3) / 65535
32+
33+
pixel_pin = board.A2
34+
num_pixels = 1
35+
pixel = neopixel.NeoPixel(pixel_pin, num_pixels,
36+
brightness=0.1, auto_write=True)
37+
hue = 0
38+
pixel.fill(colorwheel(hue))
39+
40+
bat_clock = ticks_ms()
41+
bat_timer = 5000
42+
neo_clock = ticks_ms()
43+
neo_timer = 100
44+
45+
while True:
46+
if ticks_diff(ticks_ms(), bat_clock) >= bat_timer:
47+
print(f"The battery level is: {get_vsys(analog_in):.1f}V")
48+
text.text = f"{get_vsys(analog_in):.1f}V"
49+
text.color = colorwheel(hue)
50+
bat_clock = ticks_add(bat_clock, bat_timer)
51+
if ticks_diff(ticks_ms(), neo_clock) >= neo_timer:
52+
hue = (hue + 7) % 256
53+
pixel.fill(colorwheel(hue))
54+
neo_clock = ticks_add(neo_clock, neo_timer)

0 commit comments

Comments
 (0)