diff --git a/adc.py b/adc.py index 7d7e265..ca62f3a 100644 --- a/adc.py +++ b/adc.py @@ -8,15 +8,15 @@ def __init__(self, miso, mosi, clk, cs): self._mosi = mosi self._clk = clk self._cs = cs - + @abstractmethod def get_resolution(self): pass - + @abstractmethod def setup_pins(self): pass - + @abstractmethod def read_value(self): pass \ No newline at end of file diff --git a/main.py b/main.py index b0f9e24..def5682 100644 --- a/main.py +++ b/main.py @@ -22,11 +22,11 @@ def change_icon(percent): if (DEBUGMSG == 1): print("Changed battery icon to " + percent + "%") - + out = check_output("ps aux | grep pngview | awk '{ print $2 }'", shell=True) nums = out.split('\n') i = 0 - + for num in nums: i += 1 if (i == 1): @@ -39,12 +39,12 @@ def change_led(led): if (led == "green"): GPIO.output(GOODVOLTPIN, GPIO.HIGH) GPIO.output(LOWVOLTPIN, GPIO.LOW) - + printf("Changed green LED to on, red LED to off") elif (led == "red"): GPIO.output(GOODVOLTPIN, GPIO.LOW) GPIO.output(LOWVOLTPIN, GPIO.HIGH) - + printf("Changed green LED to off, red LED to on") def end_process(signalnum = None, handler = None): @@ -53,7 +53,7 @@ def end_process(signalnum = None, handler = None): exit(0) status = 0 - + # prepare handlers for process exit signal.signal(signal.SIGTERM, end_process) signal.signal(signal.SIGINT, end_process) @@ -68,7 +68,7 @@ def end_process(signalnum = None, handler = None): else: print("Unknown ADC type " + str(ADC) + "! Please set ADC in config.py to one of the supported types.") exit(0) - + adc.setup_pins() SVOLT100 = VOLT100 * HIGHRESVAL / (LOWRESVAL + HIGHRESVAL) @@ -82,7 +82,7 @@ def end_process(signalnum = None, handler = None): ADC50 = SVOLT50 / (ADCVREF / adc.get_resolution()) ADC25 = SVOLT25 / (ADCVREF / adc.get_resolution()) ADC0 = SVOLT0 / (ADCVREF / adc.get_resolution()) - + if (DEBUGMSG == 1): print("Batteries 100% voltage: " + str(VOLT100)) print("Batteries 75% voltage: " + str(VOLT75)) @@ -117,13 +117,13 @@ def end_process(signalnum = None, handler = None): ret3 = adc.read_value() ret = (ret1 + ret2 + ret3) / 3 voltage = ((HIGHRESVAL + LOWRESVAL) * ret * (ADCVREF / adc.get_resolution())) / HIGHRESVAL - + if (CSVOUT == 1): print(str(count) + ";" + str(time.time()) + ";" + str(datetime.datetime.now()) + ";" + str(ret) + ";" + str(voltage)) - + if (DEBUGMSG == 1): print("ADC value: " + str(ret) + " (" + str(voltage) + " V)") - + if (ret < ADC0): if (status != 0): change_icon("0") @@ -140,7 +140,7 @@ def end_process(signalnum = None, handler = None): if (status != 50): change_led("green") change_icon("50") - + status = 50 elif (ret < ADC75): if (status != 75): diff --git a/mcp3001.py b/mcp3001.py index 5d96fea..c4254a1 100644 --- a/mcp3001.py +++ b/mcp3001.py @@ -7,11 +7,11 @@ class MCP3001(ADC): def setup_pins(self): GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) - + GPIO.setup(self._miso, GPIO.IN) GPIO.setup(self._clk, GPIO.OUT) GPIO.setup(self._cs, GPIO.OUT) - + def get_resolution(self): return 1024.0 @@ -23,20 +23,20 @@ def read_value(self): for i in range(3): GPIO.output(self._clk, True) GPIO.output(self._clk, False) - + # clock out data, MSB first adcout = 0 - + for i in range(10): GPIO.output(self._clk, True) - + adcout <<= 1 - + if (GPIO.input(self._miso)): adcout |= 0x1 GPIO.output(self._clk, False) - + GPIO.output(self._cs, True) - + return adcout diff --git a/mcp3008.py b/mcp3008.py index de36bd6..8bf792c 100644 --- a/mcp3008.py +++ b/mcp3008.py @@ -7,19 +7,19 @@ class MCP3008(ADC): def __init__(self, miso, mosi, clk, cs, channel): ADC.__init__(self, miso, mosi, clk, cs) self._channel = channel - + def setup_pins(self): GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) - + GPIO.setup(self._miso, GPIO.IN) GPIO.setup(self._mosi, GPIO.OUT) GPIO.setup(self._clk, GPIO.OUT) GPIO.setup(self._cs, GPIO.OUT) - + def get_resolution(self): return 1024.0 - + def read_value(self): if ((self._channel > 7) or (self._channel < 0)): return -1 @@ -31,7 +31,7 @@ def read_value(self): commandout = self._channel commandout |= 0x18 # start bit + single-ended bit commandout <<= 3 # we only need to send 5 bits here - + for i in range(5): if (commandout & 0x80): GPIO.output(self._mosi, True) @@ -43,18 +43,18 @@ def read_value(self): GPIO.output(self._clk, False) adcout = 0 - + # read in one empty bit, one null bit and 10 ADC bits for i in range(12): GPIO.output(self._clk, True) GPIO.output(self._clk, False) adcout <<= 1 - + if (GPIO.input(self._miso)): adcout |= 0x1 GPIO.output(self._cs, True) adcout /= 2 # first bit is 'null' so drop it - + return adcout diff --git a/mcp3551.py b/mcp3551.py index bcd08e6..d668c9a 100644 --- a/mcp3551.py +++ b/mcp3551.py @@ -8,14 +8,14 @@ class MCP3551(ADC): def setup_pins(self): GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) - + GPIO.setup(self._miso, GPIO.IN) GPIO.setup(self._clk, GPIO.OUT) GPIO.setup(self._cs, GPIO.OUT) def get_resolution(self): return 2097152.0 - + def read_value(self): GPIO.output(self._cs, True) GPIO.output(self._clk, True) @@ -44,7 +44,7 @@ def read_value(self): GPIO.output(self._clk, True) adcraw <<= 1 - + if (GPIO.input(self._miso)): adcraw |= 0x1 @@ -53,15 +53,15 @@ def read_value(self): if (adcraw & 0x400000): print "MCP3551 error: overflow high" - + if (adcraw & 0x800000): print "MCP3551 error: overflow low" - + # remove overflow and sign bits adcout = adcraw & 0x1FFFFF - + # convert to negative number if necessary if (adcraw & 0x200000): adcout = (~adcout & 0x1FFFFF) * -1 + 1 - + return adcout