Skip to content

Commit 237e645

Browse files
authored
Merge pull request #115 from G3zz/tcs3472-rebased
Add support for the TCS34725 colour sensor
2 parents 9a37f09 + 9a67533 commit 237e645

26 files changed

+673
-11
lines changed

README.rst renamed to README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Contributors
6060
* `Serge Schneider`_
6161
* `Dave Jones`_
6262
* `Tyler Laws`_
63+
* `George Boukeas`_
6364

6465
Open Source
6566
===========

debian/changelog

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
python-sense-hat (2.3.1~test0) UNRELEASED; urgency=medium
2+
3+
* v2.3.1 alpha
4+
5+
-- George Boukeas <[email protected]> Wed, 21 Jul 2021 15:11:00 +0000
6+
7+
python-sense-hat (2.3.0~test0) UNRELEASED; urgency=medium
8+
9+
* v2.3.0 alpha
10+
11+
-- Serge Schneider <[email protected]> Fri, 11 Dec 2020 14:41:32 +0000
12+
13+
python-sense-hat (2.2.0-1) jessie; urgency=medium
14+
15+
* v2.2.0 release
16+
17+
-- Serge Schneider <[email protected]> Sun, 07 Aug 2016 14:32:16 +0100
18+
19+
python-sense-hat (2.1.0-1) jessie; urgency=low
20+
21+
* v2.1.0 release
22+
23+
-- Serge Schneider <[email protected]> Tue, 25 Aug 2015 05:19:02 +0100
24+
25+
python-sense-hat (2.0.0-1) jessie; urgency=low
26+
27+
* v2.0.0 release
28+
* Rename to python-sense-hat
29+
30+
-- Serge Schneider <[email protected]> Fri, 21 Aug 2015 19:36:23 +0100
31+
32+
astropi (1.1.5-1) jessie; urgency=low
33+
34+
* Initial release
35+
36+
-- Serge Schneider <[email protected]> Fri, 03 Jul 2015 03:24:45 +0100

debian/compat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9

debian/control

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Source: python-sense-hat
2+
Section: python
3+
Priority: optional
4+
Maintainer: Serge Schneider <[email protected]>
5+
Build-Depends: debhelper (>= 9~), dh-python, python-all (>= 2.6.6-3~),
6+
python-setuptools, python3-all, python3-setuptools
7+
Standards-Version: 4.3.0
8+
Homepage: https://github.com/RPi-Distro/python-sense-hat
9+
Vcs-Git: git://github.com/RPi-Distro/python-sense-hat.git -b debian
10+
Vcs-Browser: https://github.com/RPi-Distro/python-sense-hat/tree/debian
11+
12+
Package: python-sense-hat
13+
Architecture: all
14+
Depends: ${misc:Depends}, ${python:Depends}, python-rtimulib, python-pil,
15+
python-numpy
16+
Description: Sense HAT python library (Python 2)
17+
Python module to control the Sense HAT for the Raspberry Pi used
18+
in the Astro Pi mission - an education outreach programme for UK schools
19+
sending code experiments to the International Space Station
20+
.
21+
This package installs the library for Python 2.
22+
23+
Package: python3-sense-hat
24+
Architecture: all
25+
Depends: ${misc:Depends}, ${python3:Depends}, python3-rtimulib, python3-pil,
26+
python3-numpy, python3-smbus
27+
Description: Sense HAT python library (Python 3)
28+
Python module to control the Sense HAT for the Raspberry Pi used
29+
in the Astro Pi mission - an education outreach programme for UK schools
30+
sending code experiments to the International Space Station
31+
.
32+
This package installs the library for Python 3.

debian/copyright

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
2+
Upstream-Name: python-sense-hat
3+
Source: https://github.com/RPi-Distro/python-sense-hat
4+
5+
Files: *
6+
Copyright: 2015 Raspberry Pi Foundation
7+
License: BSD-3-Clause
8+
9+
License: BSD-3-Clause
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions
12+
are met:
13+
1. Redistributions of source code must retain the above copyright
14+
notice, this list of conditions and the following disclaimer.
15+
2. Redistributions in binary form must reproduce the above copyright
16+
notice, this list of conditions and the following disclaimer in the
17+
documentation and/or other materials provided with the distribution.
18+
3. Neither the name of the University nor the names of its contributors
19+
may be used to endorse or promote products derived from this software
20+
without specific prior written permission.
21+
.
22+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE HOLDERS OR
26+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

