Skip to content

Commit 209576a

Browse files
authored
Merge pull request SHA2017-badge#142 from SHA2017-badge/renze-easyapi
Split splash into smaller parts, thereby creating helper files for some functions
2 parents b0133c2 + 1735a97 commit 209576a

File tree

4 files changed

+220
-167
lines changed

4 files changed

+220
-167
lines changed

esp32/modules/easydraw.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# File: easydraw.py
2+
# Version: 1
3+
# Description: Wrapper that makes drawing things simple
4+
# License: MIT
5+
# Authors: Renze Nicolai <renze@rnplus.nl>
6+
7+
import ugfx, badge
8+
9+
# Globals
10+
msgLineNumber = 0
11+
msgShown = False
12+
13+
# Functions
14+
def msg(message, clear=False):
15+
global msgShown
16+
msgShown = True
17+
global msgLineNumber
18+
if clear:
19+
ugfx.clear(ugfx.WHITE)
20+
ugfx.string(0, 0, message, "PermanentMarker22", ugfx.BLACK)
21+
ugfx.set_lut(ugfx.LUT_FASTER)
22+
ugfx.flush()
23+
msgLineNumber = 0
24+
else:
25+
ugfx.string(0, 30 + (msgLineNumber * 15), message, "Roboto_Regular12", ugfx.BLACK)
26+
ugfx.flush()
27+
msgLineNumber += 1
28+
29+
def nickname(y = 25, font = "PermanentMarker36", color = ugfx.BLACK):
30+
nick = badge.nvs_get_str("owner", "name", 'Jan de Boer')
31+
ugfx.string_box(0,y,296,38, nick, font, color, ugfx.justifyCenter)
32+
33+
def battery(vUsb, vBatt, charging):
34+
if charging and vUsb>4000:
35+
try:
36+
badge.eink_png(0,0,'/lib/resources/chrg.png')
37+
except:
38+
ugfx.string(0, 0, "CHRG",'Roboto_Regular12',ugfx.BLACK)
39+
elif vUsb>4000:
40+
try:
41+
badge.eink_png(0,0,'/lib/resources/usb.png')
42+
except:
43+
ugfx.string(0, 0, "USB",'Roboto_Regular12',ugfx.BLACK)
44+
else:
45+
width = round((vBatt-vMin) / (vMax-vMin) * 44)
46+
if width < 0:
47+
width = 0
48+
elif width > 38:
49+
width = 38
50+
ugfx.box(2,2,46,18,ugfx.BLACK)
51+
ugfx.box(48,7,2,8,ugfx.BLACK)
52+
ugfx.area(3,3,width,16,ugfx.BLACK)

esp32/modules/easyrtc.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# File: easyrtc.py
2+
# Version: 1
3+
# Description: Wrapper that makes using the clock simple
4+
# License: MIT
5+
# Authors: Renze Nicolai <renze@rnplus.nl>
6+
7+
import machine, time
8+
9+
# Functions
10+
def string(date=False, time=True):
11+
[year, month, mday, wday, hour, min, sec, usec] = machine.RTC().datetime()
12+
monthstr = str(month)
13+
if (month<10):
14+
monthstr = "0"+monthstr
15+
daystr = str(mday)
16+
if (mday<10):
17+
daystr = "0"+daystr
18+
hourstr = str(hour)
19+
if (hour<10):
20+
hourstr = "0"+hourstr
21+
minstr = str(min)
22+
if (min<10):
23+
minstr = "0"+minstr
24+
output = ""
25+
if date:
26+
output += daystr+"-"+monthstr+"-"+str(year)
27+
if time:
28+
output += " "
29+
if time:
30+
output += hourstr+":"+minstr
31+
return output
32+
33+
def configure():
34+
import easywifi
35+
import easydraw
36+
import ntp
37+
if not easywifi.status():
38+
if not easywifi.enable():
39+
return False
40+
easydraw.msg("Configuring clock...", True)
41+
ntp.set_NTP_time()
42+
easydraw.msg("Done")
43+
return True

esp32/modules/easywifi.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# File: easywifi.py
2+
# Version: 1
3+
# Description: Wrapper that makes using wifi simple
4+
# License: MIT
5+
# Authors: Renze Nicolai <renze@rnplus.nl>
6+
7+
import time, network, badge, easydraw
8+
9+
state = False
10+
11+
def status():
12+
global state
13+
return state
14+
15+
def force_enable():
16+
global state
17+
state = False
18+
enable()
19+
20+
def enable():
21+
global state
22+
if not state:
23+
nw = network.WLAN(network.STA_IF)
24+
if not nw.isconnected():
25+
nw.active(True)
26+
ssid = badge.nvs_get_str('badge', 'wifi.ssid', 'SHA2017-insecure')
27+
password = badge.nvs_get_str('badge', 'wifi.password')
28+
nw.connect(ssid, password) if password else nw.connect(ssid)
29+
30+
easydraw.msg("Connecting to WiFi...", True)
31+
easydraw.msg("SSID: "+ssid)
32+
33+
timeout = badge.nvs_get_u8('badge', 'wifi.timeout', 40)
34+
while not nw.isconnected():
35+
time.sleep(0.1)
36+
timeout = timeout - 1
37+
if (timeout<1):
38+
easydraw.msg("Timeout while connecting!")
39+
disable()
40+
return False
41+
easydraw.msg("Connected!")
42+
state = True
43+
return True
44+
45+
def disable():
46+
global state
47+
state = False
48+
nw = network.WLAN(network.STA_IF)
49+
nw.active(False)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy