#!/usr/bin/python3
#
# http://www.forum-raspberrypi.de/Thread-rpm-pigpio-falsche-werte-entprellen
#
from functools import partial
from queue import Queue
from time import sleep, time
import pigpio
class ImpulsCounter:
def __init__(self, pi, gpio):
self.pi = pi
self.gpio = gpio
self.count = 0
pi.set_mode(gpio, pigpio.INPUT)
pi.set_pull_up_down(gpio, pigpio.PUD_UP)
self._cb = pi.callback(gpio, pigpio.EITHER_EDGE, self._callback)
def _callback(self, gpio, level, tick):
self.count += 1
def get(self):
return self.count
def reset(self):
self.count = 0
def cancel(self):
self._cb.cancel()
def calculate_power(level, rpm, level_to_power, lower_limit, higher_limit):
if rpm <= lower_limit:
return 0
if rpm >= higher_limit:
return 0
return level_to_power[level][(rpm) // 10]
def switch_callback(queue, gpio, level, tick):
queue.put( gpio )
def main(sensor=4, btnPlus=21, btnMinus=20, timeout=5.0, sample_time=2.0):
LEVEL_TO_POWER = {
1: [6, 12, 20, 29, 40, 53, 69, 79, 92, 106, 121],
2: [8, 16, 26, 38, 53, 68, 88, 103, 120, 138, 152],
3: [9, 20, 32, 47, 66, 84, 107, 125, 148, 172, 186],
4: [11, 23, 39, 56, 79, 101, 126, 150, 173, 206, 219],
5: [13, 27, 45, 65, 92, 117, 145, 175, 202, 238, 254],
6: [15, 31, 52, 75, 105, 135, 166, 202, 231, 275, 289],
7: [16, 35, 58, 85, 118, 152, 185, 226, 260, 305, 332],
8: [18, 39, 65, 96, 131, 169, 208, 249, 289, 333, 375],
9: [19, 42, 71, 104, 144, 184, 227, 272, 318, 361, 408],
10: [21, 46, 77, 113, 157, 199, 245, 295, 345, 386, 442],
11: [23, 50, 84, 123, 170, 216, 262, 318, 372, 413, 480],
12: [24, 53, 89, 131, 183, 230, 279, 342, 398, 441, 512],
13: [26, 56, 94, 139, 196, 245, 296, 365, 424, 468, 548],
14: [28, 60, 101, 148, 209, 261, 318, 389, 449, 494, 585],
15: [30, 64, 108, 158, 222, 277, 337, 415, 476, 518, 620],
16: [32, 68, 115, 168, 235, 296, 355, 439, 503, 548, 658],
17: [33, 72, 122, 177, 248, 312, 373, 463, 530, 576, 694],
18: [35, 76, 129, 187, 261, 328, 390, 484, 556, 606, 727],
19: [37, 79, 134, 195, 274, 342, 407, 507, 572, 632, 763],
20: [39, 83, 140, 204, 287, 354, 424, 528, 598, 659, 790],
21: [40, 87, 146, 213, 300, 368, 442, 551, 616, 689, 812],
22: [42, 91, 153, 223, 313, 385, 461, 574, 645, 720, 840],
23: [44, 95, 160, 234, 326, 401, 479, 598, 673, 752, 872],
24: [47, 101, 171, 246, 340, 418, 501, 625, 706, 788, 908],
}
level = 5
switch_queue = Queue()
pi = pigpio.pi()
pi.set_mode(btnPlus, pigpio.INPUT)
pi.set_mode(btnMinus, pigpio.INPUT)
pi.set_pull_up_down(btnPlus, pigpio.PUD_UP)
pi.set_pull_up_down(btnMinus, pigpio.PUD_UP)
pi.callback(btnPlus, pigpio.EITHER_EDGE, partial(switch_callback, switch_queue))
pi.callback(btnMinus, pigpio.EITHER_EDGE, partial(switch_callback, switch_queue))
previous_count = 0
start_time = time()
impuls = ImpulsCounter(pi, sensor)
try:
while True:
sleep(sample_time)
count = impuls.get()
if previous_count == count and time() - start_time >= timeout:
impuls.reset()
count = impuls.get()
start_time = time()
print("count: {0}".format(count))
previous_count = count
if not switch_queue.empty():
pin = switch_queue.get()
if pin == btnPlus:
if level < 24:
level = level + 1
elif pin == btnMinus:
if level > 1:
level = level - 1
print("level: {0}".format(level))
print("power: {0}".format(calculate_power(level, count, LEVEL_TO_POWER, 20, 120)))
except (KeyboardInterrupt, SystemExit):
pi.stop()
print('\nQuit\n')
if __name__ == '__main__':
main()
#EOF