Skip to content

Commit 691966e

Browse files
committed
upload stock
1 parent b0cb505 commit 691966e

File tree

2 files changed

+190
-0
lines changed

2 files changed

+190
-0
lines changed

software/images/stock_96px.png

18.1 KB
Loading

software/stock.py

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
'''
2+
3+
v0.1.0
4+
20230913
5+
first release
6+
7+
'''
8+
9+
from magiclick import *
10+
11+
import os
12+
import ssl
13+
import time
14+
import wifi
15+
import board
16+
import displayio
17+
import terminalio
18+
import socketpool
19+
import adafruit_requests
20+
# import adafruit_imageload
21+
from adafruit_display_text import bitmap_label
22+
import json
23+
24+
25+
# Define time interval between requests
26+
time_interval = 300 # set the time interval to 5 minutes
27+
stock_code = 'sh688018' #espressif
28+
29+
30+
31+
32+
33+
34+
35+
36+
display.show(None)
37+
TERMINAL_HEIGHT=display.height+20
38+
display.root_group.scale = 1
39+
40+
display.root_group[0].hidden = False
41+
display.root_group[1].hidden = True # logo
42+
display.root_group[2].hidden = True # status bar
43+
supervisor.reset_terminal(display.width,TERMINAL_HEIGHT)
44+
display.root_group[0].y = 0
45+
46+
47+
display.show(display.root_group)
48+
49+
50+
print(' Stock ')
51+
print(' ')
52+
print(' ')
53+
print(' ')
54+
55+
56+
print(f"Connecting to \r\n[ {os.getenv('WIFI_SSID')} ]")
57+
# Initialize Wi-Fi connection
58+
try:
59+
wifi.radio.connect(
60+
os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD")
61+
)
62+
print("Connected to %s!" % os.getenv("WIFI_SSID"))
63+
# Wi-Fi connectivity fails with error messages, not specific errors, so this except is broad.
64+
except Exception as e: # pylint: disable=broad-except
65+
print(
66+
"Failed to connect to WiFi. Error:", e, "\nBoard will hard reset in 30 seconds."
67+
)
68+
69+
70+
# Create a socket pool and session object for making HTTP requests
71+
pool = socketpool.SocketPool(wifi.radio)
72+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
73+
74+
# # Set location and units for weather data
75+
#
76+
# WEATHERKEY = os.getenv("WEATHER_KEY")
77+
# if WEATHERKEY =="":
78+
# WEATHERKEY = 'ctpe2272nswh94og'
79+
80+
81+
82+
83+
84+
85+
86+
87+
88+
89+
90+
group = displayio.Group()
91+
92+
# Create label for displaying temperature data
93+
stock_area = bitmap_label.Label(terminalio.FONT, scale=2)
94+
stock_area.anchor_point = (0.5, 0)
95+
stock_area.anchored_position = (display.width // 2, 0)
96+
stock_area.text=stock_code
97+
98+
99+
# Create label for displaying temperature data
100+
text_area = bitmap_label.Label(terminalio.FONT, scale=3)
101+
# text_area.x=10
102+
# text_area.y=50
103+
text_area.anchor_point = (0.5,0)
104+
text_area.anchored_position = (display.width // 2, 40)
105+
106+
text_area2 = bitmap_label.Label(terminalio.FONT, scale=2)
107+
# text_area2.x=10
108+
# text_area2.y=90
109+
text_area2.anchor_point = (0.5, 0)
110+
text_area2.anchored_position = (display.width // 2, 90)
111+
# Create main group to hold all display groups
112+
main_group = displayio.Group()
113+
main_group.append(group)
114+
main_group.append(stock_area)
115+
main_group.append(text_area)
116+
main_group.append(text_area2)
117+
# Show the main group on the display
118+
display.show(main_group)
119+
120+
121+
122+
def get_stock(stock):
123+
try:
124+
response = requests.get('http://hq.finance.ifeng.com/q.php?l='+stock )
125+
info = json.loads(response.text[11:-3])
126+
# print(info)
127+
128+
print(info[stock][0])#当前价
129+
print(info[stock][1])#开盘价
130+
print(info[stock][2])#涨跌量
131+
print(info[stock][3])#涨跌幅
132+
133+
return info[stock][0],info[stock][1],info[stock][2],info[stock][3]
134+
except:
135+
136+
return 0,0,0,0
137+
138+
139+
def disp_stock(price):
140+
if price[2]<0:
141+
text_area.color=0x00ff00
142+
text_area2.color=0x00ff00
143+
else:
144+
text_area.color=0xff0000
145+
text_area2.color=0xff0000
146+
147+
148+
text_area.text=str(price[0])
149+
text_area2.text= '{:.2f}%'.format(price[3])
150+
151+
price = get_stock(stock_code)
152+
disp_stock(price)
153+
154+
now = time.monotonic()
155+
old = now
156+
157+
while True:
158+
now = time.monotonic()
159+
if (now-old) >= time_interval:
160+
old = time.monotonic()
161+
price = get_stock(stock_code)
162+
disp_stock(price)
163+
gc.collect()
164+
165+
time.sleep(0.2)
166+
167+
key = getkey()
168+
if key>0:
169+
print(key)
170+
if key==0:
171+
172+
clearkey()
173+
elif key==2 or key==1:
174+
print('exit')
175+
returnMainPage()
176+
177+
178+
acceleration = imu.acceleration
179+
if acceleration[2] > 8.0:
180+
returnMainPage()
181+
182+
183+
184+
185+
186+
187+
188+
189+
190+

0 commit comments

Comments
 (0)