|
| 1 | +import math |
| 2 | +import time |
| 3 | +from pimoroni_i2c import PimoroniI2C |
| 4 | +from breakout_as7262 import BreakoutAS7262 |
| 5 | +from picographics import PicoGraphics, DISPLAY_PICO_W_EXPLORER, PEN_RGB332 |
| 6 | +from picovector import PicoVector, Polygon, RegularPolygon, ANTIALIAS_X4 |
| 7 | + |
| 8 | +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} |
| 9 | +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} |
| 10 | +i2c = PimoroniI2C(**PINS_PICO_EXPLORER) |
| 11 | + |
| 12 | +# Set up the AS7262 Spectrometer |
| 13 | +as7262 = BreakoutAS7262(i2c) |
| 14 | +as7262.set_gain(BreakoutAS7262.X16) |
| 15 | +as7262.set_measurement_mode(BreakoutAS7262.CONT_ROYGBR) |
| 16 | +as7262.set_illumination_current(BreakoutAS7262.MA12) |
| 17 | +as7262.set_indicator_current(BreakoutAS7262.MA4) |
| 18 | +as7262.set_leds(True, True) |
| 19 | + |
| 20 | +# Set up the display |
| 21 | +display = PicoGraphics(DISPLAY_PICO_W_EXPLORER, pen_type=PEN_RGB332) |
| 22 | +display.set_backlight(0.8) |
| 23 | + |
| 24 | +# Set up PicoVector |
| 25 | +vector = PicoVector(display) |
| 26 | +vector.set_antialiasing(ANTIALIAS_X4) |
| 27 | + |
| 28 | +# Load an Alright Font |
| 29 | +result = vector.set_font("/AdvRe.af", 30) |
| 30 | + |
| 31 | +WIDTH, HEIGHT = display.get_bounds() |
| 32 | + |
| 33 | +CENTER_X = int(WIDTH / 2) |
| 34 | +CENTER_Y = int(HEIGHT / 2) |
| 35 | + |
| 36 | +RADIUS = 90 |
| 37 | +DEBUG = False |
| 38 | + |
| 39 | +RED = display.create_pen(255, 0, 0) |
| 40 | +ORANGE = display.create_pen(255, 128, 0) |
| 41 | +YELLOW = display.create_pen(255, 255, 0) |
| 42 | +GREEN = display.create_pen(0, 255, 0) |
| 43 | +BLUE = display.create_pen(0, 0, 255) |
| 44 | +VIOLET = display.create_pen(255, 0, 255) |
| 45 | + |
| 46 | +BLACK = display.create_pen(0, 0, 0) |
| 47 | +GREY = display.create_pen(128, 128, 128) |
| 48 | +WHITE = display.create_pen(255, 255, 255) |
| 49 | + |
| 50 | +LABELS = ["R", "O", "Y", "G", "B", "V"] |
| 51 | +COLS = [RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET] |
| 52 | + |
| 53 | + |
| 54 | +# Custom regular_polygon function to give each point its own "radius" |
| 55 | +def regular_polygon(o_x, o_y, radius, rotation): |
| 56 | + sides = 6 |
| 57 | + angle = math.radians(360 / sides) |
| 58 | + rotation = math.radians(rotation) |
| 59 | + |
| 60 | + points = [] |
| 61 | + |
| 62 | + for side in range(sides): |
| 63 | + current_angle = side * angle + rotation |
| 64 | + x = math.cos(current_angle) * radius[side] |
| 65 | + y = math.sin(current_angle) * radius[side] |
| 66 | + points.append((int(x) + o_x, int(y) + o_y)) |
| 67 | + |
| 68 | + return points |
| 69 | + |
| 70 | + |
| 71 | +lines = RegularPolygon(CENTER_X, CENTER_Y, 6, RADIUS) |
| 72 | +label_points = list(RegularPolygon(CENTER_X, CENTER_Y, 6, RADIUS * 0.7, -(360 / 12))) |
| 73 | + |
| 74 | + |
| 75 | +while True: |
| 76 | + # Clear to black |
| 77 | + display.set_pen(BLACK) |
| 78 | + display.clear() |
| 79 | + |
| 80 | + # Add the title |
| 81 | + display.set_pen(WHITE) |
| 82 | + vector.text("Spectrograph", 5, -5) |
| 83 | + |
| 84 | + # Get the spectrometer readings |
| 85 | + reading = list(as7262.read()) |
| 86 | + |
| 87 | + # Print out the readings |
| 88 | + if DEBUG: |
| 89 | + for i in range(6): |
| 90 | + print(f"{LABELS[i]}: {reading[i]:0.2f}", end=" ") |
| 91 | + print("") |
| 92 | + |
| 93 | + # Draw the lines separating each section |
| 94 | + display.set_pen(GREY) |
| 95 | + for (x, y) in lines: |
| 96 | + display.line(CENTER_X, CENTER_Y, int(x), int(y)) |
| 97 | + |
| 98 | + # Scale readings for display |
| 99 | + for i in range(6): |
| 100 | + reading[i] = int(reading[i] / 3.0) |
| 101 | + reading[i] = min(reading[i], RADIUS) |
| 102 | + |
| 103 | + # Create a 6 point polygon with each points distance from the center |
| 104 | + # scaled by the corresponding reading. |
| 105 | + points = regular_polygon(CENTER_X, CENTER_Y, reading, 0) |
| 106 | + |
| 107 | + # Split the polygon into six triangles, one for each channel |
| 108 | + # draw each one, along with its corresponding label |
| 109 | + point_a = points[-1] |
| 110 | + for i in range(6): |
| 111 | + point_b = points[i] |
| 112 | + label_x, label_y = label_points[i] |
| 113 | + display.set_pen(COLS[i]) |
| 114 | + vector.text(LABELS[i], int(label_x) - 5, int(label_y) - 20) |
| 115 | + vector.draw(Polygon(point_a, point_b, (CENTER_X, CENTER_Y))) |
| 116 | + point_a = point_b |
| 117 | + |
| 118 | + display.update() |
| 119 | + time.sleep(1.0 / 60) |
0 commit comments