Skip to content

Commit 1175e05

Browse files
committed
update tides to latest api
1 parent fc2b251 commit 1175e05

File tree

2 files changed

+89
-74
lines changed

2 files changed

+89
-74
lines changed

PyPortal_Tides/pp_tides.py

+40-31
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,70 @@
44
from adafruit_bitmap_font import bitmap_font
55
from adafruit_display_text.label import Label
66

7-
#--| USER CONFIG |--------------------------
8-
STATION_ID = "9447130" # tide location, find yours here: https://tidesandcurrents.noaa.gov/
9-
HI_COLOR = 0x00FF00 # high tide times color
10-
LO_COLOR = 0x11FFFF # low tide times color
11-
DATE_COLOR = 0xFFFFFF # date and time color
12-
#-------------------------------------------
7+
# --| USER CONFIG |--------------------------
8+
STATION_ID = (
9+
"9447130" # tide location, find yours here: https://tidesandcurrents.noaa.gov/
10+
)
11+
HI_COLOR = 0x00FF00 # high tide times color
12+
LO_COLOR = 0x11FFFF # low tide times color
13+
DATE_COLOR = 0xFFFFFF # date and time color
14+
# -------------------------------------------
1315

1416
# pylint: disable=line-too-long
15-
DATA_SOURCE = "https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?date=today&product=predictions&datum=mllw&interval=hilo&format=json&units=metric&time_zone=lst_ldt&station="+STATION_ID
17+
DATA_SOURCE = (
18+
"https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?date=today&product=predictions&datum=mllw&interval=hilo&format=json&units=metric&time_zone=lst_ldt&station="
19+
+ STATION_ID
20+
)
1621
DATA_LOCATION = ["predictions"]
1722

18-
# determine the current working directory needed so we know where to find files
19-
cwd = ("/"+__file__).rsplit('/', 1)[0]
20-
pyportal = PyPortal(url=DATA_SOURCE,
21-
json_path=DATA_LOCATION,
22-
status_neopixel=board.NEOPIXEL,
23-
default_bg=cwd+"/tides_bg.bmp")
23+
# gotta have one of these
24+
pyportal = PyPortal(status_neopixel=board.NEOPIXEL, default_bg="/tides_bg.bmp")
2425

2526
# Connect to the internet and get local time
2627
pyportal.get_local_time()
2728

2829
# Setup tide times font
29-
tide_font = bitmap_font.load_font(cwd+"/fonts/cq-mono-30.bdf")
30-
tide_font.load_glyphs(b'1234567890:')
30+
tide_font = bitmap_font.load_font("/fonts/cq-mono-30.bdf")
31+
tide_font.load_glyphs(b"1234567890:")
3132

3233
# Setup date and time font
33-
date_font = bitmap_font.load_font(cwd+"/fonts/Arial-12.bdf")
34-
date_font.load_glyphs(b'1234567890-')
34+
date_font = bitmap_font.load_font("/fonts/Arial-12.bdf")
35+
date_font.load_glyphs(b"1234567890-")
3536

3637
# Labels setup
37-
HI_LABELS = [ Label(tide_font, text="00:00", color=HI_COLOR, x= 40, y= 80) ,
38-
Label(tide_font, text="00:00", color=HI_COLOR, x= 40, y=165) ]
39-
LO_LABELS = [ Label(tide_font, text="00:00", color=LO_COLOR, x=180, y= 80) ,
40-
Label(tide_font, text="00:00", color=LO_COLOR, x=180, y=165) ]
38+
HI_LABELS = [
39+
Label(tide_font, text="00:00", color=HI_COLOR, x=40, y=80),
40+
Label(tide_font, text="00:00", color=HI_COLOR, x=40, y=165),
41+
]
42+
LO_LABELS = [
43+
Label(tide_font, text="00:00", color=LO_COLOR, x=180, y=80),
44+
Label(tide_font, text="00:00", color=LO_COLOR, x=180, y=165),
45+
]
4146
DATE_LABEL = Label(date_font, text="0000-00-00 00:00:00", color=DATE_COLOR, x=75, y=228)
4247

4348
# Add all the labels to the display
4449
for label in HI_LABELS + LO_LABELS + [DATE_LABEL]:
45-
pyportal.splash.append(label)
50+
pyportal.graphics.splash.append(label)
51+
4652

4753
def get_tide_info():
4854
"""Fetch JSON tide time info and return it."""
4955

5056
# Get raw JSON data
51-
raw_info = pyportal.fetch()
57+
raw_info = pyportal.network.fetch_data(DATA_SOURCE, json_path=DATA_LOCATION)
5258

5359
# Return will be a dictionary of lists containing tide times
54-
new_tide_info = {"H":[], "L":[]}
60+
new_tide_info = {"H": [], "L": []}
5561

