Skip to content

Initial code changes after linting #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[flake8]
max-line-length = 127
ignore = D, Q000, P101, W605, S311, PLW, PLC, PLR
per-file-ignores =
buildhat/__init__.py:F401
exclude = docs/conf.py, docs/sphinxcontrib/cmtinc-buildhat.py, docs/sphinx_selective_exclude/*.py
5 changes: 4 additions & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ jobs:
- name: Install dependencies for testing
run: |
if [ -f requirements-test.txt ]; then pip install -r requirements-test.txt; fi
- name: set pythonpath
run: |
echo "PYTHONPATH=." >> $GITHUB_ENV
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude="docs/conf.py docs/sphinx_selective_exclude/modindex_exclude.py"
flake8 . --count --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
124 changes: 124 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,129 @@
# Change Log

## 0.5.9

### Added

* Allow the BuildHAT LEDs to be turned on and off
* Allow for White in set\_pixels
* Prevent using same port multiple times
* Add support for checking maximum force sensed

### Changed

* Linting of code
* Renamed exceptions to conform to linter's style

### Removed

n/a

## 0.5.8

### Added

* LED Matrix transitions
* Expose feature to measure voltage of hat
* Function to set brightness of LEDs

### Changed

* New firmware to fix passive devices in hardware revision

### Removed

n/a

## 0.5.7

### Added

* Support for light
* Allow alternative serial device to be used
* Passive motor support
* WeDo sensor support

### Changed

n/a

### Removed

n/a

## 0.5.6

### Added


### Changed

n/a

### Removed

n/a

## 0.5.5

### Added


### Changed

n/a

### Removed

n/a

## 0.5.4

### Added

Documentation copy updates

### Changed

n/a

### Removed

n/a

## 0.5.3

### Added

* Force sensor now supports force threshold
* Simplify list of colours supported by colour sensor
* Fix distance sensor firing bug
* Simplify coast call

### Changed

n/a

### Removed

n/a

## 0.5.2

### Added

* Fixes force sensor callback only firing one
* Pass distance to distance sensor callback
* Make sure motors move simultaneously for a motor pair

### Changed

n/a

### Removed

n/a

## 0.5.1

### Added
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.8
0.5.9
14 changes: 7 additions & 7 deletions buildhat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from .motors import Motor, MotorPair, PassiveMotor
from .distance import DistanceSensor
from .force import ForceSensor
from .color import ColorSensor
from .colordistance import ColorDistanceSensor
from .matrix import Matrix
from .wedo import TiltSensor, MotionSensor
from .light import Light
from .distance import DistanceSensor
from .exc import * # noqa: F403
from .force import ForceSensor
from .hat import Hat
from .light import Light
from .matrix import Matrix
from .motors import Motor, MotorPair, PassiveMotor
from .serinterface import BuildHAT
from .exc import *
from .wedo import MotionSensor, TiltSensor
70 changes: 37 additions & 33 deletions buildhat/color.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from .devices import Device
from .exc import DeviceInvalid
from threading import Condition
from collections import deque
import math
from collections import deque
from threading import Condition

from .devices import Device


class ColorSensor(Device):
"""Color sensor

:param port: Port of device
:raises DeviceInvalid: Occurs if there is no color sensor attached to port
:raises DeviceError: Occurs if there is no color sensor attached to port
"""
def __init__(self, port):
super().__init__(port)
Expand All @@ -23,14 +24,14 @@ def segment_color(self, r, g, b):
:return: Name of the color as a string
:rtype: str
"""
table = [("black",(0,0,0)),
("violet",(127,0,255)),
("blue",(0,0,255)),
("cyan",(0,183,235)),
("green",(0,128,0)),
("yellow",(255,255,0)),
("red",(255,0,0)),
("white",(255,255,255))]
table = [("black", (0, 0, 0)),
("violet", (127, 0, 255)),
("blue", (0, 0, 255)),
("cyan", (0, 183, 235)),
("green", (0, 128, 0)),
("yellow", (255, 255, 0)),
("red", (255, 0, 0)),
("white", (255, 255, 255))]
near = ""
euc = math.inf
for itm in table:
Expand All @@ -39,7 +40,7 @@ def segment_color(self, r, g, b):
near = itm[0]
euc = cur
return near

def rgb_to_hsv(self, r, g, b):
"""Convert RGB to HSV

Expand All @@ -48,7 +49,7 @@ def rgb_to_hsv(self, r, g, b):
:return: HSV representation of color
:rtype: tuple
"""
r, g, b = r/255.0, g/255.0, b/255.0
r, g, b = r / 255.0, g / 255.0, b / 255.0
cmax = max(r, g, b)
cmin = min(r, g, b)
delt = cmax - cmin
Expand All @@ -57,15 +58,15 @@ def rgb_to_hsv(self, r, g, b):
elif cmax == r:
h = 60 * (((g - b) / delt) % 6)
elif cmax == g:
h = 60 * ((((b - r) / delt)) + 2)
h = 60 * ((((b - r) / delt)) + 2)
elif cmax == b:
h = 60 * ((((r - g) / delt)) + 4)
if cmax == 0:
s = 0
else:
s = delt / cmax
v = cmax
return int(h), int(s*100), int(v*100)
return int(h), int(s * 100), int(v * 100)

def get_color(self):
"""Returns the color
Expand All @@ -84,10 +85,10 @@ def get_ambient_light(self):
"""
self.mode(2)
readings = []
for i in range(self.avg_reads):
for _ in range(self.avg_reads):
readings.append(self.get()[0])
return int(sum(readings)/len(readings))
return int(sum(readings) / len(readings))

def get_reflected_light(self):
"""Returns the reflected light

Expand All @@ -96,51 +97,54 @@ def get_reflected_light(self):
"""
self.mode(1)
readings = []
for i in range(self.avg_reads):
for _ in range(self.avg_reads):
readings.append(self.get()[0])
return int(sum(readings)/len(readings))
return int(sum(readings) / len(readings))

def _avgrgbi(self, reads):
readings = []
for read in reads:
read = [int((read[0]/1024)*255), int((read[1]/1024)*255), int((read[2]/1024)*255), int((read[3]/1024)*255)]
read = [int((read[0] / 1024) * 255),
int((read[1] / 1024) * 255),
int((read[2] / 1024) * 255),
int((read[3] / 1024) * 255)]
readings.append(read)
rgbi = []
for i in range(4):
rgbi.append(int(sum([rgbi[i] for rgbi in readings]) / len(readings)))
return rgbi

def get_color_rgbi(self):
"""Returns the color
"""Returns the color

:return: RGBI representation
:return: RGBI representation
:rtype: list
"""
self.mode(5)
reads = []
for i in range(self.avg_reads):
for _ in range(self.avg_reads):
reads.append(self.get())
return self._avgrgbi(reads)

def get_color_hsv(self):
"""Returns the color
"""Returns the color

:return: HSV representation
:return: HSV representation
:rtype: tuple
"""
self.mode(6)
readings = []
for i in range(self.avg_reads):
for _ in range(self.avg_reads):
read = self.get()
read = [read[0], int((read[1]/1024)*100), int((read[2]/1024)*100)]
read = [read[0], int((read[1] / 1024) * 100), int((read[2] / 1024) * 100)]
readings.append(read)
s = c = 0
for hsv in readings:
hue = hsv[0]
s += math.sin(math.radians(hue))
c += math.cos(math.radians(hue))
c += math.cos(math.radians(hue))

hue = int((math.degrees((math.atan2(s,c))) + 360) % 360)
hue = int((math.degrees((math.atan2(s, c))) + 360) % 360)
sat = int(sum([hsv[1] for hsv in readings]) / len(readings))
val = int(sum([hsv[2] for hsv in readings]) / len(readings))
return (hue, sat, val)
Expand All @@ -158,7 +162,7 @@ def _cb_handle(self, lst):
def wait_until_color(self, color):
"""Waits until specific color

:param color: Color to look for
:param color: Color to look for
"""
self.mode(5)
self._cond = Condition()
Expand Down
Loading