-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeedbacker.py
79 lines (59 loc) · 2.78 KB
/
feedbacker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import board
import neopixel
from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.animation.chase import Chase
from adafruit_led_animation.sequence import AnimationSequence
PIXEL_PIN = board.D21
NUM_PIXELS = 60
class Feedbacker:
def __init__(self):
self.pixels = neopixel.NeoPixel(PIXEL_PIN, NUM_PIXELS, auto_write=False, brightness=0.1)
self.power_readings = []
self.intro = Pulse(self.pixels, speed=0.5, color=(0, 102, 255))
self.realtime = Chase(self.pixels, speed=0.1, color=(0, 255, 0), size=3, spacing=6)
self.greenbank = Pulse(self.pixels, speed=0.5, color=(255, 0, 0))
self.outro = Pulse(self.pixels, speed=0.5, color=(0, 102, 255))
self.animations = AnimationSequence(self.intro, self.realtime, self.greenbank, self.outro)
def change(self, raw_data):
signal_power = get_signal_power(raw_data)
self.power_readings.append(signal_power)
avg = sum(self.power_readings) / len(self.power_readings)
print("AVERAGE POWER READING: ", avg)
mapped_to_color_range = map_power_value_to_hex(signal_power)
rgb_tuple = hex_to_rgb(mapped_to_color_range)
inverse_rgb_tuple = get_inverse_color(rgb_tuple)
print("rgb: ", rgb_tuple)
print("inverse rgb: ", inverse_rgb_tuple)
self.animations.color = rgb_tuple
# TODO: move this into radio_reader?
def get_signal_power(raw_data):
real_squared = raw_data.real ** 2
imag_squared = raw_data.imag ** 2
return real_squared + imag_squared
# https://www.30secondsofcode.org/python/s/num-to-range/
def map_num_to_range(num, inMin, inMax, outMin, outMax):
return outMin + (float(num - inMin) / float(inMax - inMin) * (outMax - outMin))
def map_power_value_to_hex(power_value):
# now that we're using power, i dont think that'll ever be less than zero
print("power: ", power_value)
mapped_num = map_num_to_range(power_value, 0, 1, 1000000, 16777215)
rounded_mapped_number = round(mapped_num)
# convert the mapped num decimal to a hex string, and then cut the x off the front
return hex(rounded_mapped_number).split('x')[-1]
#https://www.educative.io/answers/how-to-convert-hex-to-rgb-and-rgb-to-hex-in-python
def hex_to_rgb(hex):
rgb = []
for i in (0, 2, 4):
decimal = int(hex[i:i+2], 16)
rgb.append(decimal)
return tuple(rgb)
def get_inverse_color(rgb_value):
original_r = rgb_value[0]
original_g = rgb_value[1]
original_b = rgb_value[2]
new_r = (original_r * -1) + 255
new_g = (original_g * -1) + 255
new_b = (original_b * -1) + 255
return (new_r, new_g, new_b)
# neopixel library: https://docs.circuitpython.org/projects/neopixel/en/latest/api.html
# https://learn.adafruit.com/adafruit-neopixel-uberguide/python-circuitpython