|
| 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) |
0 commit comments