Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4774

General discussion • Building logic and decoding pulse received on gpio using PIGPIO

$
0
0
I have a device that sends a synchronous signal in a particular time interval.
I have used the pigpio library(monitor.py) to first match the durations of the pulses with that to an oscilloscope. And it is coming good .

Recognizing a signal train: It starts with a long low pulse of 61ms followed by a high pulse of 3.8 ms. After that the data bits starts.
Recognising the bits: bit 0- low duration of 3ms followed by high duration of 1ms.; bit 1- low duration of 1ms followed by high duration of 3ms.

monitor.py:

Code:

import timeimport pigpiolast_tick = Noneduration_list = []def cbf(GPIO, level, tick):    global last_tick, duration_list    if last_tick is not None:        diff = pigpio.tickDiff(last_tick, tick)        print("G={} l={} d={}".format(GPIO, level, diff))    last_tick = tickpi = pigpio.pi()if not pi.connected:    exit()# Add event detection for both rising and falling edges on GPIO 4cb = pi.callback(4, pigpio.EITHER_EDGE, cbf)try:    while True:        time.sleep(60)except KeyboardInterrupt:    print("\nTidying up")    cb.cancel()    pi.stop()
Below is my logic implementation inside the callback function:

Code:

def cbf(GPIO, level, tick):    global last, tick_start, bit_data, bit_start_seq, diff_data1, diff_data2, start_seq, Start_state    if last[GPIO] is not None:        diff = pigpio.tickDiff(last[GPIO], tick)        rounded_diff = round_duration(diff)        # print("G={} l={} d={}".format(GPIO, level, rounded_diff))        if rounded_diff > 61000:            Start_state = True            process_data(bit_data)            print(bit_data)            bit_data = []        if level != pigpio.TIMEOUT and rounded_diff < 3500 and Start_state: #and :tick_start != 0             if(pigpio.FALLING_EDGE):                bit_start_seq += 1                if(bit_start_seq == 2):                    diff_data2 = rounded_diff                        if(pigpio.RISING_EDGE == 0 and bit_start_seq == 1):                start_seq = True                diff_data1 = rounded_diff            if(start_seq and bit_start_seq == 2):                if(diff_data1 > diff_data2):                    bit_data.append(0)                else:                    bit_data.append(1)                bit_start_seq = 0                start_seq = False               last[GPIO] = tick
the process data inside the cbf would be used later after getting the bits.

Problem: sometimes i am getting the bits correct and sometimes it is all just 1,1,1,1
how do can I change the logic something to like: First it would call the cbf, the data would be stored in a list and after two seconds it would break. Now with the achieved list i would Implement my logic and get the bits and process then. again call the cbf.

Statistics: Posted by Axon_11 — Sun Apr 28, 2024 3:08 am



Viewing all articles
Browse latest Browse all 4774

Trending Articles