Skip to content

Commit 6891fab

Browse files
committed
New wifi_finance vresion and SDIO SD card support
1 parent c39f126 commit 6891fab

File tree

11 files changed

+173
-42
lines changed

11 files changed

+173
-42
lines changed

PyDOS.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def PyDOS():
6767
global envVars
6868
if "envVars" not in globals().keys():
6969
envVars = {}
70-
_VER = "1.39"
70+
_VER = "1.40"
7171
prmpVals = ['>','(',')','&','|','\x1b','\b','<','=',' ',_VER,'\n','$','']
7272

7373
print("Starting Py-DOS...")

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ PyDOS.
159159

160160
**fileview.py [[path]filename]** - scrollable text file viewer
161161

162-
**sdmount.py [[mount path][,pydos spi bus#]]** - mounts an sd card to the file system
162+
**sdmount.py [[mount path][,pydos spi bus#]]** - mounts an sd card to the file system. If no SD card CS pins are defined or -1 is passed as the spi bus# the sd
163+
card will be mounted using an SDIO bus rather than an SPI bus.
163164
**sdumount.py [mount path]** - dismounts an sd card from the file system
164165

165166
**setdate.py [mm-dd-yy]** - initalizes the real time clock to an entered date
@@ -208,7 +209,7 @@ and then power cycled or hard reset.
208209
**setenv.py** - Helper program for adding the WiFi SSID and Password to settings.toml (used by setup.bat).
209210

210211
*WiFi enabled boards only*
211-
**wifi_finance** - Displays the current Nasdaq prices by connecting to a financial website and scraping the information.
212+
**wifi_finance [symbol]** - Displays the current Nasdaq prices by connecting to a financial website and scraping the information. If an optional symbol is supplied, the program will attempt to identify the symbol and scrap the corresponding price information.
212213
**wifi_weather** - Displays the 7 day forcast from api.weather.gov
213214

214215
## Hardware (Pin) customization file (pydos_bcfg.py)
@@ -235,6 +236,9 @@ example: `'led' : (25, "GP25")`
235236
**I2C_NUM** - MicroPython hardware I2C number
236237
**SCL**
237238
**SDA**
239+
**SDIO_CLK**
240+
**SDIO_CMD**
241+
**SDIO_DPINS** - the "pin number" element for SDIO_DPINS is a list of pins
238242

239243
**LIST OF TUPLES**
240244
*First tuple in list used for machine/board SD dedicated SPI (board.SD_SPI)*

cpython/boardconfigs/pydos_bcfg_makerfabs_tft7.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import board
44

55
Pydos_pins = {
6-
'SCL' : (board.SCL,"GPIO18 board.SCL"),
7-
'SDA' : (board.SDA,"GPIO17 board.SDA")
6+
'SCL' : (board.SCL,"GPIO18 board.SCL"),
7+
'SDA' : (board.SDA,"GPIO17 board.SDA"),
8+
'SDIO_CLK' : (board.SDIO_CLK,"GPIO12 board.SDIO_CLK"),
9+
'SDIO_CMD' : (board.SDIO_CMD,"GPIO11 board.SDIO_CMD"),
10+
'SDIO_DPINS' : ([board.SDIO_D0],"[GPIO13] [board.SDIO_D0]")
811
}

cpython/kbdFeatherWing/keys.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ echo -) >
1313
echo _# ^
1414
echo _/ \
1515
echo -/ %%
16+
echo _! &
1617
echo Speaker Key (left of enter) $
1718
echo Track ball up up arrow
1819
echo Track ball down down arrow

cpython/lib/optional/pydos_ui_lilygokbd.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ def input(disp_text=None):
112112
if disp_text != None:
113113
print(disp_text,end="")
114114

115-
bld_chr1 = '_(+(-__-'
116-
bld_chr2 = '-+)-)/#/'
117-
bld_chr = '=[]<>\^%'
115+
bld_chr1 = '_(+(-__-_'
116+
bld_chr2 = '-+)-)/#/!'
117+
bld_chr = '=[]<>\^%&'
118118
bld_started = False
119119

120120
histPntr = len(Pydos_ui.commandHistory)

lib/pydos_bcfg.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,10 @@
151151
else:
152152
Pydos_pins["CS"].append((None,"None"))
153153

154+
if 'SDIO_CLK' in dir(board) and 'SDIO_CMD' in dir(board):
155+
Pydos_pins['SDIO_CLK'] = (board.SDIO_CLK,"board.SDIO_CLK")
156+
Pydos_pins['SDIO_CMD'] = (board.SDIO_CMD,"board.SDIO_CMD")
157+
datapins = [getattr(board,attr) for attr in dir(board) if 'SDIO_D' in attr]
158+
if len(datapins) > 0:
159+
Pydos_pins['SDIO_DPINS'] = (datapins,"[board.SDIO_D*]")
160+

lib/pydos_hw.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ def __init__(self):
116116
self.SD.append(None)
117117
self._SPI.append(None)
118118

119+
self.SDIO_CLK=Pydos_pins.get('SDIO_CLK',(None,None))[0]
120+
self.SDIO_CMD=Pydos_pins.get('SDIO_CMD',(None,None))[0]
121+
self.SDIO_DPINS=Pydos_pins.get('SDIO_DPINS',([None],None))[0]
122+
if self.SDIO_CLK:
123+
self.SDdrive.append(None)
124+
self.SD.append(None)
125+
119126
if implementation.name.upper() == 'CIRCUITPYTHON':
120127
try:
121128
if i == 0:

lib/pydos_wifi.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PyDOS_wifi_VER = "1.37"
1+
PyDOS_wifi_VER = "1.40"
22

33
import os
44
import time
@@ -217,10 +217,16 @@ def get(self,text_url,headers=None,getJSON=False):
217217
def json(self):
218218
retVal = None
219219
if implementation.name.upper() == 'CIRCUITPYTHON':
220-
retVal = self.response.json()
220+
try:
221+
retVal = self.response.json()
222+
except ValueError:
223+
retVal = {'ValueError':'Possibly caused by syntax error in JSON'}
221224
elif implementation.name.upper() == 'MICROPYTHON':
222225
if 'json' in dir(self.response):
223-
retVal = self.response.json()
226+
try:
227+
retVal = self.response.json()
228+
except ValueError:
229+
retVal = {'ValueError':'Possibly caused by syntax error in JSON'}
224230
else:
225231
foundStart = False
226232
retVal = ''

pydospins.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def printPinAssignments():
5757
print("PyDOS Sound Pin: ",Pydos_pins['sndPin'][1],end="")
5858
elif entry in ['led','SCL','SDA','neoPixel','neoPixel_Pow','dotStar_Clock', \
5959
'dotStar_Data','dotStar_Extra','dotStar_Pow','LED_RED','LED_GREEN', \
60-
'LED_BLUE','i2s_BitClock','i2s_WordSelect','i2s_Data']:
60+
'LED_BLUE','i2s_BitClock','i2s_WordSelect','i2s_Data','SDIO_CLK', \
61+
'SDIO_CMD','SDIO_DPINS']:
6162

6263
print(entry+" Pin:"+(20-len(entry))*" ",Pydos_pins[entry][1],end="")
6364
elif entry in ['I2C_NUM']:

sdmount.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
import adafruit_sdcard
1818
except:
1919
import sdcardio as adafruit_sdcard
20+
try:
21+
import sdioio
22+
except:
23+
pass
2024
import storage
2125

22-
2326
def sdMount(drive,spiNo):
2427

2528
def chkPath(tstPath):
@@ -129,7 +132,25 @@ def do_mount(drive,spiNo):
129132

130133
elif implementation.name.upper() == "CIRCUITPYTHON":
131134

132-
if spiNo+1 > len(Pydos_hw.CS):
135+
if Pydos_hw.SDIO_CLK and (spiNo == -1 or len(Pydos_hw.CS) == 0):
136+
sdioNo = len(Pydos_hw.CS)
137+
if Pydos_hw.SDdrive[sdioNo] != None:
138+
print("SD card on SDIO interface already mounted as",Pydos_hw.SDdrive[sdioNo])
139+
else:
140+
try:
141+
if not Pydos_hw.SD[sdioNo]:
142+
Pydos_hw.SD[sdioNo] = sdioio.SDCard(
143+
clock=Pydos_hw.SDIO_CLK,
144+
command=Pydos_hw.SDIO_CMD,
145+
data=Pydos_hw.SDIO_DPINS,
146+
frequency=25000000)
147+
vfs = storage.VfsFat(Pydos_hw.SD[sdioNo])
148+
storage.mount(vfs, drive)
149+
Pydos_hw.SDdrive[sdioNo] = drive
150+
sdMounted = True
151+
except Exception as e:
152+
print('SD-Card: Fail,', e)
153+
elif spiNo+1 > len(Pydos_hw.CS):
133154
print("CS Pin not allocated for Pydos_bcfg SPI interface #",spiNo)
134155
elif Pydos_hw.SDdrive[spiNo] != None:
135156
print("SD card on Pydos_bcfg SPI interface #",spiNo,"already mounted as",Pydos_hw.SDdrive[spiNo])

wifi_finance.py

Lines changed: 109 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55
from os import uname
66
from pydos_wifi import Pydos_wifi
77

8-
def wifi_finance():
8+
def wifi_finance(symbol):
99
try:
1010
_scrWidth = int(envVars.get('_scrWidth',80))
1111
except:
1212
_scrWidth = 80
1313

14+
if not symbol:
15+
symbol = "IXIC"
16+
else:
17+
symbol = symbol.upper()
18+
prt_sym = symbol
19+
srch_sym = symbol
20+
1421
# Get wifi details and more from a .env file
1522
if Pydos_wifi.getenv('CIRCUITPY_WIFI_SSID') is None:
1623
raise Exception("WiFi secrets are kept in settings.toml, please add them there by using setenv.py!")
@@ -30,18 +37,17 @@ def wifi_finance():
3037
#TEXT_URL = "https://www.moneycontrol.com/us-markets"
3138
#search_string = '<!-- -->Nasdaq<!-- -->'
3239

33-
TEXT_URL = "https://www.google.com/search?q=%3Cspan%20class="x x-first x-last">nasdaq+index+price"
34-
search_string = 'Nasdaq Composite'
40+
TEXT_URL = f"https://www.google.com/search?q=%3Cspan%20class="pl-s1">{symbol.replace('&','%26')}+stock+price"
41+
search_string = symbol
3542

3643
#headers = {"user-agent": "RetiredWizard@"+implementation.name.lower()+uname()[2]}
3744

3845
print("Fetching text from %s" % TEXT_URL)
3946
response = Pydos_wifi.get(TEXT_URL)
4047
response_window = []
4148
for _ in range(4):
42-
tmp = Pydos_wifi.next(256)
43-
response_window.append(tmp)
44-
if len(tmp) != 256:
49+
response_window.append(Pydos_wifi.next(256))
50+
if len(response_window[-1]) != 256:
4551
break
4652

4753
try:
@@ -55,44 +61,119 @@ def wifi_finance():
5561
print(pline)
5662
print("-" * _scrWidth)
5763

58-
nasdaq = str(b''.join(response_window)).find(search_string)
64+
print("Identifying symbol",end="")
65+
name_loc = -1
5966
iKount = 0
60-
while nasdaq == -1 and iKount<800:
67+
while name_loc == -1 and iKount<800:
6168
iKount +=1
6269
if iKount % 10 == 0:
6370
print(".",end="")
64-
for i in range(3):
65-
response_window[i] = response_window[i+1]
66-
try:
67-
response_window[3] = Pydos_wifi.next(256)
68-
except:
69-
iKount=800
7071

71-
nasdaq = str(b''.join(response_window)).find(search_string)
72-
print("*")
72+
found_window = str(b''.join(response_window))
73+
74+
name_loc = found_window.find(' Inc. is')
75+
if name_loc == -1:
76+
name_loc = found_window.find(' Inc., commonly')
77+
if name_loc == -1:
78+
name_loc = found_window.find(' is a stock market ')
79+
if name_loc != -1:
80+
if found_window[:name_loc].rfind('or simply the ') != -1:
81+
srch_sym = found_window[found_window[:name_loc].rfind('or simply the ')+14:name_loc]
82+
elif found_window[:name_loc].rfind('>') != -1:
83+
srch_sym = found_window[found_window[:name_loc].rfind('>')+1:name_loc]
84+
srch_sym = srch_sym.replace(',','')
85+
prt_sym = srch_sym.replace('&amp;','&')
86+
prt_sym = prt_sym.replace('amp;','')
87+
if srch_sym[0:4].upper() == 'THE ':
88+
srch_sym = srch_sym[4:]
89+
print(f'* {search_string} * {srch_sym} * {prt_sym}',end="")
90+
91+
if iKount<800:
92+
for i in range(3):
93+
response_window[i] = response_window[i+1]
94+
try:
95+
response_window[3] = Pydos_wifi.next(256)
96+
if len(response_window[3]) != 256:
97+
print('X',end="")
98+
iKount=800
99+
except:
100+
iKount=800
101+
print()
102+
103+
print("Locating price data",end="")
104+
response.close()
105+
response = Pydos_wifi.get(TEXT_URL)
106+
response_window = []
107+
iKount = 0
108+
for _ in range(4):
109+
response_window.append(Pydos_wifi.next(256))
110+
if len(response_window[-1]) != 256:
111+
iKount = 799
112+
break
73113

74-
if iKount < 800:
75-
for _ in range(2):
76-
response_window.append(Pydos_wifi.next(256))
114+
nasdaq = -1
115+
while nasdaq == -1 and iKount<800:
116+
iKount +=1
117+
if iKount % 10 == 0:
118+
print(".",end="")
77119

120+
found_window = str(b''.join(response_window))
121+
nasdaq = found_window.upper().find(search_string)
122+
if nasdaq == -1:
123+
nasdaq = found_window.upper().find(srch_sym.upper())
124+
if nasdaq == -1:
125+
for i in range(3):
126+
response_window[i] = response_window[i+1]
127+
try:
128+
response_window[3] = Pydos_wifi.next(256)
129+
if len(response_window[3]) != 256:
130+
print('X',end="")
131+
iKount=800
132+
except:
133+
print('X',end="")
134+
iKount=800
135+
else:
136+
if iKount < 800:
137+
for _ in range(2):
138+
response_window.append(Pydos_wifi.next(256))
139+
if len(response_window[-1]) != 256:
140+
print('X',end="")
141+
iKount = 800
142+
break
143+
144+
found_window = str(b''.join(response_window))
145+
pct = found_window[nasdaq:].find('%)')
146+
if pct == -1 and iKount<800:
147+
response_window[0] = response_window[2]
148+
response_window[1] = response_window[3]
149+
response_window[3] = response_window.pop()
150+
response_window[2] = response_window.pop()
151+
nasdaq = -1
152+
153+
print("*\n")
78154
found_window = str(b''.join(response_window))
79-
nasdaq = found_window.find(search_string)
155+
nasdaq = found_window.upper().find(search_string)
156+
if nasdaq == -1:
157+
nasdaq = str(b''.join(response_window)).upper().find(srch_sym.upper())
80158

81-
pct = found_window[nasdaq:].find('%)')
82-
pctst = found_window[nasdaq+pct-17:].find('">')+2
83-
pctend = found_window[nasdaq+pct:].find('<')
84-
#print("Debug: %s\n" % found_window[nasdaq:nasdaq+pct+pctend])
159+
pct = found_window.find('%)')
160+
pctst = found_window[:pct].rfind('>')+1
161+
pctend = pct + found_window[pct:].find('<')
162+
#print("Debug: %s\n" % found_window[nasdaq:pctend])
163+
pricest = found_window[:pctst-2].rfind('>')+1
164+
priceend = pricest + found_window[pricest:].find('<')
85165

86166
if nasdaq != -1:
87-
print(f'Nasdaq: {found_window[nasdaq+pct-17+pctst:nasdaq+pct+pctend].replace("<","")}\n')
167+
print(f'{prt_sym}: {found_window[pricest:priceend]} {found_window[pctst:pctend].replace("<","")}\n')
88168
else:
89-
print("Nasdaq symbol not found\n")
169+
print(f"{prt_sym} symbol not found\n")
90170

91171
Pydos_wifi.close()
92172
del response_window
93173
del found_window
94174

95175
if __name__ == "PyDOS":
96-
wifi_finance()
176+
wifi_finance(passedIn)
97177
else:
98-
print("Enter 'wifi_finance.wifi_finance()' in the REPL or PEXEC command to run.")
178+
print('Enter "wifi_finance.wifi_finance("symbol")" in the REPL or PEXEC command to run.')
179+
print(' A null symbol ("") will default to the Nasdaq Index')

0 commit comments

Comments
 (0)