Skip to content

Commit a8863b0

Browse files
committed
updated fluepdot.py to a class
so that multiple instances can be created updated README.md accordingly
1 parent ca6695d commit a8863b0

2 files changed

Lines changed: 75 additions & 77 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
This is a small collection of functions for interacting with a
44
[fluepdot](https://fluepdot.readthedocs.io/en/latest/) module.
55

6-
the first call after importing should always be to set the URL at
7-
which the module can be reached.
6+
import Fluepdot class and create an instance with link as the first arg
87

98
```python
10-
import fluepdot as fd
9+
from fluepdot import Fluepdot
1110

12-
fd.set_url("module.local")
11+
fd = Fluepdot("http://module.local")
1312
```
1413

1514
## Functions
1615

1716

1817
function | args | default values | return type | description
1918
---|---|---|---|---
19+
_init_ | baseURL: str, width: int, height: int | x=115, y=16 | fluepdot.Fluepdot | Constructor for Fluepdot class
2020
post_time ||| None | indefinitly sets the module to display the current time.
2121
get_size ||| (int, int) | returns the size of the connected display
2222
get_frame ||| str | returns the current frame stored by the module

src/fluepdot/fluepdot.py

Lines changed: 71 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -25,119 +25,117 @@ class Mode(Enum):
2525

2626

2727
# endpoints:
28-
baseURL: str = None
2928
frameURL: str = "/framebuffer"
3029
pixelURL: str = "/pixel"
3130
textURL: str = "/framebuffer/text"
3231
fontURL: str = "/fonts"
3332
modeURL: str = "/rendering/mode"
3433

35-
# size defaults:
36-
width: int = 115
37-
height: int = 16
34+
class Fluepdot:
35+
def __init__(self, baseURL: str, width: int = 115, height: int = 16):
36+
self.baseURL = baseURL
37+
self.width = width
38+
self.height = height
39+
self.fonts: Optional[List[str]] = None
3840

39-
fonts: Optional[List[str]] = None
4041

42+
def set_url(self, url: str):
43+
self.baseURL = url
4144

42-
def set_url(url: str):
43-
global baseURL
44-
baseURL = url
4545

46+
def post_time(self) -> None:
47+
import datetime
48+
dt: str = ""
49+
while True:
50+
ndt: str = datetime.datetime.now().strftime("%d.%m.%y %H:%M")
51+
if ndt != dt:
52+
dt = ndt
53+
post_text(dt, x=8, y=1, font="fixed_7x14")
4654

47-
def post_time() -> None:
48-
import datetime
49-
dt: str = ""
50-
while True:
51-
ndt: str = datetime.datetime.now().strftime("%d.%m.%y %H:%M")
52-
if ndt != dt:
53-
dt = ndt
54-
post_text(dt, x=8, y=1, font="fixed_7x14")
5555

56+
def get_size(self) -> (int, int):
57+
frame = get_frame()
58+
self.width = len(frame[0])
59+
self.height = len(frame) - 1
60+
return [self.width, self.height]
5661

57-
def get_size() -> (int, int):
58-
global width, height
59-
frame = get_frame()
60-
width = len(frame[0])
61-
height = len(frame) - 1
62-
return [width, height]
6362

63+
def get_frame(self) -> List[str]:
64+
r = _get(frameURL)
65+
return r.text.split('\n')
6466

65-
def get_frame() -> List[str]:
66-
r = _get(frameURL)
67-
return r.text.split('\n')
6867

68+
def get_pixel(self, x: int = 0, y: int = 0) -> bool | None:
69+
r = _get(pixelURL, get={"x": x, "y": y})
70+
rtn = True if r.text == "X" else False if r.text == " " else None
71+
return rtn
6972

70-
def get_pixel(x: int = 0, y: int = 0) -> bool:
71-
r = _get(pixelURL, get={"x": x, "y": y})
72-
rtn = True if r.text == "X" else False if r.text == " " else None
73-
return rtn
7473

74+
def get_fonts(self) -> None:
75+
r = _get(fontURL)
76+
fonts = r.text.split("\n")
77+
print(fonts)
7578

76-
def get_fonts() -> None:
77-
r = _get(fontURL)
78-
fonts = r.text.split("\n")
79-
print(fonts)
8079

80+
def get_mode(self) -> Mode:
81+
r = _get(modeURL)
82+
return Mode(r.text)
8183

82-
def get_mode() -> Mode:
83-
r = _get(modeURL)
84-
return Mode(r.text)
8584

85+
def post_text(self, text: str, x: int = 0, y: int = 0, font: str = "DejaVuSans12") -> Response:
86+
return _post(textURL, get={"x": x, "y": y, "font": font}, post=text)
8687

87-
def post_text(text: str, x: int = 0, y: int = 0, font: str = "DejaVuSans12") -> Response:
88-
return _post(textURL, get={"x": x, "y": y, "font": font}, post=text)
8988

89+
def post_frame_raw(self, frame: str) -> Response:
90+
return _post(frameURL, post=frame)
9091

91-
def post_frame_raw(frame: str) -> Response:
92-
return _post(frameURL, post=frame)
9392

93+
def post_frame(self, frame: List[List[bool]]) -> Response:
94+
data: List[List[str]] = [[" "] * width for _ in range(height)]
95+
for x, l in frame:
96+
for y, b in l:
97+
if b:
98+
try:
99+
data[x, y] = "X"
100+
except IndexError as e:
101+
print(e)
102+
return _post(frameURL, post=data)
94103

95-
def post_frame(frame: List[List[bool]]) -> Response:
96-
data: List[List[str]] = [[" "] * width for _ in range(height)]
97-
for x, l in frame:
98-
for y, b in l:
99-
if b:
100-
try:
101-
data[x, y] = "X"
102-
except IndexError as e:
103-
print(e)
104-
return _post(frameURL, post=data)
105104

105+
def set_pixel(self, x: int = 0, y: int = 0) -> Response:
106+
return _post(pixelURL, get={"x": x, "y": y})
106107

107-
def set_pixel(x: int = 0, y: int = 0) -> Response:
108-
return _post(pixelURL, get={"x": x, "y": y})
109108

109+
def unset_pixel(self, x: int = 0, y: int = 0) -> Response:
110+
return _delete(pixelURL, get={"x": x, "y": y})
110111

111-
def unset_pixel(x: int = 0, y: int = 0) -> Response:
112-
return _delete(pixelURL, get={"x": x, "y": y})
113112

113+
def set_mode(self, mode: Mode = Mode.FULL) -> Response:
114+
return _put(modeURL, post=str(mode.value))
114115

115-
def set_mode(mode: Mode = Mode.FULL) -> Response:
116-
return _put(modeURL, post=str(mode.value))
117116

117+
def _delete(self, endpoint: str, get: GetParam = {}, post: PostParam = '') -> Response:
118+
if self.baseURL == None:
119+
raise RuntimeError('baseURL is None, call set_url')
120+
return requests.delete(url=self.baseURL + endpoint, params=get)
118121

119-
def _delete(endpoint: str, get: GetParam = {}, post: PostParam = '') -> Response:
120-
if baseURL == None:
121-
raise RuntimeError('baseURL is None, call set_url')
122-
return requests.delete(url=baseURL + endpoint, params=get)
123122

123+
def _post(self, endpoint: str, get: GetParam = {}, post: PostParam = '') -> Response:
124+
if self.baseURL == None:
125+
raise RuntimeError('baseURL is None, call set_url')
126+
return requests.post(url=self.baseURL + endpoint, params=get, data=post)
124127

125-
def _post(endpoint: str, get: GetParam = {}, post: PostParam = '') -> Response:
126-
if baseURL == None:
127-
raise RuntimeError('baseURL is None, call set_url')
128-
return requests.post(url=baseURL + endpoint, params=get, data=post)
129128

129+
def _put(self, endpoint: str, get: GetParam = {}, post: PostParam = '') -> Response:
130+
if self.baseURL == None:
131+
raise RuntimeError('baseURL is None, call set_url')
132+
return requests.put(url=self.baseURL + endpoint, params=get, data=post)
130133

131-
def _put(endpoint: str, get: GetParam = {}, post: PostParam = '') -> Response:
132-
if baseURL == None:
133-
raise RuntimeError('baseURL is None, call set_url')
134-
return requests.put(url=baseURL + endpoint, params=get, data=post)
135134

136-
137-
def _get(endpoint: str, get: GetParam = {}) -> Response:
138-
if baseURL == None:
139-
raise RuntimeError('baseURL is None, call set_url')
140-
return requests.get(url=baseURL + endpoint, params=get)
135+
def _get(self, endpoint: str, get: GetParam = {}) -> Response:
136+
if self.baseURL == None:
137+
raise RuntimeError('baseURL is None, call set_url')
138+
return requests.get(url=self.baseURL + endpoint, params=get)
141139

142140

143141
if __name__ == "__main__":

0 commit comments

Comments
 (0)