コード例 #1
0
class DHT(object):
    def __init__(self,
                 pin: Pin,
                 state_machine_id: int = 0,
                 min_interval: int = 2000):
        """
        Create a DHT object to get communication and get data from DHT sensor.
        :param pin: Pin connected to DHT's data pin
        :param state_machine_id: State Machine ID, default value 1
        :param min_interval: Minimum interval between communication with DHT, default value 2000 (for DHT22)
        :type pin: machine.Pin
        :type state_machine_id: int
        :type min_interval: int
        """
        self._pin = pin
        self._last_pull_time = None
        self._temperature = None
        self._humidity = None
        self._min_interval = min_interval
        # 1 cycle should be 10 us, 1s = 1,000,000us so freq should be 100,000
        self._sm = StateMachine(state_machine_id,
                                dht_get_data,
                                freq=100000,
                                set_base=pin)
        self._sm.irq(handle_dht_irq)
        self._sm.active(1)

    def _get_data_from_sensor(self, force: bool = False):
        if force or self._last_pull_time is None or \
                fabs(ticks_diff(ticks_ms(), self._last_pull_time)) > self._min_interval:
            global _irq_count, temp_data
            _irq_count = 0
            for i in range(5):
                temp_data[i] = 0

            # start state machine
            self._sm.put(0)

            # Wait for state machine work
            utime.sleep_ms(20)

            if _irq_count != 5:
                print(
                    "Didn't receive enough data. Received {} byte(s).".format(
                        len(temp_data)))
                return

            # data validation, 1st byte + 2nd byte + 3rd byte + 4th byte == 5th byte (last 8 bits)
            check_sum = (temp_data[0] + temp_data[1] + temp_data[2] +
                         temp_data[3]) & EIGHT_1_BIT_MASK
            if check_sum != temp_data[4]:
                print('Data validation error.')
                return

            # temperature data is last 15 bits, first bit if 1, is negative. data is 10x of actual value
            raw_temperature = (
                (temp_data[2] & SEVEN_1_BIT_MASK) << 8) + temp_data[3]
            self._temperature = raw_temperature / 10
            if temp_data[2] >> 7 == 1:
                self._temperature *= -1

            raw_humidity = (temp_data[0] << 8) + temp_data[1]
            # humidity data is 10x of actual value
            self._humidity = raw_humidity / 10

            self._last_pull_time = ticks_ms()

    def get_temperature(self, force: bool = False) -> float:
        """
        Get temperature from DHT
        :param force: Force communicate with DHT sensor
        :type force: bool
        :return: Last measured temperature
        :rtype: float
        """
        self._get_data_from_sensor(force)
        return self._temperature

    def get_humidity(self, force: bool = False) -> float:
        """
        Get humidity from DHT
        :param force: Force communicate with DHT sensor
        :type force: bool
        :return: Last measured humidity
        :rtype: float
        """
        self._get_data_from_sensor(force)
        return self._humidity

    def get_temperature_and_humidity(self,
                                     force: bool = False) -> (float, float):
        """
        Get temperature and humidity from DHT
        :param force: Force communicate with DHT sensor
        :type force: bool
        :return: Last measured temperature and humidity
        :rtype: (float, float)
        """
        self._get_data_from_sensor(force)
        return self._temperature, self._humidity
コード例 #2
0
data = [(1, 2, 4, 8), (2, 4, 8, 1), (4, 8, 1, 2), (8, 1, 2, 4)]
steps = 0


def turn(sm):
    global steps
    global data

    idx = steps % 4
    a = data[idx][0] | (data[idx][1] << 4) | (data[idx][2] << 8) | (
        data[idx][3] << 12)
    a = a << 16 | a

    #print("{0:b}".format(a))
    sleep(1)

    sm.put(500)
    sm.put(a)

    steps += 500


sm.irq(turn)
sm.active(1)
turn(sm)

sleep(50)
print("done")
sm.active(0)
sm.exec("set(pins,0)")
コード例 #3
0

# Function for core1 to execute to write to the given UART.
def core1_task(uart, text):
    uart.write(text)


# Set up the hard UART we're going to use to print characters.
uart = UART(1, UART_BAUD, tx=HARD_UART_TX_PIN)

for pio_prog in ("uart_rx_mini", "uart_rx"):
    # Set up the state machine we're going to use to receive the characters.
    sm = StateMachine(
        0,
        globals()[pio_prog],
        freq=8 * UART_BAUD,
        in_base=PIO_RX_PIN,  # For WAIT, IN
        jmp_pin=PIO_RX_PIN,  # For JMP
    )
    sm.irq(handler)
    sm.active(1)

    # Tell core 1 to print some text to UART 1
    text = "Hello, world from PIO, using {}!".format(pio_prog)
    _thread.start_new_thread(core1_task, (uart, text))

    # Echo characters received from PIO to the console.
    for i in range(len(text)):
        print(chr(sm.get() >> 24), end="")
    print()