Skip to content

Commit 611a849

Browse files
authored
Initial code changes after linting (#142)
1 parent e0f5be6 commit 611a849

28 files changed

+465
-303
lines changed

.flake8

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[flake8]
2+
max-line-length = 127
3+
ignore = D, Q000, P101, W605, S311, PLW, PLC, PLR
4+
per-file-ignores =
5+
buildhat/__init__.py:F401
6+
exclude = docs/conf.py, docs/sphinxcontrib/cmtinc-buildhat.py, docs/sphinx_selective_exclude/*.py

.github/workflows/python-app.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ jobs:
2626
- name: Install dependencies for testing
2727
run: |
2828
if [ -f requirements-test.txt ]; then pip install -r requirements-test.txt; fi
29+
- name: set pythonpath
30+
run: |
31+
echo "PYTHONPATH=." >> $GITHUB_ENV
2932
- name: Lint with flake8
3033
run: |
3134
# stop the build if there are Python syntax errors or undefined names
32-
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude="docs/conf.py docs/sphinx_selective_exclude/modindex_exclude.py"
35+
flake8 . --count --show-source --statistics
3336
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
3437
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

CHANGELOG.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,129 @@
11
# Change Log
22

3+
## 0.5.9
4+
5+
### Added
6+
7+
* Allow the BuildHAT LEDs to be turned on and off
8+
* Allow for White in set\_pixels
9+
* Prevent using same port multiple times
10+
* Add support for checking maximum force sensed
11+
12+
### Changed
13+
14+
* Linting of code
15+
* Renamed exceptions to conform to linter's style
16+
17+
### Removed
18+
19+
n/a
20+
21+
## 0.5.8
22+
23+
### Added
24+
25+
* LED Matrix transitions
26+
* Expose feature to measure voltage of hat
27+
* Function to set brightness of LEDs
28+
29+
### Changed
30+
31+
* New firmware to fix passive devices in hardware revision
32+
33+
### Removed
34+
35+
n/a
36+
37+
## 0.5.7
38+
39+
### Added
40+
41+
* Support for light
42+
* Allow alternative serial device to be used
43+
* Passive motor support
44+
* WeDo sensor support
45+
46+
### Changed
47+
48+
n/a
49+
50+
### Removed
51+
52+
n/a
53+
54+
## 0.5.6
55+
56+
### Added
57+
58+
59+
### Changed
60+
61+
n/a
62+
63+
### Removed
64+
65+
n/a
66+
67+
## 0.5.5
68+
69+
### Added
70+
71+
72+
### Changed
73+
74+
n/a
75+
76+
### Removed
77+
78+
n/a
79+
80+
## 0.5.4
81+
82+
### Added
83+
84+
Documentation copy updates
85+
86+
### Changed
87+
88+
n/a
89+
90+
### Removed
91+
92+
n/a
93+
94+
## 0.5.3
95+
96+
### Added
97+
98+
* Force sensor now supports force threshold
99+
* Simplify list of colours supported by colour sensor
100+
* Fix distance sensor firing bug
101+
* Simplify coast call
102+
103+
### Changed
104+
105+
n/a
106+
107+
### Removed
108+
109+
n/a
110+
111+
## 0.5.2
112+
113+
### Added
114+
115+
* Fixes force sensor callback only firing one
116+
* Pass distance to distance sensor callback
117+
* Make sure motors move simultaneously for a motor pair
118+
119+
### Changed
120+
121+
n/a
122+
123+
### Removed
124+
125+
n/a
126+
3127
## 0.5.1
4128

5129
### Added

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.8
1+
0.5.9

buildhat/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from .motors import Motor, MotorPair, PassiveMotor
2-
from .distance import DistanceSensor
3-
from .force import ForceSensor
41
from .color import ColorSensor
52
from .colordistance import ColorDistanceSensor
6-
from .matrix import Matrix
7-
from .wedo import TiltSensor, MotionSensor
8-
from .light import Light
3+
from .distance import DistanceSensor
4+
from .exc import * # noqa: F403
5+
from .force import ForceSensor
96
from .hat import Hat
7+
from .light import Light
8+
from .matrix import Matrix
9+
from .motors import Motor, MotorPair, PassiveMotor
1010
from .serinterface import BuildHAT
11-
from .exc import *
11+
from .wedo import MotionSensor, TiltSensor

buildhat/color.py

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
from .devices import Device
2-
from .exc import DeviceInvalid
3-
from threading import Condition
4-
from collections import deque
51
import math
2+
from collections import deque
3+
from threading import Condition
4+
5+
from .devices import Device
6+
67

78
class ColorSensor(Device):
89
"""Color sensor
910
1011
:param port: Port of device
11-
:raises DeviceInvalid: Occurs if there is no color sensor attached to port
12+
:raises DeviceError: Occurs if there is no color sensor attached to port
1213
"""
1314
def __init__(self, port):
1415
super().__init__(port)
@@ -23,14 +24,14 @@ def segment_color(self, r, g, b):
2324
:return: Name of the color as a string
2425
:rtype: str
2526
"""
26-
table = [("black",(0,0,0)),
27-
("violet",(127,0,255)),
28-
("blue",(0,0,255)),
29-
("cyan",(0,183,235)),
30-
("green",(0,128,0)),
31-
("yellow",(255,255,0)),
32-
("red",(255,0,0)),
33-
("white",(255,255,255))]
27+
table = [("black", (0, 0, 0)),
28+
("violet", (127, 0, 255)),
29+
("blue", (0, 0, 255)),
30+
("cyan", (0, 183, 235)),
31+
("green", (0, 128, 0)),
32+
("yellow", (255, 255, 0)),
33+
("red", (255, 0, 0)),
34+
("white", (255, 255, 255))]
3435
near = ""
3536
euc = math.inf
3637
for itm in table:
@@ -39,7 +40,7 @@ def segment_color(self, r, g, b):
3940
near = itm[0]
4041
euc = cur
4142
return near
42-
43+
4344
def rgb_to_hsv(self, r, g, b):
4445
"""Convert RGB to HSV
4546
@@ -48,7 +49,7 @@ def rgb_to_hsv(self, r, g, b):
4849
:return: HSV representation of color
4950
:rtype: tuple
5051
"""
51-
r, g, b = r/255.0, g/255.0, b/255.0
52+
r, g, b = r / 255.0, g / 255.0, b / 255.0
5253
cmax = max(r, g, b)
5354
cmin = min(r, g, b)
5455
delt = cmax - cmin
@@ -57,15 +58,15 @@ def rgb_to_hsv(self, r, g, b):
5758
elif cmax == r:
5859
h = 60 * (((g - b) / delt) % 6)
5960
elif cmax == g:
60-
h = 60 * ((((b - r) / delt)) + 2)
61+
h = 60 * ((((b - r) / delt)) + 2)
6162
elif cmax == b:
6263
h = 60 * ((((r - g) / delt)) + 4)
6364
if cmax == 0:
6465
s = 0
6566
else:
6667
s = delt / cmax
6768
v = cmax
68-
return int(h), int(s*100), int(v*100)
69+
return int(h), int(s * 100), int(v * 100)
6970

7071
def get_color(self):
7172
"""Returns the color
@@ -84,10 +85,10 @@ def get_ambient_light(self):
8485
"""
8586
self.mode(2)
8687
readings = []
87-
for i in range(self.avg_reads):
88+
for _ in range(self.avg_reads):
8889
readings.append(self.get()[0])
89-
return int(sum(readings)/len(readings))
90-
90+
return int(sum(readings) / len(readings))
91+
9192
def get_reflected_light(self):
9293
"""Returns the reflected light
9394
@@ -96,51 +97,54 @@ def get_reflected_light(self):
9697
"""
9798
self.mode(1)
9899
readings = []
99-
for i in range(self.avg_reads):
100+
for _ in range(self.avg_reads):
100101
readings.append(self.get()[0])
101-
return int(sum(readings)/len(readings))
102+
return int(sum(readings) / len(readings))
102103

103104
def _avgrgbi(self, reads):
104105
readings = []
105106
for read in reads:
106-
read = [int((read[0]/1024)*255), int((read[1]/1024)*255), int((read[2]/1024)*255), int((read[3]/1024)*255)]
107+
read = [int((read[0] / 1024) * 255),
108+
int((read[1] / 1024) * 255),
109+
int((read[2] / 1024) * 255),
110+
int((read[3] / 1024) * 255)]
107111
readings.append(read)
108112
rgbi = []
109113
for i in range(4):
110114
rgbi.append(int(sum([rgbi[i] for rgbi in readings]) / len(readings)))
111115
return rgbi
112116

113117
def get_color_rgbi(self):
114-
"""Returns the color
118+
"""Returns the color
115119
116-
:return: RGBI representation
120+
:return: RGBI representation
117121
:rtype: list
118122
"""
119123
self.mode(5)
120124
reads = []
121-
for i in range(self.avg_reads):
125+
for _ in range(self.avg_reads):
122126
reads.append(self.get())
123127
return self._avgrgbi(reads)
124128

125129
def get_color_hsv(self):
126-
"""Returns the color
130+
"""Returns the color
127131
128-
:return: HSV representation
132+
:return: HSV representation
129133
:rtype: tuple
130134
"""
131135
self.mode(6)
132136
readings = []
133-
for i in range(self.avg_reads):
137+
for _ in range(self.avg_reads):
134138
read = self.get()
135-
read = [read[0], int((read[1]/1024)*100), int((read[2]/1024)*100)]
139+
read = [read[0], int((read[1] / 1024) * 100), int((read[2] / 1024) * 100)]
136140
readings.append(read)
137141
s = c = 0
138142
for hsv in readings:
139143
hue = hsv[0]
140144
s += math.sin(math.radians(hue))
141-
c += math.cos(math.radians(hue))
145+
c += math.cos(math.radians(hue))
142146

143-
hue = int((math.degrees((math.atan2(s,c))) + 360) % 360)
147+
hue = int((math.degrees((math.atan2(s, c))) + 360) % 360)
144148
sat = int(sum([hsv[1] for hsv in readings]) / len(readings))
145149
val = int(sum([hsv[2] for hsv in readings]) / len(readings))
146150
return (hue, sat, val)
@@ -158,7 +162,7 @@ def _cb_handle(self, lst):
158162
def wait_until_color(self, color):
159163
"""Waits until specific color
160164
161-
:param color: Color to look for
165+
:param color: Color to look for
162166
"""
163167
self.mode(5)
164168
self._cond = Condition()

0 commit comments

Comments
 (0)