-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
174 lines (145 loc) · 3.98 KB
/
code.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import time
import terminalio
import neopixel
import alarm
import board
import json
import math
from adafruit_magtag.magtag import MagTag
from analogio import AnalogIn
from wakeful import store_data, load_data
#NeoPix Colors
RED = 0x880000
GREEN = 0x008800
BLUE = 0x000088
YELLOW = 0x884400
CYAN = 0x0088BB
MAGENTA = 0x9900BB
WHITE = 0x888888
SYNCHRONIZE_CLOCK = True
#Prepare Stock Api
STOCK1 = "WSO"
# Set up where we'll be fetching data from
# Get a free API/token from here https://finnhub.io/
DATA_SOURCE = (
"https://finnhub.io/api/v1/quote?symbol=" + STOCK1 + "&token="
)
#Parse the Json data
DATA1_LOCATION = ["c"] # Current price
DATA2_LOCATION = ["h"] # High price of the day
DATA3_LOCATION = ["l"] # Low price of the day
DATA4_LOCATION = ["pc"] # Prevoius Close
#Set the pin for Battery
analog_in = AnalogIn(board.A1)
#----------------Functions--------------------
def get_voltage(pin):
return (pin.value * 3.3) / 65536
def blink(color, duration):
magtag.peripherals.neopixel_disable = False
magtag.peripherals.neopixels.fill(color)
time.sleep(duration)
magtag.peripherals.neopixel_disable = True
def text_Current(val):
return "$%g " % val + STOCK1
def text_High(val):
return "$%g High" % val
def text_Low(val):
return "$%g Low" % val
def text_Close(val):
return "$%g PrvClose" % val
#Main
#Set Data
magtag = MagTag(
url=DATA_SOURCE,
json_path=(DATA1_LOCATION, DATA2_LOCATION, DATA3_LOCATION, DATA4_LOCATION),
)
#Get Time
magtag.network.connect()
magtag.network.get_local_time()
#Get Background
magtag.graphics.set_background("/bmps/magtag_quotes_bg.bmp")
#Get batery info
voltage = magtag.peripherals.battery
#Print to the eInk
# Current Stock quote in bold text
magtag.add_text(
text_transform=text_Current,
text_font="/fonts/LeagueGothic-Regular-36.pcf",
text_position=(
(magtag.graphics.display.width // 2) - 1,
31,
),
text_anchor_point=(0.5, 0.7),
)
# High Quote in italic text, no wrapping
magtag.add_text(
text_transform=text_High,
text_font="/fonts/Arial-Bold-12.pcf",
text_position=(
(magtag.graphics.display.width // 2) - 1,
60,
),
text_anchor_point=(0.5, 0.4),
)
# Low Quote in italic text, no wrapping
magtag.add_text(
text_transform=text_Low,
text_font="/fonts/Arial-Italic-12.pcf",
text_position=(
(magtag.graphics.display.width // 2) - 1,
80,
),
text_anchor_point=(0.5, 0.3),
)
# Prevoius Quote in italic text, no wrapping
magtag.add_text(
text_transform=text_Close,
text_font="/fonts/Arial-Bold-12.pcf",
text_position=(
(magtag.graphics.display.width // 2) - 1,
120,
),
text_anchor_point=(0.5, 0.75),
)
#Go Get the Info
print("Fetchiing Data from JSON")
try:
value = magtag.fetch()
print("Response is", value)
TS = time.localtime()
print(TS)
except (ValueError, RuntimeError) as e:
print("Some error occured, retrying! -", e)
magtag.refresh()
CurrentPrice = float(value[0])
print(CurrentPrice)
PrvPrice=load_data()
print(PrvPrice)
if CurrentPrice >= PrvPrice:
blink(GREEN, .8)
else:
blink(RED, .8)
store_data(CurrentPrice)
print(voltage)
if voltage < 1.9:
magtag.peripherals.play_tone(165, 1)
#Undecided on the sound I want to make when the batery is low
# (Freq,Length)
#magtag.peripherals.play_tone(9980, 0.15)
#magtag.peripherals.play_tone(880, 0.15)
#magtag.peripherals.play_tone(147, 1)
#magtag.peripherals.play_tone(175, 1)
#magtag.peripherals.play_tone(196, 1)
#magtag.peripherals.play_tone(131, 1)
CurrentTime = (TS[3])
print(CurrentTime)
print(TS)
# Create a an alarm that will trigger x seconds from now.
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() +30)
#Future Use even more deep sleep based on clock time for 6pm to 6am
#if CurrentTime > 6:
# alarm.exit_and_deep_sleep_until_alarms(time_alarm)
#else:
# magtag.exit_and_deep_sleep(900)
#Deep sleep for 290 seconds to save the battery
magtag.exit_and_deep_sleep(290)