5
5
from adafruit_bitmap_font import bitmap_font
6
6
from adafruit_display_text .label import Label
7
7
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
+ # -------------------------------------------
18
20
19
21
# 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
+ )
21
26
DATA_LOCATION = ["predictions" ]
22
27
23
28
WIDTH = board .DISPLAY .width
24
29
HEIGHT = board .DISPLAY .height
25
30
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" )
32
33
33
34
# Connect to the internet and get local time
34
35
pyportal .get_local_time ()
42
43
43
44
# Setup tide plot bitmap
44
45
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 ))
46
47
47
48
# 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-" )
50
51
51
52
# Setup date label
52
53
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 )
54
55
55
56
# Setup time label
56
57
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 )
58
59
59
60
# Setup current time marker
60
61
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
+
65
68
66
69
def get_tide_data ():
67
70
"""Fetch JSON tide data and return parsed results in a list."""
68
71
69
72
# Get raw JSON data
70
- raw_data = pyportal .fetch ( )
73
+ raw_data = pyportal .network . fetch_data ( DATA_SOURCE , json_path = DATA_LOCATION )
71
74
72
75
# Results will be stored in a list that is display WIDTH long
73
- new_tide_data = [None ]* WIDTH
76
+ new_tide_data = [None ] * WIDTH
74
77
75
78
# 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 )
81
84
y = (HEIGHT // 2 ) - round (VSCALE * float (v ))
82
85
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
84
87
new_tide_data [x ] = y
85
88
86
89
return new_tide_data
87
90
91
+
88
92
def draw_data_point (x , y , size = PLOT_SIZE , color = 1 ):
89
93
"""Draw data point on to the tide plot bitmap at (x,y)."""
90
94
if y is None :
91
95
return
92
96
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 ):
95
99
try :
96
100
tide_plot [xx , yy ] = color
97
101
except IndexError :
98
102
pass
99
103
104
+
100
105
def draw_time_marker (time_info ):
101
106
"""Draw a marker on the tide plot for the current time."""
102
107
h = time_info .tm_hour
103
108
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 )
105
110
y = tide_data [x ]
106
111
if y is not None :
107
112
x -= MARK_SIZE // 2
108
113
y -= MARK_SIZE // 2
109
114
time_marker .x = x
110
115
time_marker .y = y
111
116
117
+
112
118
def update_display (time_info , update_tides = False ):
113
119
"""Update the display with current info."""
114
120
@@ -125,12 +131,12 @@ def update_display(time_info, update_tides=False):
125
131
draw_time_marker (time_info )
126
132
127
133
# 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
+ )
134
140
135
141
136
142
# First run update
0 commit comments