5662
# Parse out the tide time info
57-
for info in raw_info:
63+
for info in raw_info[0]:
5864
tide_type = info["type"]
5965
tide_time = info["t"].split(" ")[1]
6066
new_tide_info[tide_type].append(tide_time)
6167

6268
return new_tide_info
6369

70+
6471
def update_display(time_info, update_tides=False):
6572
"""Update the display with current info."""
6673

@@ -76,12 +83,14 @@ def update_display(time_info, update_tides=False):
7683
LO_LABELS[i].text = lo_time
7784

7885
# Date and time
79-
DATE_LABEL.text = "{:04}-{:02}-{:02} {:02}:{:02}:{:02}".format(time_info.tm_year,
80-
time_info.tm_mon,
81-
time_info.tm_mday,
82-
time_info.tm_hour,
83-
time_info.tm_min,
84-
time_info.tm_sec)
86+
DATE_LABEL.text = "{:04}-{:02}-{:02} {:02}:{:02}:{:02}".format(
87+
time_info.tm_year,
88+
time_info.tm_mon,
89+
time_info.tm_mday,
90+
time_info.tm_hour,
91+
time_info.tm_min,
92+
time_info.tm_sec,
93+
)
8594

8695

8796
# First run update

PyPortal_Tides/pp_tides_graphical.py

+49-43
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,31 @@
55
from adafruit_bitmap_font import bitmap_font
66
from adafruit_display_text.label import Label
77

8-
#--| USER CONFIG |--------------------------
9-
STATION_ID = "9447130" # tide location, find yours here: https://tidesandcurrents.noaa.gov/
10-
PLOT_SIZE = 2 # tide plot thickness
11-
PLOT_COLOR = 0x00FF55 # tide plot color
12-
MARK_SIZE = 6 # current time marker size
13-
MARK_COLOR = 0xFF0000 # current time marker color
14-
DATE_COLOR = 0xE0CD1A # date text color
15-
TIME_COLOR = 0xE0CD1A # time text color
16-
VSCALE = 20 # vertical plot scale
17-
#-------------------------------------------
8+
# --| USER CONFIG |--------------------------
9+
STATION_ID = (
10+
"9447130" # tide location, find yours here: https://tidesandcurrents.noaa.gov/
11+
)
12+
PLOT_SIZE = 2 # tide plot thickness
13+
PLOT_COLOR = 0x00FF55 # tide plot color
14+
MARK_SIZE = 6 # current time marker size
15+
MARK_COLOR = 0xFF0000 # current time marker color
16+
DATE_COLOR = 0xE0CD1A # date text color
17+
TIME_COLOR = 0xE0CD1A # time text color
18+
VSCALE = 20 # vertical plot scale
19+
# -------------------------------------------
1820

1921
# pylint: disable=line-too-long
20-
DATA_SOURCE = "https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?date=today&product=predictions&datum=mllw&format=json&units=metric&time_zone=lst_ldt&station="+STATION_ID
22+
DATA_SOURCE = (
23+
"https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?date=today&product=predictions&datum=mllw&format=json&units=metric&time_zone=lst_ldt&station="
24+
+ STATION_ID
25+
)
2126
DATA_LOCATION = ["predictions"]
2227

2328
WIDTH = board.DISPLAY.width
2429
HEIGHT = board.DISPLAY.height
2530

26-
# determine the current working directory needed so we know where to find files
27-
cwd = ("/"+__file__).rsplit('/', 1)[0]
28-
pyportal = PyPortal(url=DATA_SOURCE,
29-
json_path=DATA_LOCATION,
30-
status_neopixel=board.NEOPIXEL,
31-
default_bg=cwd+"/tides_bg_graph.bmp")
31+
# gotta have one of these
32+
pyportal = PyPortal(status_neopixel=board.NEOPIXEL, default_bg="/tides_bg_graph.bmp")
3233

3334
# Connect to the internet and get local time
3435
pyportal.get_local_time()
@@ -42,73 +43,78 @@
4243

4344
# Setup tide plot bitmap
4445
tide_plot = displayio.Bitmap(WIDTH, HEIGHT, 3)
45-
pyportal.splash.append(displayio.TileGrid(tide_plot, pixel_shader=palette))
46+
pyportal.graphics.splash.append(displayio.TileGrid(tide_plot, pixel_shader=palette))
4647

4748
# Setup font used for date and time
48-
date_font = bitmap_font.load_font(cwd+"/fonts/mono-bold-8.bdf")
49-
date_font.load_glyphs(b'1234567890-')
49+
date_font = bitmap_font.load_font("/fonts/mono-bold-8.bdf")
50+
date_font.load_glyphs(b"1234567890-")
5051

