JoyPi-Advanced-RaspberryPi 2.0.0__py3-none-any.whl → 2.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,20 +28,60 @@
28
28
  # Step 2) sudo apt-get install i2c-tools python3-smbus
29
29
  # Step 3) plug it in (don't forget your pull-up resistors) and run this program
30
30
 
31
- import smbus
31
+ import busio
32
+ from adafruit_bus_device import i2c_device
32
33
  import time
33
34
  import threading
34
35
  import numpy as np
35
36
 
36
37
  class colour:
37
38
 
38
- def __init__(self, i2c_address = 0x10):
39
+ REG_CONF = 0x00
40
+ REG_RED = 0x08
41
+ REG_GREEN = 0x09
42
+ REG_BLUE = 0x0A
43
+ REG_WHITE = 0x0B
44
+
45
+ INTEGRATION_TIME_40MS = 0
46
+ INTEGRATION_TIME_80MS = 1
47
+ INTEGRATION_TIME_160MS = 2
48
+ INTEGRATION_TIME_320MS = 3
49
+ INTEGRATION_TIME_640MS = 4
50
+ INTEGRATION_TIME_1280MS = 5
51
+
52
+ _INTEGRATION_TIME_VALUES = {
53
+ INTEGRATION_TIME_40MS: 0x00,
54
+ INTEGRATION_TIME_80MS: 0x10,
55
+ INTEGRATION_TIME_160MS: 0x20,
56
+ INTEGRATION_TIME_320MS: 0x30,
57
+ INTEGRATION_TIME_640MS: 0x40,
58
+ INTEGRATION_TIME_1280MS: 0x50,
59
+ }
60
+
61
+ _INTEGRATION_TIME_DELAY = {
62
+ INTEGRATION_TIME_40MS: 0.05,
63
+ INTEGRATION_TIME_80MS: 0.09,
64
+ INTEGRATION_TIME_160MS: 0.17,
65
+ INTEGRATION_TIME_320MS: 0.33,
66
+ INTEGRATION_TIME_640MS: 0.65,
67
+ INTEGRATION_TIME_1280MS: 1.29,
68
+ }
69
+
70
+ BIT_SD = 0x01
71
+ BIT_AF = 0x02
72
+ BIT_TRIG = 0x04
73
+ MASK_INTEGRATION_TIME = 0x70
74
+
75
+ def __init__(self, i2c: busio.I2C, integration_time=INTEGRATION_TIME_160MS, i2c_address = 0x10):
39
76
  """
40
77
  initialize colour sensor
41
78
  i2c_address - i2c_address of colour sensor (default 0x10)
42
79
  """
43
80
  self.address = i2c_address
44
- self.bus = smbus.SMBus(1)
81
+ if integration_time not in self._INTEGRATION_TIME_VALUES:
82
+ raise ValueError("integration_time is not in range")
83
+ self.integration_time = integration_time
84
+ self.device = i2c_device.I2CDevice(i2c, self.address)
45
85
  self.enableSensor()
46
86
 
47
87
  def write(self, cmd, val):
@@ -50,74 +90,143 @@ class colour:
50
90
  cmd - register
51
91
  val - value to be written
52
92
  """
53
- self.bus.write_word_data(self.address, cmd, val)
93
+ data = bytes([
94
+ cmd & 0xFF,
95
+ val & 0xFF,
96
+ (val >> 8) & 0xFF,
97
+ ])
98
+ with self.device as bus_device:
99
+ bus_device.write(data)
54
100
 
55
101
  def read(self, cmd):
56
102
  """
57
103
  read value from register on the colour sensor
58
104
  cmd - register
59
105
  """
60
- return self.bus.read_word_data(self.address, cmd)
61
-
106
+ register = bytes([cmd & 0xFF])
107
+ data = bytearray(2)
108
+ last_error = None
109
+ for _ in range(3):
110
+ try:
111
+ with self.device as bus_device:
112
+ bus_device.write_then_readinto(register, data)
113
+ return data[0] | (data[1] << 8)
114
+ except OSError as error:
115
+ last_error = error
116
+ time.sleep(0.02)
117
+ raise last_error
118
+
62
119
  def enableSensor(self):
63
120
  """
64
121
  activate Sensor
65
122
  """
66
- conf = self.read(0x00) & 0x00FE
67
- self.write(0x00, conf)
123
+ config = self._INTEGRATION_TIME_VALUES[self.integration_time]
124
+ config &= ~self.BIT_SD
125
+ config &= ~self.BIT_AF
126
+ config &= ~self.BIT_TRIG
127
+ self.write(self.REG_CONF, config)
128
+ time.sleep(self._INTEGRATION_TIME_DELAY[self.integration_time])
68
129
 
69
130
  def disableSensor(self):
70
131
  """
