-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasehat.py
732 lines (569 loc) · 23.7 KB
/
basehat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
from __future__ import division
from __future__ import print_function
# UltrasonicSensor.py
# Created by Noah Grzegorek on behalf of the ENGR 16X Teaching Team
### DO NOT MODIFY CODE IN THIS FILE ###
# This is the code for Grove - Ultrasonic Ranger
# (https://www.seeedstudio.com/Grove-Ultrasonic-Ranger-p-960.html)
# which is a non-contact distance measurement module which works with 40KHz sound wave.
#
# This is the library for Grove Base Hat which used to connect grove sensors for raspberry pi.
#
'''
## License
The MIT License (MIT)
Grove Base Hat for the Raspberry Pi, used to connect grove sensors.
Copyright (C) 2018 Seeed Technology Co.,Ltd.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
import sys
import time
from grove.gpio import GPIO
# This usleep is to have used to have the sensor sleep for a certain amount of microseconds
usleep = lambda x: time.sleep(x / 1000000.0)
# These global variables are used as part of the distance calculations the sensor performs
_TIMEOUT1 = 1000
_TIMEOUT2 = 10000
class UltrasonicSensor(object):
def __init__(self, pin):
self.dio = GPIO(pin)
# This function calculates the distance that the nearest object it can detect is from the sensor in centimeters
@property
def getDist(self):
self.dio.dir(GPIO.OUT)
self.dio.write(0)
usleep(2)
self.dio.write(1)
usleep(10)
self.dio.write(0)
self.dio.dir(GPIO.IN)
t0 = time.time()
count = 0
while count < _TIMEOUT1:
if self.dio.read():
break
count += 1
if count >= _TIMEOUT1:
return None
t1 = time.time()
count = 0
while count < _TIMEOUT2:
if not self.dio.read():
break
count += 1
if count >= _TIMEOUT2:
return None
t2 = time.time()
dt = int((t1 - t0) * 1000000)
if dt > 530:
return None
distance = ((t2 - t1) * 1000000 / 29 / 2) # cm
return distance
# LineFinder.py
# Created by Noah Grzegorek on behalf of the ENGR 16X Teaching Team
### DO NOT MODIFY CODE IN THIS FILE ###
from gpiozero import SmoothedInputDevice
class LineFinder(SmoothedInputDevice):
"""
Extends :class:`SmoothedInputDevice` and represents a single pin line
sensor like the TCRT5000 infra-red proximity sensor found in the `CamJam #3
EduKit`_.
A typical line sensor has a small circuit board with three pins: VCC, GND,
and OUT. VCC should be connected to a 3V3 pin, GND to one of the ground
pins, and finally OUT to the GPIO specified as the value of the *pin*
parameter in the constructor.
The following code will print a line of text indicating when the sensor
detects a line, or stops detecting a line::
from gpiozero import LineSensor
from signal import pause
sensor = LineSensor(4)
sensor.when_line = lambda: print('Line detected')
sensor.when_no_line = lambda: print('No line detected')
pause()
:type pin: int or str
:param pin:
The GPIO pin which the sensor is connected to. See :ref:`pin-numbering`
for valid pin numbers. If this is :data:`None` a :exc:`GPIODeviceError`
will be raised.
:type pull_up: bool or None
:param pull_up:
See description under :class:`InputDevice` for more information.
:type active_state: bool or None
:param active_state:
See description under :class:`InputDevice` for more information.
:param int queue_len:
The length of the queue used to store values read from the sensor. This
defaults to 5.
:param float sample_rate:
The number of values to read from the device (and append to the
internal queue) per second. Defaults to 100.
:param float threshold:
Defaults to 0.5. When the average of all values in the internal queue
rises above this value, the sensor will be considered "active" by the
:attr:`~SmoothedInputDevice.is_active` property, and all appropriate
events will be fired.
:param bool partial:
When :data:`False` (the default), the object will not return a value
for :attr:`~SmoothedInputDevice.is_active` until the internal queue has
filled with values. Only set this to :data:`True` if you require
values immediately after object construction.
:type pin_factory: Factory or None
:param pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _CamJam #3 EduKit: http://camjam.me/?page_id=1035
"""
def __init__(self, pin=None, *, pull_up=False, active_state=None,
queue_len=1, sample_rate=100, threshold=0.5, partial=False,
pin_factory=None):
super().__init__(
pin, pull_up=pull_up, active_state=active_state,
threshold=threshold, queue_len=queue_len,
sample_wait=1 / sample_rate, partial=partial,
pin_factory=pin_factory)
self._queue.start()
# Gets the current reading whether there it detects black or white (0 or 1)
@property
def value(self):
"""
Returns a value representing the average of the queued values. This
is 0 for black under the sensor, and 1 for white under
the sensor.
"""
return super().value
@property
def line_detected(self):
return not self.is_active
# LightSensor.py
# Created by Noah Grzegorek on behalf of the ENGR 16X Teaching Team
### DO NOT MODIFY CODE IN THIS FILE ###
# -*- coding: utf-8 -*-
#
# The MIT License (MIT)
#
# Grove Base Hat for the Raspberry Pi, used to connect grove sensors.
# Copyright (C) 2018 Seeed Technology Co.,Ltd.
'''
This is the code for
- `Grove - Light Sensor <https://www.seeedstudio.com/Grove-Light-Sensor-v1.2-p-2727.html>`_
Examples:
.. code-block:: python
import time
from grove.grove_light_sensor import GroveLightSensor
# connect to alalog pin 2(slot A2)
PIN = 2
sensor = GroveLightSensor(pin)
print('Detecting light...')
while True:
print('Light value: {0}'.format(sensor.light))
time.sleep(1)
'''
import time, sys, math
from grove.adc import ADC
class LightSensor(object):
'''
Grove Light Sensor class
Args:
pin(int): number of analog pin/channel the sensor connected.
'''
def __init__(self, channel):
self.channel = channel
self.adc = ADC()
# Gets the current light strength percentage (0% or 100%)
@property
def light(self):
'''
Get the light strength value, maximum value is 100.0%
Returns:
(int): ratio, 0(0.0%) - 1000(100.0%)
'''
value = self.adc.read(self.channel)
return value
# IRSensor.py
# Created by Seth McConkey on behalf of the ENGR 16X Teaching Team
### DO NOT MODIFY CODE IN THIS FILE ###
'''
IR Sensor:
Description:
This sensor has two values that can be accessed: a value from 0 to 999 that is
proportional to the amount of infrared rays the left sensor is reading, and a value
from 0 to 999 that is proportional to the amount of infrared rays the left sensor is
reading. It is used to determine presence/distance of an infrared source, such as an
LED light or Infrared Beacon.
Hardware:
Connect this sensor to any analog port on the Grove BaseHAT: A0, A2, A4, or A6.
Initialize the sensor using only the number of the port, DO NOT include the A.
More info:
https://wiki.seeedstudio.com/Grove_Base_Hat_for_Raspberry_Pi/
https://wiki.seeedstudio.com/Grove-Infrared_Reflective_Sensor/
Initialization:
sensor_name = IRSensor(pin, pin + 1)
If you plug your IR Sensor into A0 and want to name it 'IR', your initialization will
look like this:
IR = IRSensor(0, 1)
IRSensor.value1:
Read the value from the sensor labeled sensor1. If your sensor's name is 'IR', accessing the left
value will look like this:
value_from_sensor1 = IR.value1
IRSensor.value2:
Read the value from the sensor labeled sensor2. If your sensor's name is 'IR', accessing the right
value will look like this:
value_from_sensor2 = IR.value2
IRSensor_Example.py:
Usage of this code is demonstrated in the example file for this sensor in the Examples
folder on your Pi's desktop.
'''
### DO NOT MODIFY CODE BELOW THIS LINE ###
# original code for comparison:
# adapted from light sensor code:
# https://github.com/Seeed-Studio/grove.py/blob/master/grove/grove_light_sensor_v1_2.py
from grove.adc import ADC
class IRSensor(object):
'''
IR Sensor class
Args:
pin(int): number of the analog pin the sensor is connected to.
'''
def __init__(self, channel1, channel2):
self.channel1 = channel1
self.channel2 = channel2
self.adc = ADC()
@property
def value1(self):
'''
Get the infrared strength value, maximum value is 999
Returns:
(int): ratio, 0 - 999
'''
value = self.adc.read(self.channel1)
return value
@property
def value2(self):
'''
Get the infrared strength value, maximum value is 999
Returns:
(int): ratio, 0 - 999
'''
value = self.adc.read(self.channel2)
return value
# IMUSensor.py
# Created by Noah Grzegorek on behalf of the ENGR 16X Teaching Team
### DO NOT MODIFY CODE IN THIS FILE ###
# This is the code for Grove - IMU 9DOF (ICM20600+AK09918).
# (https://www.seeedstudio.com/Grove-IMU-9DOF-ICM20600-AK0991-p-3157.html)
# which is 9 Degrees of Freedom IMU (Inertial measurement unit) with
# gyroscope, accelerometer and electronic compass, implemented by
# two chips LCM20600 and AK09918.
#
# Author: Peter Yang <[email protected]>
#
# Grove.py is the library for Grove Base Hat which used to
# connect grove sensors for raspberry pi.
#
'''
## License
The MIT License (MIT)
Grove Base Hat for the Raspberry Pi, used to connect grove sensors.
Copyright (C) 2018 Seeed Technology Co.,Ltd.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
### ENGR 161X STUDENTS IGNORE THIS CODE ###
from ctypes import *
from ctypes import util
import sys
from grove.i2c import Bus
ICM20600_I2C_ADDR0 = 0x68
ICM20600_I2C_ADDR1 = 0x69
ICM20600_RANGE_250_DPS, \
ICM20600_RANGE_500_DPS, \
ICM20600_RANGE_1K_DPS, \
ICM20600_RANGE_2K_DPS = 0, 1, 2, 3
ICM20600_RANGE_2G, \
ICM20600_RANGE_4G, \
ICM20600_RANGE_8G, \
ICM20600_RANGE_16G = 0, 1, 2, 3
ICM20600_GYRO_RATE_8K_BW_3281, \
ICM20600_GYRO_RATE_8K_BW_250, \
ICM20600_GYRO_RATE_1K_BW_176, \
ICM20600_GYRO_RATE_1K_BW_92, \
ICM20600_GYRO_RATE_1K_BW_41, \
ICM20600_GYRO_RATE_1K_BW_20, \
ICM20600_GYRO_RATE_1K_BW_10, \
ICM20600_GYRO_RATE_1K_BW_5 = 0, 1, 2, 3, 4, 5, 6, 7
ICM20600_ACC_RATE_4K_BW_1046,\
ICM20600_ACC_RATE_1K_BW_420, \
ICM20600_ACC_RATE_1K_BW_218, \
ICM20600_ACC_RATE_1K_BW_99, \
ICM20600_ACC_RATE_1K_BW_44, \
ICM20600_ACC_RATE_1K_BW_21, \
ICM20600_ACC_RATE_1K_BW_10, \
ICM20600_ACC_RATE_1K_BW_5 = 0, 1, 2, 3, 4, 5, 6, 7
ICM20600_ACC_AVERAGE_4, \
ICM20600_ACC_AVERAGE_8, \
ICM20600_ACC_AVERAGE_16,\
ICM20600_ACC_AVERAGE_32 = 0, 1, 2, 3
ICM20600_GYRO_AVERAGE_1, \
ICM20600_GYRO_AVERAGE_2, \
ICM20600_GYRO_AVERAGE_4, \
ICM20600_GYRO_AVERAGE_8, \
ICM20600_GYRO_AVERAGE_16, \
ICM20600_GYRO_AVERAGE_32, \
ICM20600_GYRO_AVERAGE_64, \
ICM20600_GYRO_AVERAGE_128 = 0, 1, 2, 3, 4, 5, 6, 7
ICM20600_ICM_SLEEP_MODE, \
ICM20600_ICM_STANDYBY_MODE, \
ICM20600_ICM_ACC_LOW_POWER, \
ICM20600_ICM_ACC_LOW_NOISE, \
ICM20600_ICM_GYRO_LOW_POWER, \
ICM20600_ICM_GYRO_LOW_NOISE, \
ICM20600_ICM_6AXIS_LOW_POWER,\
ICM20600_ICM_6AXIS_LOW_NOISE = 0, 1, 2, 3, 4, 5, 6, 7
__c_module = "akicm" # doesnt work
try:
_ = util.find_library(__c_module) # doesnt work
# print(_)
_akicm = cdll.LoadLibrary("libakicm.so")
# print(_akicm)
except Exception:
print("Error: module lib{}.so unusable, please install lib{}".
format(__c_module, __c_module))
sys.exit(1)
class ICM20600Cfg(Structure):
_fields_ = [("gyro_range", c_uint16), \
("gyro_rate", c_uint16), \
("gyro_aver", c_uint16), \
("acc_range", c_uint16), \
("acc_rate", c_uint16), \
("acc_aver", c_uint16), \
("power", c_uint16), \
("divider", c_uint16) ]
class GroveIMU9DOFICM20600(object):
def __init__(self, addr = ICM20600_I2C_ADDR1):
self._dev = _akicm.rpi_icm20600_alloc()
dev_path = "/dev/i2c-{}".format(Bus().bus)
icm20600_cfg = ICM20600Cfg(ICM20600_RANGE_2K_DPS,
ICM20600_GYRO_RATE_1K_BW_176,
ICM20600_GYRO_AVERAGE_1,
ICM20600_RANGE_16G,
ICM20600_ACC_RATE_1K_BW_420,
ICM20600_ACC_AVERAGE_4,
ICM20600_ICM_6AXIS_LOW_POWER,
0)
dev_path = dev_path.encode('utf-8') # SEED STUDIO SHOULD HIRE SETH MCCONKEY
_akicm.rpi_icm20600_init(self._dev,
dev_path,
addr,
byref(icm20600_cfg))
def __del__(self):
_akicm.rpi_icm20600_free(self._dev)
def get_temperature(self):
t = c_double()
_akicm.rpi_icm20600_get_temperature(self._dev, byref(t))
return t.value
def get_accel(self):
x, y, z = c_double(), c_double(), c_double()
_akicm.rpi_icm20600_get_accel(self._dev,
byref(x), byref(y), byref(z))
return x.value, y.value, z.value
def get_gyro(self):
x, y, z = c_double(), c_double(), c_double()
_akicm.rpi_icm20600_get_gyro(self._dev,
byref(x), byref(y), byref(z))
return x.value, y.value, z.value
temperature = get_temperature
AK09918_I2C_ADDR = 0x0C
AK09918_POWER_DOWN = 0x00
AK09918_NORMAL = 0x01
AK09918_CONTINUOUS_10HZ = 0x02
AK09918_CONTINUOUS_20HZ = 0x04
AK09918_CONTINUOUS_50HZ = 0x06
AK09918_CONTINUOUS_100HZ = 0x08
AK09918_SELF_TEST = 0x10
AK09918_ERR_OK = 0 # OK
AK09918_ERR_DOR = 1 # data skipped
AK09918_ERR_NOT_RDY = 2 # not ready
AK09918_ERR_TIMEOUT = 3 # read/write timeout
AK09918_ERR_SELFTEST_FAILED = 4 # self test failed
AK09918_ERR_OVERFLOW = 5 # sensor overflow, means |x|+|y|+|z| >= 4912uT
AK09918_ERR_WRITE_FAILED = 6 # fail to write
AK09918_ERR_READ_FAILED = 7 # fail to read
class GroveIMU9DOFAK09918(object):
def __init__(self, addr = AK09918_I2C_ADDR):
self._dev = _akicm.rpi_ak09918_alloc()
dev_path = "/dev/i2c-{}".format(Bus().bus)
dev_path = dev_path.encode('utf-8') # SEED STUDIO SHOULD HIRE SETH MCCONKEY
_akicm.rpi_ak09918_init(self._dev,
dev_path,
addr,
AK09918_NORMAL)
def __del__(self):
_akicm.rpi_ak09918_free(self._dev)
def mode(self, mode = None):
if not mode is None:
_akicm.rpi_ak09918_set_mode(self._dev, mode)
return _akicm.rpi_ak09918_get_mode(self._dev)
def reset(self):
return _akicm.rpi_ak09918_reset(self._dev)
def is_ready(self):
if _akicm.rpi_ak09918_is_ready(self._dev) == AK09918_ERR_OK:
return True
return False
def is_skip(self):
r = _akicm.rpi_ak09918_is_skip(self._dev)
return (r == AK09918_ERR_DOR)
def get_magnet(self):
x, y, z = c_double(), c_double(), c_double()
_akicm.rpi_ak09918_read(self._dev,
byref(x), byref(y), byref(z))
return x.value, y.value, z.value
def get_magnet_raw(self):
x, y, z = c_double(), c_double(), c_double()
_akicm.rpi_ak09918_read_raw(self._dev,
byref(x), byref(y), byref(z))
return x.value, y.value, z.value
def err_string(self, errval):
return _akicm.rpi_ak09918_err_string(errval)
### END OF CODE TO IGNORE FOR 161X STUDENTS ###
### IMPORTANT CODE FOR ENGR 161X STUDENTS ###
# Class combining all sensor functionality into one class for ease of use
class IMUSensor(object):
# This sensor utilizes two different chips and they are initialized here so all functions can be called easily
def __init__(self):
self.icmChip = GroveIMU9DOFICM20600()
self.akChip = GroveIMU9DOFAK09918()
self.akChip.mode(AK09918_CONTINUOUS_100HZ)
# Function returns a three dimensional vector of the respective x, y, and z acceleration values (Mega-Goombas)
def getAccel(self):
return (tuple(x / 100 for x in self.icmChip.get_accel()))
# Function returns a three dimensional vector of the respective x, y, and z gyroscope values (Degrees/Second)
def getGyro(self):
return self.icmChip.get_gyro()
# Function returns a three dimensional vector of the respective x, y, and z magnetic values (Micro-Teslas)
def getMag(self):
return self.akChip.get_magnet()
# for Button, follow this code:
# https://github.com/Seeed-Studio/grove.py/blob/master/grove/grove_button.py
# For most of these, you will simply just need to rename the class to make the name simpler.
# You may change property namees/functions to be more consistent or easier to remember, but
# that is not 100% necessary all the time.
# If you are confused on how the class works, consider changing it to be more intuitive
# so it is also easier for students
# Created by Aayush Iyengar on behalf on the ENGR 16X Teaching Team
from gpiozero import DigitalInputDevice
from gpiozero import HoldMixin
class Button(HoldMixin, DigitalInputDevice):
"""
Extends :class:`DigitalInputDevice` and represents a simple push button
or switch.
Connect one side of the button to a ground pin, and the other to any GPIO
pin. Alternatively, connect one side of the button to the 3V3 pin, and the
other to any GPIO pin, then set *pull_up* to :data:`False` in the
:class:`Button` constructor.
The following example will print a line of text when the button is pushed::
from gpiozero import Button
button = Button(4)
button.wait_for_press()
print("The button was pressed!")
:type pin: int or str
:param pin:
The GPIO pin which the button is connected to. See :ref:`pin-numbering`
for valid pin numbers. If this is :data:`None` a :exc:`GPIODeviceError`
will be raised.
:type pull_up: bool or None
:param pull_up:
If :data:`True` (the default), the GPIO pin will be pulled high by
default. In this case, connect the other side of the button to ground.
If :data:`False`, the GPIO pin will be pulled low by default. In this
case, connect the other side of the button to 3V3. If :data:`None`, the
pin will be floating, so it must be externally pulled up or down and
the ``active_state`` parameter must be set accordingly.
:type active_state: bool or None
:param active_state:
See description under :class:`InputDevice` for more information.
:type bounce_time: float or None
:param bounce_time:
If :data:`None` (the default), no software bounce compensation will be
performed. Otherwise, this is the length of time (in seconds) that the
component will ignore changes in state after an initial change.
:param float hold_time:
The length of time (in seconds) to wait after the button is pushed,
until executing the :attr:`when_held` handler. Defaults to ``1``.
:param bool hold_repeat:
If :data:`True`, the :attr:`when_held` handler will be repeatedly
executed as long as the device remains active, every *hold_time*
seconds. If :data:`False` (the default) the :attr:`when_held` handler
will be only be executed once per hold.
:type pin_factory: Factory or None
:param pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
"""
def __init__(self, pin=None, *, pull_up=False, active_state=None,
bounce_time=None, hold_time=1, hold_repeat=False,
pin_factory=None):
super().__init__(
pin, pull_up=pull_up, active_state=active_state,
bounce_time=bounce_time, pin_factory=pin_factory)
self.hold_time = hold_time
self.hold_repeat = hold_repeat
@property
def value(self):
"""
Returns 1 if the button is currently pressed, and 0 if it is not.
"""
return super().value
Button.is_pressed = Button.is_active
Button.pressed_time = Button.active_time
Button.when_pressed = Button.when_activated
Button.when_released = Button.when_deactivated
Button.wait_for_press = Button.wait_for_active
Button.wait_for_release = Button.wait_for_inactive
'''
def run_button_example():
#Main function to run the button example
pin = 5 #Assuming pin 5 for the Grove Button
button = GroveButton(pin) #Create a GroveButton instance
def on_press(elapsed_time):
#Callback function for button press event
print('Button is pressed')
def on_release(elapsed_time):
#Callback function for button release event
print("Button is released, pressed for {0} seconds".format(round(elapsed_time, 6)))
#Assign the callback functions to the button instance
button.set_on_press_callback(on_press)
button.set_on_release_callback(on_release)
#Main loop to continuously check button state
while True:
time.sleep(1)
#Run the example when this script is executed
if __name__ == '__main__':
run_button_example()
'''