debian/rules

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/make -f
2+
# See debhelper(7) (uncomment to enable)
3+
# output every command that modifies files on the build system.
4+
#DH_VERBOSE = 1
5+
6+
DPKG_EXPORT_BUILDFLAGS = 1
7+
include /usr/share/dpkg/default.mk
8+
9+
10+
export PYBUILD_NAME = sense-hat
11+
12+
%:
13+
dh $@ --with python2,python3 --buildsystem=pybuild

debian/source/format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.0 (native)

docs/api.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,3 +774,131 @@ Note that the `direction_any` event is always called *after* all other events
774774
making it an ideal hook for things like display refreshing (as in the example
775775
above).
776776

777+
- - -
778+
## Light and colour sensor
779+
780+
The v2 Sense HAT includes a TCS34725 colour sensor that is capable of measuring the amount of Red, Green and Blue (RGB) in the incident light, as well as providing a Clear light (brightness) reading.
781+
782+
You can interact with the colour sensor through the `colour` (or `color`) attribute of the Sense HAT, which corresponds to a `ColourSensor` object.
783+
784+
The example below serves as an overview of how the colour sensor can be used, while the sections that follow provide additional details and explanations.
785+
786+
```python
787+
from sense_hat import SenseHat
788+
from time import sleep
789+
790+
sense = SenseHat()
791+
sense.color.gain = 4
792+
sense.color.integration_cycles = 64
793+
794+
while True:
795+
sleep(2 * sense.colour.integration_time)
796+
red, green, blue, clear = sense.colour.colour # readings scaled to 0-256
797+
print(f"R: {red}, G: {green}, B: {blue}, C: {clear}")
798+
```
799+
800+
---
801+
### Obtaining RGB and Clear light readings
802+
803+
The `colour` (or `color`) property of the `ColourSensor` object is a 4-tuple containing the measured values for Red, Green and Blue (RGB), along with a Clear light value, which is a measure of brightness. Individual colour and light readings can also be obtained through the `red`, `green`, `blue` and `clear` properties of the `ColourSensor` object.
804+
805+
`ColourSensor` property | Returned type | Explanation
806+
--- | --- | ---
807+
`red` | int | The amount of incident red light, scaled to 0-256
808+
`green` | int | The amount of incident green light, scaled to 0-256
809+
`blue` | int | The amount of incident blue light, scaled to 0-256
810+
`clear` | int | The amount of incident light (brightness), scaled to 0-256
811+
`colour` | tuple | A 4-tuple containing the RGBC (Red, Green, Blue and Clear) sensor readings, each scaled to 0-256
812+
813+
These are all read-only properties; they cannot be set.
814+
815+
Note that, in the current implementation, the four values accessed through the `colour` property are retrieved through a single sensor reading. Obtaining these values through the `red`, `green`, `blue` and `clear` properties would require four separate readings.
816+
817+
---
818+
### Gain
819+
820+
In sensors, the term "gain" can be understood as being synonymous to _sensitivity_. A higher gain setting means the output values will be greater for the same input.
821+
822+
There are four possible gain values for the colour sensor: `1`, `4`, `16` and `60`, with the default value being `1`. You can get or set the sensor gain through the `gain` property of the `ColourSensor` object. An attempt to set the gain to a value that is not valid will result in an `InvalidGainError` exception being raised.
823+
824+
```python
825+
from sense_hat import SenseHAT
826+
from time import sleep
827+
828+
sense = SenseHat()
829+
sense.colour.gain = 1
830+
sleep(1)
831+
print(f"Gain: {sense.colour.gain}")
832+
print(f"RGBC: {sense.colour.colour}")
833+
834+
sense.colour.gain = 16
835+
sleep(1)
836+
print(f"Gain: {sense.colour.gain}")
837+
print(f"RGBC: {sense.colour.colour}")
838+
```
839+
840+
Under the same lighting conditions, the RGBC values should be considerably higher when the gain setting is increased.
841+
842+
When there is very little ambient light and the RGBC values are low, it makes sense to use a higher gain setting. Conversely, when there is too much light and the RGBC values are maximal, the sensor is saturated and the gain should be set to lower values.
843+
844+
---
845+
### Integration cycles and the interval between measurements
846+
847+
You can specify the number of _integration cycles_ required to generate a new set of sensor readings. Each integration cycle is 2.4 milliseconds long, so the number of integration cycles determines the _minimum_ amount of time required between consecutive readings.
848+
849+
You can set the number of integration cycles to any integer between `1` and `256`, through the `integration_cycles` property of the `ColourSensor` object. The default value is `1`. An attempt to set the number of integration cycles to a value that is not valid will result in a `InvalidIntegrationCyclesError` or `TypeError` exception being raised.
850+
851+
```python
852+
from sense_hat import SenseHAT
853+
from time import sleep
854+
855+
sense = SenseHat()
856+
sense.colour.integration_cycles = 100
857+
print(f"Integration cycles: {sense.colour.integration_cycles}")
858+
print(f"Minimum wait time between measurements: {sense.colour.integration_time} seconds")
859+
```
860+
861+
---
862+
### Integration cycles and raw values
863+
864+
The values of the `colour`, `red`, `green`, `blue` and `clear` properties are integers between 0 and 256. However, these are not the actual _raw_ values obtained from the sensor; they have been scaled down to this range for convenience.
865+
866+
The range of the raw values depends on the number of integration cycles:
867+
868+
`integration_cycles` | maximum raw value (`max_raw`)
869+
--- | ---
870+
1 - 64 | 1024 * `integration_cycles`
871+
\> 64 | 65536
872+
873+
What this really means is that the _accuracy_ of the sensor is affected by the number of integration cycles, i.e. the time required by the sensor to obtain a reading. A longer integration time will result in more reliable readings that fall into a wider range of values, being able to more accurately distinguish between similar lighting conditions.
874+
875+
The following properties of the `ColourSensor` object provide direct access to the raw values measured by the sensor.
876+
877+
`ColourSensor` property | Returned type | Explanation
878+
--- | --- | ---
879+
`red_raw` | int | The amount of incident red light, between 0 and `max_raw`
880+
`green_raw` | int | The amount of incident green light, between 0 and `max_raw`
881+
`blue_raw` | int | The amount of incident blue light, between 0 and `max_raw`
882+
`clear_raw` | int | The amount of incident light (brightness), between 0 and `max_raw`
883+
`colour_raw` | tuple | A 4-tuple containing the RGBC (Red, Green, Blue and Clear) raw sensor readings, each between 0 and `max_raw`
884+
885+
Here is an example comparing raw values to the corresponding scaled ones, for a given number of integration cycles.
886+
887+
```
888+
from sense_hat import SenseHAT
889+
from time import sleep
890+
891+
sense = SenseHat()
892+
sense.colour.integration_cycles = 64
893+
print(f"Minimum time between readings: {sense.colour.integration_time} seconds")
894+
print(f"Maximum raw sensor reading: {sense.colour.max_raw}")
895+
sleep(sense.colour.integration_time + 0.1) # try omitting this
896+
print(f"Current raw sensor readings: {sense.colour.colour_raw}")
897+
print(f"Scaled values: {sense.colour.colour}")
898+
```
899+
900+
## Exceptions
901+
902+
Custom Sense HAT exceptions are statically defined in the `sense_hat.exceptions` module.
903+
The exceptions relate to problems encountered while initialising the colour chip or due to setting invalid parameters.
904+
Each exception includes a message describing the issue encountered, and is subclassed from the base class `SenseHatException`.

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## v2
44

5+
### 2.3.x
6+
7+
- Added support for the light/colour sensor in the v2 Sense HAT
8+
59
### 2.2.0
610

711
- Added new stick interface for the joystick
File renamed without changes.

0 commit comments

Comments
 (0)