71
132
  deactivate Sensor
72
133
  """
73
- conf = (self.read(0x00) & 0x00FE) | 0x000
74
- self.write(0x00, conf)
75
- time.sleep(.001)
134
+ config = self.read(self.REG_CONF)
135
+ config |= self.BIT_SD
136
+ self.write(self.REG_CONF, config)
137
+
138
+ def setIntegrationTime(self, integration_time):
139
+ """
140
+ set integration time with variable integration_time
141
+ integration_time - _INTEGRATION_TIME_VALUES
142
+ INTEGRATION_TIME_40MS = 40 ms
143
+ INTEGRATION_TIME_80MS = 80 ms
144
+ INTEGRATION_TIME_160MS = 160 ms
145
+ INTEGRATION_TIME_320MS = 320 ms
146
+ INTEGRATION_TIME_640MS = 640 ms
147
+ INTEGRATION_TIME_1280MS = 1280 ms
148
+ """
149
+ if integration_time not in self._INTEGRATION_TIME_VALUES:
150
+ raise ValueError("integration_time is not in range")
151
+ config = self.read(self.REG_CONF)
152
+ config &= ~self.MASK_INTEGRATION_TIME
153
+ config |= self._INTEGRATION_TIME_VALUES[integration_time]
154
+ config &= ~self.BIT_SD
155
+ self.integration_time = integration_time
156
+ self.write(self.REG_CONF, config)
157
+ time.sleep(self._INTEGRATION_TIME_DELAY[self.integration_time])
158
+
159
+ def getRed(self):
160
+ """
161
+ returns measured value for colour red
162
+ """
163
+ return self.read(self.REG_RED)
164
+
165
+ def getBlue(self):
166
+ """
167
+ returns measured value for colour blue
168
+ """
169
+ return self.read(self.REG_BLUE)
170
+
171
+ def getGreen(self):
172
+ """
173
+ returns measured value for colour green
174
+ """
175
+ return self.read(self.REG_GREEN)
176
+
177
+ def getWhite(self):
178
+ """
179
+ returns measured value for colour white
180
+ """
181
+ return self.read(self.REG_WHITE)
76
182
 
77
183
  def getRGBW(self):
78
184
  """
79
185
  returns colour values from colour sensor - red, green, blue, white
80
186
  """
81
- return self.read(0x08), self.read(0x09), int(self.read(0x0A) * 1.5), self.read(0x0B)
82
-
83
- def setIntegrationTime(self, int_time):
84
- """
85
- set integration time with variable int_time
86
- int_time - integration time
87
- 0 = 40 ms
88
- 1 = 80 ms
89
- 2 = 160 ms
90
- 3 = 320 ms
91
- 4 = 640 ms
92
- 5 = 1280 ms
93
- """
94
- if int_time < 0 or int_time > 5:
95
- raise ValueError('int_time is not in range')
96
- conf = self.read(0x00) & 0x0003
97
- self.write(0x00, (conf | (int_time << 4)))
98
-
187
+ red = self.getRed()
188
+ time.sleep(0.005)
189
+ green = self.getGreen()
190
+ time.sleep(0.005)
191
+ blue = self.getBlue()
192
+ time.sleep(0.005)
193
+ white = self.getWhite()
194
+ return red, green, blue, white
195
+
99
196
  def forceMode(self):
100
197
  """
101
198
  forces measurement mode - triggers to start
102
199
  """
103
- conf = (self.read(0x00) & 0x0072) | 0x0002
104
- self.write(0x00, conf)
200
+ config = self.read(self.REG_CONF)
201
+ config &= self.MASK_INTEGRATION_TIME
202
+ config |= self.BIT_AF
203
+ config |= self.BIT_TRIG
204
+ config &= ~self.BIT_SD
205
+ self.write(self.REG_CONF, config)
206
+ time.sleep(self._INTEGRATION_TIME_DELAY[self.integration_time])
105
207
 
106
208
  def autoMode(self):
107
209
  """
108
210
  automatic measurement mode
109
211
  """
110
- conf = self.read(0x00) & 0x0070
111
- self.write(0x00, conf)
212
+ config = self.read(self.REG_CONF)
213
+ config &= self.MASK_INTEGRATION_TIME
214
+ config &= ~self.BIT_AF
215
+ config &= ~self.BIT_TRIG
216
+ config &= ~self.BIT_SD
217
+ self.write(self.REG_CONF, config)
218
+ time.sleep(self._INTEGRATION_TIME_DELAY[self.integration_time])
112
219
 
113
220
  def readAll(self):
114
221
  """
115
222
  returns most recognized colour and raw values
116
223
  """
117
- r, g, b, w = self.getRGBW()
118
- raw_val = [r, g, b, w]
119
- dominant_col, dominant_val = "red", r
120
- if g > dominant_val: dominant_col, dominant_val = "green", g
121
- if b > dominant_val: dominant_col, dominant_val = "blue", b
122
-
123
- return dominant_col, raw_val
224
+ red, green, blue, white = self.getRGBW()
225
+ values = {
226
+ "red": red,
227
+ "green": green,
228
+ "blue": blue,
229
+ }
230
+
231
+ dominant_color = max(values, key=values.get)
232
+ return dominant_color, [red, green, blue, white]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: JoyPi_Advanced_RaspberryPi
3
- Version: 2.0.0
3
+ Version: 2.0.1
4
4
  Summary: This library enables usage of some sensors and actors of the Joy-Pi Advanced on the Raspberry Pi.
5
5
  Project-URL: Homepage, https://github.com/joy-it/JoyPi_Advanced_RaspberryPi
6
6
  Project-URL: Issues, https://github.com/joy-it/JoyPi_Advanced_RaspberryPi/issues
@@ -36,7 +36,7 @@ This library has some dependencies which are used to control the modules of the
36
36
  - [`Adafruit_CircuitPython_MCP230xx`](https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx) - for the button matrix - with `pip install adafruit-circuitpython-mcp230xx`
37
37
 
38
38
  ## Installation
39
- ```
39
+ ```bash
40
40
  pip install JoyPi_Advanced_RaspberryPi
41
41
  ```
42
42
  ## Library Guide
@@ -3,11 +3,11 @@ JoyPi_Advanced_RaspberryPi/__init__.py,sha256=f3EEO5VotY0d15Cr35XwMq7iQ1SSwYId-g
3
3
  JoyPi_Advanced_RaspberryPi/adc.py,sha256=uD8kx5AQzXrzkqTWWQsznt6Tmp--rxhD1XrmmlfizoQ,2851
4
4
  JoyPi_Advanced_RaspberryPi/barometer.py,sha256=Pvhtg77Fi9LMMIrnrmDk9MBdnho93kzy574HNbbt698,3503
5
5
  JoyPi_Advanced_RaspberryPi/buttonmatrix.py,sha256=REaGy0q2C0iYvoDr2WidsITNT67dp49il6u9eVNkSCY,4369
6
- JoyPi_Advanced_RaspberryPi/colour.py,sha256=ExkvKWuBeBnu344YoJhFYRoDhq1hK0wMzKkZkJ7WZBM,3987
6
+ JoyPi_Advanced_RaspberryPi/colour.py,sha256=lQbBE5dASpuz6TQVqJX8uIYBowYPOD1opcnIKJrN0uE,7575
7
7
  JoyPi_Advanced_RaspberryPi/gyroscope.py,sha256=pP6V2upAqPfajaKcJBPrgQSSpUFVUSDId0k8uli7abw,3516
8
8
  JoyPi_Advanced_RaspberryPi/servomotor.py,sha256=H-soLiQqiyvR6gNu0LffV19QIBWAiokwg2Kc9WYzblM,3169
9
9
  JoyPi_Advanced_RaspberryPi/stepmotor.py,sha256=YpggJICaSLzTvSDpGDJK8kC2VDP6aM__I-phwGiP6dA,2957
10
- joypi_advanced_raspberrypi-2.0.0.dist-info/METADATA,sha256=GyP76-xTNSkhb9f-z66YJks5wykuM1Dsy44vuJBZkXc,5393
11
- joypi_advanced_raspberrypi-2.0.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
12
- joypi_advanced_raspberrypi-2.0.0.dist-info/licenses/LICENSE,sha256=IwGE9guuL-ryRPEKi6wFPI_zOhg7zDZbTYuHbSt_SAk,35823
13
- joypi_advanced_raspberrypi-2.0.0.dist-info/RECORD,,
10
+ joypi_advanced_raspberrypi-2.0.1.dist-info/METADATA,sha256=vUNsWAO45Urjoq2L0w0T22o5OqkYZRoobEzglbx-p0M,5397
11
+ joypi_advanced_raspberrypi-2.0.1.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
12
+ joypi_advanced_raspberrypi-2.0.1.dist-info/licenses/LICENSE,sha256=IwGE9guuL-ryRPEKi6wFPI_zOhg7zDZbTYuHbSt_SAk,35823
13
+ joypi_advanced_raspberrypi-2.0.1.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.29.0
2
+ Generator: hatchling 1.30.1
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any