5152
# Setup date label
5253
date_label = Label(date_font, text="0000-00-00", color=DATE_COLOR, x=7, y=14)
53-
pyportal.splash.append(date_label)
54+
pyportal.graphics.splash.append(date_label)
5455

5556
# Setup time label
5657
time_label = Label(date_font, text="00:00:00", color=TIME_COLOR, x=234, y=14)
57-
pyportal.splash.append(time_label)
58+
pyportal.graphics.splash.append(time_label)
5859

5960
# Setup current time marker
6061
time_marker_bitmap = displayio.Bitmap(MARK_SIZE, MARK_SIZE, 3)
61-
for pixel in range(MARK_SIZE * MARK_SIZE):
62-
time_marker_bitmap[pixel] = 2
63-
time_marker = displayio.TileGrid(time_marker_bitmap, pixel_shader=palette, x=-MARK_SIZE, y=-MARK_SIZE)
64-
pyportal.splash.append(time_marker)
62+
time_marker_bitmap.fill(2)
63+
time_marker = displayio.TileGrid(
64+
time_marker_bitmap, pixel_shader=palette, x=-MARK_SIZE, y=-MARK_SIZE
65+
)
66+
pyportal.graphics.splash.append(time_marker)
67+
6568

6669
def get_tide_data():
6770
"""Fetch JSON tide data and return parsed results in a list."""
6871

6972
# Get raw JSON data
70-
raw_data = pyportal.fetch()
73+
raw_data = pyportal.network.fetch_data(DATA_SOURCE, json_path=DATA_LOCATION)
7174

7275
# Results will be stored in a list that is display WIDTH long
73-
new_tide_data = [None]*WIDTH
76+
new_tide_data = [None] * WIDTH
7477

7578
# Convert raw data to display coordinates
76-
for data in raw_data:
77-
_, t = data["t"].split(" ") # date and time
78-
h, m = t.split(":") # hours and minutes
79-
v = data["v"] # water level
80-
x = round( (WIDTH - 1) * (60 * float(h) + float(m)) / 1440 )
79+
for data in raw_data[0]:
80+
_, t = data["t"].split(" ") # date and time
81+
h, m = t.split(":") # hours and minutes
82+
v = data["v"] # water level
83+
x = round((WIDTH - 1) * (60 * float(h) + float(m)) / 1440)
8184
y = (HEIGHT // 2) - round(VSCALE * float(v))
8285
y = 0 if y < 0 else y
83-
y = HEIGHT-1 if y >= HEIGHT else y
86+
y = HEIGHT - 1 if y >= HEIGHT else y
8487
new_tide_data[x] = y
8588

8689
return new_tide_data
8790

91+
8892
def draw_data_point(x, y, size=PLOT_SIZE, color=1):
8993
"""Draw data point on to the tide plot bitmap at (x,y)."""
9094
if y is None:
9195
return
9296
offset = size // 2
93-
for xx in range(x-offset, x+offset+1):
94-
for yy in range(y-offset, y+offset+1):
97+
for xx in range(x - offset, x + offset + 1):
98+
for yy in range(y - offset, y + offset + 1):
9599
try:
96100
tide_plot[xx, yy] = color
97101
except IndexError:
98102
pass
99103

104+
100105
def draw_time_marker(time_info):
101106
"""Draw a marker on the tide plot for the current time."""
102107
h = time_info.tm_hour
103108
m = time_info.tm_min
104-
x = round( (WIDTH - 1) * (60 * float(h) + float(m)) / 1440 )
109+
x = round((WIDTH - 1) * (60 * float(h) + float(m)) / 1440)
105110
y = tide_data[x]
106111
if y is not None:
107112
x -= MARK_SIZE // 2
108113
y -= MARK_SIZE // 2
109114
time_marker.x = x
110115
time_marker.y = y
111116

117+
112118
def update_display(time_info, update_tides=False):
113119
"""Update the display with current info."""
114120

@@ -125,12 +131,12 @@ def update_display(time_info, update_tides=False):
125131
draw_time_marker(time_info)
126132

127133
# Date and time
128-
date_label.text = "{:04}-{:02}-{:02}".format(time_info.tm_year,
129-
time_info.tm_mon,
130-
time_info.tm_mday)
131-
time_label.text = "{:02}:{:02}:{:02}".format(time_info.tm_hour,
132-
time_info.tm_min,
133-
time_info.tm_sec)
134+
date_label.text = "{:04}-{:02}-{:02}".format(
135+
time_info.tm_year, time_info.tm_mon, time_info.tm_mday
136+
)
137+
time_label.text = "{:02}:{:02}:{:02}".format(
138+
time_info.tm_hour, time_info.tm_min, time_info.tm_sec
139+
)
134140

135141

136142
# First run update

0 commit comments

Comments
 (0)