Skip to content

Commit a1f1c01

Browse files
authored
Merge pull request #3 from freaf87/master
buzzer, mcp3008, ultrasonic, pca9685 and rgb interfaces added
2 parents c83d48e + 8c48b06 commit a1f1c01

5 files changed

Lines changed: 144 additions & 10 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# This file is part of FSE 2017.
4+
#
5+
# FSE 2017 is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# FSE 2017 is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with FSE 2017. If not, see <http://www.gnu.org/licenses/>.
17+
18+
"""Driver for a BUZZER."""
19+
20+
import time
21+
import wiringpi
22+
from gpio_manager import GPIO_Manager
23+
from time import sleep
24+
25+
class BUZZER(GPIO_Manager):
26+
"""Driver for a BUZZER connected by GPIO."""
27+
28+
_pin = 17
29+
_pins = [_pin]
30+
31+
def __init__(self):
32+
super(BUZZER, self).__init__()
33+
wiringpi.pinMode(self._pin, wiringpi.OUTPUT)
34+
35+
def buzzerHigh(self):
36+
wiringpi.digitalWrite(self._pin, wiringpi.OUTPUT)
37+
38+
def buzzerLow(self):
39+
wiringpi.digitalWrite(self._pin, wiringpi.INPUT)
40+
41+
def buzzForTime(self, time):
42+
self.buzzerHigh()
43+
sleep(time)
44+
self.buzzerLow()
45+
sleep(time)
46+
47+
if __name__ == "__main__":
48+
with BUZZER() as buzzer:
49+
while True:
50+
for i in list(range(40,0,-1)):
51+
print ("distance = {0} cm".format(i))
52+
if i <= 30 and i >= 1:
53+
buzzer.buzzForTime(i*0.02)
54+
sleep(0.1)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# This file is part of FSE 2018.
4+
#
5+
# FSE 2018 is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# FSE 2018 is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with FSE 2018. If not, see <http://www.gnu.org/licenses/>.
17+
18+
"""Interface to Analog to Digital Converters."""
19+
20+
import time
21+
import spidev
22+
23+
24+
class AnalogToDigitalConverter():
25+
"""Class to represent MCP3008 analog to digital Converter"""
26+
# Voltage dividers 1kOhm/1kOhm (channel 0-3) - 22kOhm/10kOhm(channel 4-7)
27+
_facCh0123 = 2
28+
_facCh4567 = 3.195
29+
# Bytes for building read commands
30+
start_byte = 0x01
31+
channel_modifier = 0x08
32+
end_byte = 0x00
33+
34+
def __init__(self):
35+
self._spi = spidev.SpiDev()
36+
self._spi.open(0, 0)
37+
self._spi.max_speed_hz = 5000000
38+
39+
def _build_read_command(self, channel):
40+
"""
41+
Produce 3-byte read command.
42+
The command is 1 byte, book-ended by start and end signifiers.
43+
"""
44+
return [self.start_byte, (self.channel_modifier + channel) << 4,
45+
self.end_byte]
46+
47+
def _process_adc_value(self, channel, value):
48+
"""Return result of processing analog to digital converter value."""
49+
if channel <= 3:
50+
coefficient = self._facCh0123
51+
else:
52+
coefficient = self._facCh4567
53+
return (((value[1] & 3) << 8) + value[2]) * 0.00322 * coefficient
54+
55+
def read_adc(self, channel):
56+
"""Read ADC channel."""
57+
assert 0 <= channel <= 7, "ADC number must be a value of 0-7!"
58+
r = self._spi.xfer2(self._build_read_command(channel))
59+
return self._process_adc_value(channel, r)
60+
61+
def __enter__(self):
62+
return self
63+
64+
def __exit__(self, *args):
65+
self._spi.close()
66+
67+
68+
if __name__ == '__main__':
69+
with AnalogToDigitalConverter() as adc:
70+
print('Reading MCP3008 values, press Ctrl-C to quit...')
71+
print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} |{7:>4} |'.format(*range(8)))
72+
print('-' * 57)
73+
74+
while True:
75+
values = [0]*8
76+
for i in range(8):
77+
values[i] = round(adc.read_adc(i),2)
78+
79+
print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*values))
80+
time.sleep(0.5)

02_Code/PDC_python/drivers/rgbLed.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@
1515
# You should have received a copy of the GNU General Public License
1616

1717

18-
from gpio_manager import GPIO_Manager
1918
from PCA9685 import PWM_Driver
2019
import os
2120
import time
2221
import sys
23-
import logging
2422

25-
logger = logging.getLogger(__name__)
26-
27-
class RGB_LED(GPIO_Manager):
23+
class RGB_LED():
2824
""" Interface class for FSEDevBoard onboard LED """
2925
def __init__(self):
30-
super(RGB_LED, self).__init__()
3126
self._RChannel = 13
3227
self._GChannel = 14
3328
self._BChannel = 15
@@ -47,10 +42,18 @@ def setRGBDutycycles(self, dutyCycleRChannel, dutyCycleGChannel, dutyCycleBChann
4742
self.setGChannelPWM(dutyCycleGChannel)
4843
self.setBChannelPWM(dutyCycleBChannel)
4944

45+
def __enter__(self):
46+
return self
47+
48+
def __exit__(self, *args):
49+
rgbLed.setRChannelPWM(0)
50+
rgbLed.setGChannelPWM(0)
51+
rgbLed.setBChannelPWM(0)
52+
5053
if __name__ == "__main__":
5154
with RGB_LED() as rgbLed:
5255
try:
53-
rgbLed.pwmDriver.setPwmFreq(60)
56+
rgbLed.pwmDriver.setPwmFreq(600)
5457
while True:
5558
rgbLed.setRGBDutycycles(2, 0, 0)
5659
time.sleep(0.5)
@@ -66,6 +69,3 @@ def setRGBDutycycles(self, dutyCycleRChannel, dutyCycleGChannel, dutyCycleBChann
6669
except:
6770
print("Unexpected error:", sys.exc_info()[0])
6871
raise
69-
finally:
70-
rgbLed.pwmDriver.setAllPwm(0,0)
71-
File renamed without changes.

0 commit comments

Comments
 (0)