Skip to content

Instantly share code, notes, and snippets.

@Lunik
Last active June 2, 2018 20:17
Show Gist options
  • Save Lunik/cd8e2b55fab01618db7f8c5ce67892bb to your computer and use it in GitHub Desktop.
Save Lunik/cd8e2b55fab01618db7f8c5ce67892bb to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from sense_hat import SenseHat
import datetime
from time import sleep
import requests
import signal
import os
sense = SenseHat()
# Clear HAT on CTRL+C
def signal_handler(signal, frame):
sense.clear()
os._exit(0)
signal.signal(signal.SIGINT, signal_handler)
# Draw pixel array
def draw(pixel_array):
sense.set_pixels(pixel_array)
# Gather crypto values from API
def get_crypto_values():
url = "https://api.coingecko.com/api/v3/coins"
params = dict(
per_page = '64',
page = '1',
order = 'market_cap_desc'
)
resp = requests.get(url=url, params=params)
return resp.json()
# Parse json into array of int (percentage)
def parse_crypto_json (json):
cryptos = []
for crypto in json:
cryptos.append(int(float(crypto['market_data']['price_change_percentage_24h'])))
print("Average change is {}%".format(reduce(lambda x, y: x + y, cryptos) / len(cryptos)))
return cryptos
# Transalate a value from a range to another
def change_range (OldMin, OldMax, NewMin, NewMax, OldValue):
OldRange = (OldMax - OldMin)
NewRange = (NewMax - NewMin)
NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin
return NewValue
# Translate a percentage to a color
def get_color_from_percent (percent):
MAX = 10
MIN = -10
if percent > MAX:
percent = MAX
if percent < MIN:
percent = MIN
n = change_range(MIN, MAX, 0, 100, percent)
R = 255 - (255 * n / 100)
G = 255 * n / 100
B = 0
return (R, G, B)
# Translate array of int into array of color
def percent_to_color (percent_array):
array = []
for percent in percent_array:
array.append(get_color_from_percent(percent))
return array
# Prevent my eye from burning
# Turn off between min and max
def check_time():
now = datetime.datetime.now()
min = now.replace(hour=7, minute=0, second=0)
max = now.replace(hour=23, minute=59, second=59, microsecond=0)
return (now > min) and (now < max)
def main ():
if (not check_time()):
sense.clear()
return
json = get_crypto_values()
crypto_changes = parse_crypto_json(json)
colors = percent_to_color(crypto_changes)
draw(colors)
if __name__ == "__main__":
while True:
main()
sleep(60) # Update every 1 min
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment