Ejemplo n.º 1
0
def Rx_1byte_ICU(_receivepin, _baudrate):

    duration1bitMICROS = int(round(
        (1 / _baudrate) *
        (10**6)))  # 1s = 10^6 microseconds, rounded to next integer

    #wait for a falling edge to start capturing
    tmpICU = icu.capture(_receivepin,
                         LOW,
                         8,
                         int(10 * (round(1 / _baudrate)) * (10**6)),
                         time_unit=MICROS)  #returns a list of microseconds
    #create binary list from tmpICU
    timeListICU = [int(round(x / duration1bitMICROS))
                   for x in tmpICU]  #list of times in units of bit duration
    BinListICU = [
    ]  # initiate list of binary values created from the microseconds list coming from the ICU
    for i in range(len(tmpICU)):
        if i % 2 == 0:
            for j in range(timeListICU[i]):
                BinListICU.append(0)
        else:
            for j in range(timeListICU[i]):
                BinListICU.append(1)
    BinListICU[0:1] = []  #remove start bit

    if len(BinListICU) < 8:  #fill up all 8 bits
        for i in range(8 - len(BinListICU)):
            BinListICU.append(0)

    Buffer = SoftRx_Bin2Dec(BinListICU)
    return Buffer  # hand the buffer back to the thread/main program
Ejemplo n.º 2
0
def IR_capture():
    while True:
        print("Capturing...")
        # Starts capturing from the icu configured pin.
        # The capture starts from a selected trigger (in this case capture will start when the pin first goes from
        # HIGH to LOW).
        # The max number of samples to be collected and a maximum time window are specified.
        
        # Play with max number and time window to fit your remote protocol.
        # The following values are for the NEC IR (used by LG) protocol
        x = icu.capture(ir_pin,LOW,67,68,pull=HIGH)
        print(x,"\n captured n samples:",len(x))
Ejemplo n.º 3
0
def IR_capture():
    while True:
        print("Capturing...")
        # starts capturing from the chosen pin (which must be an icu pin) setting
        # a trigger (in this case capture will start when the pin first goes from
        # HIGH to LOW), the max number of samples to be collected (a sample represents the
        # interval in microseconds passed between a change from a LOW to a HIGH
        # value on the pin or viceversa) and a maximum time window in ms after that the function exits 
        
        # Values chosen for this example come from NEC IR (used by LG) specification.
        # Play with these for fitting your remote protocol
        x = icu.capture(ir_pin,LOW,67,68,pull=HIGH)
        print(x,"\n captured n samples:",len(x))
Ejemplo n.º 4
0
def IR_capture():
    while True:
        print("Capturing...")
        # starts capturing from the chosen pin (which must be an icu pin) setting
        # a trigger (in this case capture will start when the pin first goes from
        # HIGH to LOW), the max number of samples to be collected (a sample represents the
        # interval in microseconds passed between a change from a LOW to a HIGH
        # value on the pin or viceversa) and a maximum time window in ms after that the function exits

        # Values chosen for this example come from NEC IR (used by LG) specification.
        # Play with these for fitting your remote protocol
        x = icu.capture(ir_pin, LOW, 67, 68, pull=HIGH)
        print(x, "\n captured n samples:", len(x))
Ejemplo n.º 5
0
    def captureAndDecode(self,max_samples=100,time_window=200,wait=0):
        """
        .. method:: captureAndDecode(max_samples=100,time_window=200,wait=0)

            Starts the IR capture activating the ICU on the receiver pin and pass the captured raw signal to the decode method returning an IRPacket
            
            * max_samples: it sets the number of samples to be collected before terminate the capture. Default is set to 100 that works for most of the used IR protocols.
            * time_window: it sets the max amount in milliseconds of the capture window. Default is set to 200 milliseconds that works for most of the used IR protocols. 
            * wait: it allows the definition of a sleeping time in millisecond to be waited after a capture in order to avoid capturing repeated packet as partial raw data.
     
            Consider that air conditioning remote controller IR packets are very long therefore these values have to be tuned according tot the desired application.  
            
            The method returns an IRPacket.       
        """
        x=icu.capture(self.pin,LOW,max_samples,time_window,pull=HIGH)
        if wait!=0:
            sleep(wait)
        return self.decode(x)
Ejemplo n.º 6
0
    def captureAndDecode(self, max_samples=100, time_window=200, wait=0):
        """
        .. method:: captureAndDecode(max_samples=100,time_window=200,wait=0)

            Starts the IR capture activating the ICU on the receiver pin and pass the captured raw signal to the decode method returning an IRPacket
            
            * max_samples: it sets the number of samples to be collected before terminate the capture. Default is set to 100 that works for most of the used IR protocols.
            * time_window: it sets the max amount in milliseconds of the capture window. Default is set to 200 milliseconds that works for most of the used IR protocols. 
            * wait: it allows the definition of a sleeping time in millisecond to be waited after a capture in order to avoid capturing repeated packet as partial raw data.
     
            Consider that air conditioning remote controller IR packets are very long therefore these values have to be tuned according tot the desired application.  
            
            The method returns an IRPacket.       
        """
        x = icu.capture(self.pin, LOW, max_samples, time_window, pull=HIGH)
        if wait != 0:
            sleep(wait)
        return self.decode(x)
Ejemplo n.º 7
0
    def ReadDistance(self): 
        """This method calculate the distance and save it as class attribute, does not return anything"""
        raw_distance = 0
        sleep(60)#SR04 datasheet advice 60ms between two measurements
        digitalWrite(self.triggerPort,LOW)
        sleep(2,MICROS)
        digitalWrite(self.triggerPort,HIGH)
        sleep(10,MICROS)

        digitalWrite(self.triggerPort,LOW)
        elapsed_time = icu.capture(self.echoPort,LOW_TO_HIGH,1,23200,MICROS)

        # Some detail from sr04 datasheet:    
        # 23200 and 116 equals to 400cm and 2cm the range of the sensor
        # 23200 = 58*400; 116 = 58*2 
        # when it goes out or range (2 to 400 centimeters) elapsed_time is empty
        if elapsed_time:
            raw_distance=elapsed_time[0]/58
        else:
            raw_distance = 1

        self.distance = int(raw_distance)
Ejemplo n.º 8
0
    def read(self):
        ### don't execute this more than once every 2s!
#         print("read")
        timer1 = timers.timer()
        timer1.start()
        foo = 0

        self.DHT22_temp = 0
        self.DHT22_hum = 0
        BinListDHT22 = []
        timeListDHT22 = []

        #Go into high impedence state to let pull-up raise data line level and start the reading process.
        pinMode(self.receivepinShort,OUTPUT)
        digitalWrite(self.receivepinShort, HIGH)
        #wait 10ms
        timer1.reset()
        while timer1.get()<10: # maybe change this while to one_shot?
            foo+=1 # probably unecessary
        
        #First pull data line low for 10 ms.
        digitalWrite(self.receivepinShort, LOW)
        timer1.reset()
        while timer1.get()<10: # maybe change this while to one_shot?
            foo+=1 # probably unecessary
#         print("ICU")
        #get the data stream via ICU
        #call to ICU seems to take some time, thus call *before* initiation is finished
        tmpICU = icu.capture(self.receivepin,LOW,86,10000,time_unit=MICROS)
#         print(tmpICU)
        # End the start signal by setting data line high.
        digitalWrite(self.receivepinShort, HIGH)
        pinMode(self.receivepinShort,INPUT_PULLUP)
#         print("go to calculus")
        
        # remove all even entries, they're just "start bits", discard 1st two entries
        for i in range(3,len(tmpICU),1):
            if i%2!=0: #these are the odd entries
                timeListDHT22.append(tmpICU[i])
#         print(timeListDHT22)
        # convert to list of binaries
        for i in range(len(timeListDHT22)):
            if timeListDHT22[i] < 35:    # shouldn't be longer than 28us, but allow some wiggle room here
                BinListDHT22.append(0)
            else:
                BinListDHT22.append(1)    
        # extract hum, temp parts (16bits each)
        tmp_hum = BinListDHT22[0:16]    #1st 16 bits are humidity, 2nd 16 bits are temperature
        tmp_temp = BinListDHT22[16:32]
        
        tmp_tempSign = 1
        if tmp_temp[0] == 1:
            tmp_tempSign = -1 # neg temperatures are encoded most significant bit = 1
            tmp_temp[0] = 0
        tmp_temp = tmp_temp[::-1] #invert the list for conversion to decimal
        tmp_hum = tmp_hum[::-1]
#         print(tmp_temp,tmp_hum)

        DHT22_temp_1 = 0
        DHT22_hum_1 = 0
        for i in range(16):
            DHT22_temp_1 += tmp_temp[i]*(2**i)
            DHT22_hum_1 += tmp_hum[i]*(2**i)
            
#         print(DHT22_temp_1/10,DHT22_hum_1/10)
        self.DHT22_temp = DHT22_temp_1/10
        self.DHT22_hum = DHT22_hum_1/10
#         print(self.DHT22_temp, self.DHT22_hum)
        digitalWrite(self.receivepinShort, HIGH)
        timer1.clear()
Ejemplo n.º 9
0
    
#define a global variable for PWM duty cycle and turn on the PWM on board LED (Pin 13)

duty=10
pwm.write(pwmPin,100, duty,MICROS) #pwm.write needs (pn, period, duty, time_unit)

#define the function to be called for changing the PWM duty when the button is pressed
def pwm_control():
    global duty
    duty= duty+10
    pwm.write(pwmPin, 100, duty,MICROS)
    if duty>=100:
        duty=0
    print("Duty:", duty, "millis")
    
#Attach an interrupt on the button pin waiting for signal going from high to low when the button is pressed.
#The interrupt if triggered call the pwm_control function
onPinFall(buttonPin, pwm_control)

while True:
    #define an icu capture on pin 2 to be triggered when the pin rise.
    #this routine acquires 10 steps (HIGH or LOW states) or terminates after 50000 micros, the time unit is MICROS
    #this is a blocking function, icu.capture can be also instanced as non blocking using call-back, refer to the doc for more info
    x = icu.capture(captPin,HIGH,10,50000,MICROS)
    print("alive")
    # x is a list of steps lengths in microseconds, pass it to the printing function for showing on the serial console
    print_results(x)
    
    sleep(1000)
    
Ejemplo n.º 10
0
 def captureAndDecode(self, max_samples=100, time_window=200, wait=0):
     x = icu.capture(self.pin, LOW, max_samples, time_window, pull=HIGH)
     if wait != 0:
         sleep(wait)
     return self.decode(x)
Ejemplo n.º 11
0
pwm.write(pwmPin, 100, duty,
          MICROS)  #pwm.write needs (pn, period, duty, time_unit)


# define the function to be called for changing the PWM duty when the button is pressed
def pwm_control():
    global duty
    duty = duty + 10
    if duty >= 100:
        duty = 0
    pwm.write(pwmPin, 100, duty, MICROS)
    print("Duty:", duty, "millis")


# Attach an interrupt on the button pin waiting for signal going from high to low when the button is pressed.
# pwm_control will be called when the interrupt is fired.
# If you are on Arduino DUE and you haven't connected any button comment the following line
# you will not change the PWM duty but you can still test the ICU capture
onPinFall(buttonPin, pwm_control)

while True:
    # start an icu capture to be triggered when the pin rise.
    # this routine acquires 10 steps (HIGH or LOW states) or terminates after 50000 micros
    # this is a blocking function
    x = icu.capture(captPin, LOW_TO_HIGH, 10, 50000, MICROS)
    print("captured")
    # x is a list of step durations in microseconds, pass it to the printing function and check the serial console
    print_results(x)

    sleep(1000)
Ejemplo n.º 12
0
def getDHT22data(
    _receivepin, _receivepinShort
):  #expects input like (D3.ICU,D3) [TODO: do some nifty string manipulation to get "D3" from D3.ICU]

    ### don't execute this more than once every 2s!

    timer1 = timers.timer()
    timer1.start()
    foo = 0

    DHT22_temp = 0
    DHT22_hum = 0
    BinListDHT22 = []
    timeListDHT22 = []

    #Go into high impedence state to let pull-up raise data line level andstart the reading process.
    pinMode(_receivepinShort, OUTPUT)
    digitalWrite(_receivepinShort, HIGH)
    timer1.reset()
    while timer1.get() < 10:
        foo += 1
    #First set data line low for 10 milliseconds.
    digitalWrite(_receivepinShort, LOW)
    timer1.reset()
    while timer1.get() < 10:
        foo += 1  # maybe change this while to one_shot?
    tmpICU = icu.capture(
        _receivepin, LOW, 86, 10000, time_unit=MICROS
    )  #call to ICU seems to take some time, thus call *before* initiation is finished
    # End the start signal by setting data line high for [40 microseconds].
    digitalWrite(_receivepinShort, HIGH)
    pinMode(_receivepinShort, INPUT_PULLUP)

    # remove all even entries, they're just "start bits", discard 1st two entries
    for i in range(3, len(tmpICU), 1):
        if i % 2 != 0:  #these are the odd entries
            timeListDHT22.append(tmpICU[i])
    # convert to list of binaries
    for i in range(len(timeListDHT22)):
        if timeListDHT22[
                i] < 35:  # shouldn't be longer than 28us, but allow some wiggle room here
            BinListDHT22.append(0)
        else:
            BinListDHT22.append(1)
    # extract hum, temp parts (16bits each)
    tmp_hum = BinListDHT22[
        0:16]  #1st 16 bits are humidity, 2nd 16 bits are temperature
    tmp_temp = BinListDHT22[16:32]
    tmp_tempSign = 1
    if tmp_temp[0] == 1:
        tmp_tempSign = -1  # neg temperatures are encoded most significant bit = 1
        tmp_temp[0] = 0
    tmp_temp = tmp_temp[::-1]  #invert the list for conversion to decimal
    tmp_hum = tmp_hum[::-1]

    for i in range(16):
        DHT22_temp += tmp_temp[i] * (2**i)
        DHT22_hum += tmp_hum[i] * (2**i)
    DHT22_temp = DHT22_temp / 10
    DHT22_hum = DHT22_hum / 10

    digitalWrite(_receivepinShort, HIGH)

    timer1.clear()

    return (DHT22_hum, DHT22_temp)
 def captureAndDecode(self,max_samples=100,time_window=200,wait=0):
     x=icu.capture(self.pin,LOW,max_samples,time_window,pull=HIGH)
     if wait!=0:
         sleep(wait)
     return self.decode(x)