Hyungsun Yoon

[HW] Platform change to RPI Pico

...@@ -8,3 +8,4 @@ ...@@ -8,3 +8,4 @@
8 8
9 ## 일정 9 ## 일정
10 + 2021.05.03: Init(개인 아두이노 코드 git에서 이전) 10 + 2021.05.03: Init(개인 아두이노 코드 git에서 이전)
11 ++ 2021.05.11: Arduino 개발 중단, RaspberryPi Pico로 플랫폼 이전, C++ -> MicroPython
...\ No newline at end of file ...\ No newline at end of file
......
1 -from bluetooth import *
2 -
3 -socket = BluetoothSocket( RFCOMM )
4 -socket.connect(("04:A3:16:99:C7:42", 1))
5 -print("bluetooth connected!")
6 -
7 -msg = input("send message : ")
8 -socket.send(msg)
9 -
10 -print("finished")
11 -socket.close()
...\ No newline at end of file ...\ No newline at end of file
1 +from bluetooth import *
2 +import time
3 +
4 +client_socket = BluetoothSocket( RFCOMM )
5 +client_socket.connect(("00:18:91:D8:24:39", 1))
6 +print("bluetooth connected!")
7 +
8 +def _send_data(msg:str):
9 + client_socket.send(msg)
10 +
11 +def _recv_data():
12 + # 소켓에 2초간 받을 수 있는 시간을 준다
13 + client_socket.settimeout(2)
14 +
15 + # 데이터 받을 변수
16 + data = ''
17 +
18 + try:
19 + timeout_start = time.time()
20 + timeout = 2
21 + while time.time() < timeout_start + timeout:
22 + data += client_socket.recv(1024).decode('utf-8')
23 + except:
24 + print("INFO: DATA RECV TIMEOUT")
25 + finally:
26 + return data
27 +
28 +
29 +while True:
30 + sel_mode = input("SELECT MODE: ")
31 +
32 + if sel_mode == 'W':
33 + msg = input("send message : ")
34 + _send_data(msg)
35 +
36 + elif sel_mode == 'WR':
37 + msg = input("send message : ")
38 + _send_data(msg)
39 +
40 + data = _recv_data()
41 + print(f"Received: {data}")
42 +
43 +client_socket.close()
...\ No newline at end of file ...\ No newline at end of file
1 +import uos
2 +import machine
3 +import utime
4 +
5 +uart0 = machine.UART(0,baudrate=9600)
6 +
7 +# --------------------------------------------------- #
8 +# INTERNAL FUNCTIONS
9 +# --------------------------------------------------- #
10 +def _clartBuf(uart=uart0):
11 + ''' Clear Buffer
12 + '''
13 + while uart.any():
14 + print(uart.read(1))
15 +
16 +# --------------------------------------------------- #
17 +# FUNCTIONS
18 +# --------------------------------------------------- #
19 +def send_data_bt(sdata:str):
20 + ''' Send string data using Bluetooth
21 + '''
22 + _clartBuf()
23 + uart0.write(sdata)
24 +
25 +
26 +def recv_data_bt():
27 + ''' Receive string data using Bluetooth
28 + '''
29 + _clartBuf()
30 + prvMills = utime.ticks_ms()
31 + received_data = ''
32 +
33 + while (utime.ticks_ms()-prvMills)<2000:
34 + if uart0.any():
35 + single_data = uart0.read(1)
36 + received_data += single_data.decode('utf-8')
37 +
38 + return received_data
...\ No newline at end of file ...\ No newline at end of file
1 +''' Original code from https://www.raspberrypi.org/forums/viewtopic.php?t=303606
2 +'''
3 +import utime
4 +import rp2
5 +from rp2 import PIO, asm_pio
6 +from machine import Pin
7 +
8 +# --------------------------------------------------- #
9 +# INIT
10 +# --------------------------------------------------- #
11 +# For VCC
12 +dht_pwr = Pin(14, Pin.OUT)
13 +# For data
14 +dht_data = Pin(15, Pin.IN, Pin.PULL_UP)
15 +
16 +# Power on
17 +dht_pwr.value(1)
18 +# Create empty state
19 +sm=rp2.StateMachine(1)
20 +# Wait for DHT22 to start up
21 +utime.sleep(2)
22 +
23 +# --------------------------------------------------- #
24 +# INIT2
25 +# --------------------------------------------------- #
26 +@asm_pio(set_init=(PIO.OUT_HIGH),autopush=True, push_thresh=8) #output one byte at a time
27 +def DHT22():
28 + #drive output low for at least 20ms
29 + set(pindirs,1) #set pin to output
30 + set(pins,0) #set pin low
31 + set(y,31) #prepare countdown, y*x*100cycles
32 + label ('waity')
33 + set(x,31)
34 + label ('waitx')
35 + nop() [30]
36 + jmp(x_dec,'waitx') #decrement x reg every 100 cycles
37 + jmp(y_dec,'waity') #decrement y reg every time x reaches zero
38 +
39 + #begin reading from device
40 + set(pindirs,0) #set pin to input
41 + wait(1,pin,0) #check pin is high before starting
42 + wait(0,pin,0)
43 + wait(1,pin,0)
44 + wait(0,pin,0) #wait for start of data
45 +
46 + #read databit
47 + label('readdata')
48 + set(x,21) #reset x register to count down from 20
49 + wait(1,pin,0) #wait for high signal
50 + label('countdown')
51 + jmp(pin,'continue') #if pin still high continue counting
52 + #pin is low before countdown is complete - bit '0' detected
53 + set(y,0)
54 + in_(y, 1) #shift '0' into the isr
55 + jmp('readdata') #read the next bit
56 +
57 + label('continue')
58 + jmp(x_dec,'countdown') #decrement x reg and continue counting if x!=0
59 + #pin is still high after countdown complete - bit '1' detected
60 + set(y,1)
61 + in_(y, 1) #shift one bit into the isr
62 + wait(0,pin,0) #wait for low signal (next bit)
63 + jmp('readdata') #read the next bit
64 +
65 +
66 +# --------------------------------------------------- #
67 +# FUNCTIONS
68 +# --------------------------------------------------- #
69 +def work_dht():
70 + data=[]
71 + total=0
72 + sm.init(DHT22,freq=1600000,set_base=dht_data,in_base=dht_data,jmp_pin=dht_data)
73 + sm.active(1)
74 + for i in range(5): #data should be 40 bits (5 bytes) long
75 + data.append(sm.get()) #read byte
76 +
77 + #check checksum (lowest 8 bits of the sum of the first 4 bytes)
78 + for i in range(4):
79 + total=total+data[i]
80 + if((total & 255) == data[4]):
81 + humidity=((data[0]<<8) + data[1])/10.0
82 + temperature=(((data[2] &0x7f) << 8) + data[3]) /10.0
83 + if (data[2] & 0x80) == 0x80:
84 + temperature = -temperature
85 + return [humidity, temperature]
86 + else:
87 + return False
...\ No newline at end of file ...\ No newline at end of file
1 +import array, utime
2 +from machine import Pin
3 +import rp2
4 +
5 +import neopixel
6 +import dht
7 +import bluetoooth as bto
8 +
9 +# --------------------------------------------------- #
10 +# INIT
11 +# --------------------------------------------------- #
12 +
13 +
14 +# --------------------------------------------------- #
15 +# ENTRYPOINT
16 +# --------------------------------------------------- #
17 +def _run():
18 + while True:
19 + input_data = bto.recv_data_bt()
20 + if input_data != '':
21 + input_data = input_data.strip()
22 + print('INPUT FOUND ', input_data)
23 + print(len(input_data))
24 + if input_data == 'A':
25 + neopixel.work_led(0.2)
26 + elif input_data == 'B':
27 + dht_data = dht.work_dht()
28 + if dht_data == False:
29 + print("ERROR: DHT22 NOT WORKING")
30 + else:
31 + print("INFO: HUMI ", dht_data[0])
32 + print("INFO: TEMP ", dht_data[1])
33 + send_string = str(dht_data[0]) + ',' + str(dht_data[1])
34 + print(send_string)
35 + bto.send_data_bt(send_string)
36 + else:
37 + print('WRONG INPUT')
38 +
39 +if __name__ == '__main__':
40 + _run()
...\ No newline at end of file ...\ No newline at end of file
1 +''' Original code from https://core-electronics.com.au/tutorials/how-to-use-ws2812b-rgb-leds-with-raspberry-pi-pico.html
2 +'''
3 +import array, utime
4 +from machine import Pin
5 +import rp2
6 +
7 +# --------------------------------------------------- #
8 +# INIT
9 +# --------------------------------------------------- #
10 +NUM_LEDS = 8
11 +PIN_NUM = 22
12 +
13 +BLACK = (0, 0, 0)
14 +RED = (255, 0, 0)
15 +YELLOW = (255, 150, 0)
16 +GREEN = (0, 255, 0)
17 +CYAN = (0, 255, 255)
18 +BLUE = (0, 0, 255)
19 +PURPLE = (180, 0, 255)
20 +WHITE = (255, 255, 255)
21 +COLORS = (BLACK, RED, YELLOW, GREEN, CYAN, BLUE, PURPLE, WHITE, BLACK)
22 +
23 +# --------------------------------------------------- #
24 +# INIT2
25 +# --------------------------------------------------- #
26 +@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24)
27 +def ws2812():
28 + T1 = 2
29 + T2 = 5
30 + T3 = 3
31 + wrap_target()
32 + label("bitloop")
33 + out(x, 1) .side(0) [T3 - 1]
34 + jmp(not_x, "do_zero") .side(1) [T1 - 1]
35 + jmp("bitloop") .side(1) [T2 - 1]
36 + label("do_zero")
37 + nop() .side(0) [T2 - 1]
38 + wrap()
39 +
40 +# Create the StateMachine with the ws2812 program, outputting on pin
41 +sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(PIN_NUM))
42 +# Start the StateMachine, it will wait for data on its FIFO.
43 +sm.active(1)
44 +# Display a pattern on the LEDs via an array of LED RGB values.
45 +ar = array.array("I", [0 for _ in range(NUM_LEDS)])
46 +
47 +# --------------------------------------------------- #
48 +# INTERNAL FUNCTIONS
49 +# --------------------------------------------------- #
50 +def _pixels_show(brightness:int = 0.2):
51 + dimmer_ar = array.array("I", [0 for _ in range(NUM_LEDS)])
52 + for i,c in enumerate(ar):
53 + r = int(((c >> 8) & 0xFF) * brightness)
54 + g = int(((c >> 16) & 0xFF) * brightness)
55 + b = int((c & 0xFF) * brightness)
56 + dimmer_ar[i] = (g<<16) + (r<<8) + b
57 + sm.put(dimmer_ar, 8)
58 + utime.sleep_ms(10)
59 +
60 +def _pixels_set(i, color):
61 + ar[i] = (color[1]<<16) + (color[0]<<8) + color[2]
62 +
63 +def _pixels_fill(color):
64 + for i in range(len(ar)):
65 + _pixels_set(i, color)
66 +
67 +
68 +# --------------------------------------------------- #
69 +# FUNCTIONS
70 +# --------------------------------------------------- #
71 +def work_led(brightness:int = 0.2):
72 + for color in COLORS:
73 + _pixels_fill(color)
74 + _pixels_show(brightness)
75 + utime.sleep(0.4)