├── ADkeyboard ├── draw │ └── main.py └── main.py ├── ADrotary ├── .keep └── README.md ├── DHT11 ├── DHT22.py ├── main.py └── 引脚图.jpg ├── DS1302 ├── DS1302.py ├── LCD1602_clock │ ├── DS1302.py │ ├── esp8266_i2c_lcd.py │ ├── lcd_api.py │ └── main.py ├── ds1302.jpg └── main.py ├── HC-SR04 ├── hcsr04.py └── main.py ├── LICENSE ├── MG996R └── main.py ├── NEC_遥控 ├── .keep └── README.md ├── Pico-R3-A4-Pinout.pdf ├── Pin └── main.py ├── README.md ├── SG90 └── main.py ├── ULN2003 ├── Stepper.py └── main.py ├── VL53L0X ├── .keep ├── README.md └── vl53l0x.py ├── bmp180 ├── .keep ├── bmp180.py ├── demo.py └── readme.md ├── bmp280 ├── .keep ├── bmp280.py ├── main.py └── readme.md ├── empty ├── boot.py └── main.py ├── esp01 └── esp01.py ├── gps ├── .keep ├── demo.py ├── neo6.py └── readme.md ├── hc12 ├── HC-HID_V1.2.exe ├── MYUART.py └── uar.py ├── ina3221 ├── .keep ├── 1.jpg ├── address.jpg ├── ex3221.py ├── ina3221.py ├── readme.md └── 建议将A0与GND相连.jpg ├── lcd1602 ├── esp8266_i2c_lcd.py ├── lcd_api.py └── main.py ├── lcd2004 ├── lcd2004.py └── main.py ├── max7219 ├── main.py ├── max7219.py └── 连线.jpg ├── mlx90614 ├── .keep ├── README.md └── mlx90614.py ├── pca9685 ├── LICENSE ├── README.md ├── docs │ ├── Makefile │ ├── conf.py │ ├── index.rst │ ├── make.bat │ ├── motor.rst │ ├── pca9685.rst │ └── servo.rst ├── main.py ├── motor.py ├── pca9685.py ├── servo.py ├── setup.py ├── stepper.py └── 接线.png ├── rotary ├── .keep ├── readme.md ├── rotary.py ├── rotary_irq_esp.py ├── rotary_irq_pyb.py └── rotary_irq_rp2.py ├── rp2-pico-20210206-unstable-v1.14-9-g9dedcf122.uf2 ├── rp2-pico-20220117-v1.18.uf2 ├── ssd1306 ├── badapple25s │ ├── bad.data │ ├── main.py │ └── ssd1306.py ├── badapplecz │ ├── bad.data │ └── main.py ├── main.py ├── pbm │ ├── WeAct Studio Download Tool 烧录教程.mp4 │ ├── a.jpg │ ├── a.pbm │ ├── b.jpg │ ├── b.pbm │ ├── lifegame-main (1).zip │ ├── lifegame-main.zip │ ├── main.py │ └── pbm.py └── ssd1306.py ├── tm1637 ├── main.py └── tm1637.py ├── wifi ├── LICENSE ├── README.md └── wifiat.py └── ws2812 ├── main.py └── pico_python_ws2812b-main ├── .gitignore ├── LICENSE ├── README.md ├── examples ├── fireflies.py ├── flash.py ├── rainbow.py └── thermometer.py ├── pico_ws2812b.jpg └── ws2812b.py /ADkeyboard/draw/main.py: -------------------------------------------------------------------------------- 1 | from machine import I2C,Pin 2 | from ssd1306 import SSD1306_I2C#I2C的oled选该方法 3 | i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000) 4 | oled = SSD1306_I2C(128, 64, i2c) #你的OLED分辨率,使用I2C 5 | from machine import ADC 6 | import time 7 | 8 | c=ADC(2) 9 | 10 | 11 | oled.fill(1) #清空屏幕 12 | oled.show() 13 | oled.fill(0) 14 | oled.show() 15 | 16 | 17 | 18 | s=range(9000,11000) 19 | x=range(19000,21000) 20 | z=range(0,2000) 21 | y=range(30000,32000) 22 | q=range(45000,47000) 23 | 24 | qr=False 25 | p=[0,0] 26 | 27 | 28 | m=[] 29 | while True: 30 | oled.pixel(p[0],p[1],1) 31 | oled.show() 32 | time.sleep(0.1) 33 | oled.pixel(p[0],p[1],0) 34 | oled.show() 35 | 36 | u=c.read_u16() 37 | if u in q: 38 | qr=not qr 39 | elif u in s: 40 | p[1]=p[1]-1 41 | elif u in x: 42 | p[1]=p[1]+1 43 | elif u in z: 44 | p[0]=p[0]-1 45 | elif u in y: 46 | p[0]=p[0]+1 47 | else: 48 | pass 49 | if qr: 50 | if p not in m: 51 | nc=p*1 52 | m.append(nc) 53 | # print(m) 54 | for i in m: 55 | oled.pixel(i[0],i[1],1) 56 | oled.show() 57 | time.sleep(0.2) 58 | -------------------------------------------------------------------------------- /ADkeyboard/main.py: -------------------------------------------------------------------------------- 1 | from machine import ADC 2 | import time 3 | # 34号 4 | c=ADC(2) 5 | c.read_u16() 6 | 7 | s=range(9000,11000) 8 | x=range(19000,21000) 9 | z=range(0,2000) 10 | y=range(30000,32000) 11 | q=range(45000,47000) 12 | 13 | while True: 14 | cc=c.read_u16() 15 | if cc in s: 16 | print("s") 17 | elif cc in x: 18 | print("x") 19 | elif cc in z: 20 | print("z") 21 | elif cc in y: 22 | print("y") 23 | elif cc in q: 24 | print("q") 25 | else: 26 | print("0") 27 | time.sleep(0.5) 28 | # 不按60000以上 29 | # 上(9000-11000) 30 | # 左(0-2000) 右(30000-32000) 外(45000-47000) 31 | # 下(19000-21000) 32 | -------------------------------------------------------------------------------- /ADrotary/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/ADrotary/.keep -------------------------------------------------------------------------------- /ADrotary/README.md: -------------------------------------------------------------------------------- 1 | 2 | |Rotation sensor|pico| 3 | |-|-| 4 | |GND|GND| 5 | |vcc|3v3| 6 | |OUT|26| 7 | 8 | ```python 9 | from machine import ADC 10 | import time 11 | c=ADC(0)# GPIO26 ADC为0 12 | while True: 13 | print(c.read_u16()) 14 | time.sleep(0.1) 15 | ``` 16 | ADC值为 65535-0 间 17 | 18 | 只能转360度 19 | 20 | -------------------------------------------------------------------------------- /DHT11/DHT22.py: -------------------------------------------------------------------------------- 1 | ''' 2 | * 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2021 Daniel Perron 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | ''' 27 | 28 | import utime 29 | import rp2 30 | from rp2 import PIO, asm_pio 31 | from machine import Pin 32 | 33 | # 34 | # A B C D E F 35 | # ___ ___ ... 36 | # ____/ \___/ \___/ \ 37 | # 38 | # A = start pulse (> 1ms ) 39 | # B = 2-40 us 40 | # C = 80 us 41 | # D = 80 us 42 | # 43 | # E and F are data clock 44 | # 45 | # E = 50 us 46 | # F = 26-28 us => 0 70 us => 1 47 | # 48 | 49 | 50 | @asm_pio(set_init=(PIO.OUT_HIGH),autopush=True, push_thresh=8) 51 | def DHT22_PIO(): 52 | # clock set at 500Khz Cycle is 2us 53 | # drive output low for at least 20ms 54 | set(y,1) # 0 55 | pull() # 1 56 | mov(x,osr) # 2 57 | set(pindirs,1) # 3 set pin to output 58 | set(pins,0) # 4 set pin low 59 | label ('waitx') 60 | jmp(x_dec,'waitx') # 5 decrement x reg every 32 cycles 61 | set(pindirs,0) # 6 set pin to input 62 | # STATE A. Wait for high at least 80us. max should be very short 63 | set(x,31) # 7 64 | label('loopA') 65 | jmp(pin,'got_B') # 8 66 | jmp(x_dec,'loopA') # 9 67 | label('Error') 68 | in_(y,1) # 10 69 | jmp('Error') # 11 Infinity loop error 70 | 71 | # STATE B. Get HIGH pulse. max should be 40us 72 | label('got_B') 73 | set(x,31) # 12 74 | label('loop_B') 75 | jmp(x_dec,'check_B') # 13 76 | jmp('Error') # 14 77 | label('check_B') 78 | jmp(pin,'loop_B') # 15 79 | 80 | # STATE C. Get LOW pulse. max should be 80us 81 | set(x,31) # 16 82 | label('loop_C') 83 | jmp(pin,'got_D') # 17 84 | jmp(x_dec,'loop_C') # 18 85 | jmp('Error') # 19 86 | 87 | # STATE D. Get HIGH pulse. max should be 80us 88 | label('got_D') 89 | set(x,31) # 20 90 | label('loop_D') 91 | jmp(x_dec,'check_D') # 21 92 | jmp('Error') # 22 93 | label('check_D') 94 | jmp(pin,'loop_D') # 23 95 | 96 | # STATE E. Get Low pulse delay. should be around 50us 97 | set(x,31) # 24 98 | label('loop_E') 99 | jmp(pin,'got_F') # 25 100 | jmp(x_dec,'loop_E') # 26 101 | jmp('Error') # 27 102 | 103 | # STATE F. 104 | # wait 40 us 105 | label('got_F') 106 | nop() [20] # 28 107 | in_(pins,1) # 29 108 | # now wait for low pulse 109 | set(x,31) # 30 110 | jmp('loop_D') # 31 111 | 112 | 113 | 114 | 115 | class DHT22: 116 | 117 | def __init__(self,dataPin, powerPin=None,dht11=False,smID=1): 118 | self.dataPin = dataPin 119 | self.powerPin = powerPin 120 | self.dht11 = dht11 121 | self.smID = smID 122 | self.dataPin.init(Pin.IN, Pin.PULL_UP) 123 | if self.powerPin is not None: 124 | self.powerPin.init(Pin.OUT) 125 | self.powerPin.value(0) 126 | self.sm= rp2.StateMachine(self.smID) 127 | 128 | 129 | 130 | 131 | 132 | def read_array(self): 133 | if self.powerPin is not None: 134 | self.powerPin.value(1) 135 | utime.sleep_ms(100) 136 | #start state machine 137 | self.sm.init(DHT22_PIO,freq=500000, 138 | set_base=self.dataPin, 139 | in_base=self.dataPin, 140 | jmp_pin=self.dataPin) 141 | if self.dht11: 142 | self.sm.put(10000) 143 | else: 144 | self.sm.put(1000) 145 | self.sm.active(1) 146 | value = [] 147 | for i in range(5): 148 | value.append(self.sm.get()) 149 | self.sm.active(0) 150 | return value 151 | 152 | def read(self): 153 | value = self.read_array() 154 | sumV = 0 155 | for i in range(4): 156 | sumV += value[i] 157 | if (sumV & 0xff) == value[4]: 158 | if self.dht11: 159 | humidity=value[0] & 0x7f 160 | temperature=value[2] 161 | else: 162 | humidity=((value[0]<<8) + value[1])/10.0 163 | temperature=(((value[2] &0x7f) << 8) + value[3]) /10.0 164 | if (value[2] & 0x80) == 0x80: 165 | temperature = -temperature 166 | return temperature, humidity 167 | else: 168 | return None, None 169 | 170 | if __name__ == "__main__": 171 | from machine import Pin 172 | from DHT22 import DHT22 173 | import utime 174 | dht_data = Pin(15,Pin.IN,Pin.PULL_UP) 175 | dht_sensor=DHT22(dht_data,Pin(14,Pin.OUT),dht11=False) 176 | while True: 177 | T,H = dht_sensor.read() 178 | if T is None: 179 | print(" sensor error") 180 | else: 181 | print("{:3.1f}'C {:3.1f}%".format(T,H)) 182 | #DHT22 not responsive if delay to short 183 | utime.sleep_ms(500) 184 | 185 | -------------------------------------------------------------------------------- /DHT11/main.py: -------------------------------------------------------------------------------- 1 | from machine import Pin 2 | from DHT22 import DHT22 3 | import utime 4 | 5 | # DHT22 libray is available at 6 | # https://github.com/danjperron/PicoDHT22 7 | 8 | dht_sensor=DHT22(Pin(0,Pin.IN,Pin.PULL_UP),dht11=True) 9 | T,H = dht_sensor.read() 10 | # 正面 11 | # VCC-DATA-none——GND -------------------------------------------------------------------------------- /DHT11/引脚图.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/DHT11/引脚图.jpg -------------------------------------------------------------------------------- /DS1302/DS1302.py: -------------------------------------------------------------------------------- 1 | ''' 2 | DS1302 RTC drive 3 | 4 | Author: shaoziyang 5 | Date: 2018.3 6 | 7 | http://www.micropython.org.cn 8 | ''' 9 | from machine import * 10 | 11 | DS1302_REG_SECOND = (0x80) 12 | DS1302_REG_MINUTE = (0x82) 13 | DS1302_REG_HOUR = (0x84) 14 | DS1302_REG_DAY = (0x86) 15 | DS1302_REG_MONTH = (0x88) 16 | DS1302_REG_WEEKDAY= (0x8A) 17 | DS1302_REG_YEAR = (0x8C) 18 | DS1302_REG_WP = (0x8E) 19 | DS1302_REG_CTRL = (0x90) 20 | DS1302_REG_RAM = (0xC0) 21 | 22 | class DS1302: 23 | def __init__(self, clk, dio, cs): 24 | self.clk = clk 25 | self.dio = dio 26 | self.cs = cs 27 | self.clk.init(Pin.OUT) 28 | self.cs.init(Pin.OUT) 29 | 30 | def DecToHex(self, dat): 31 | return (dat//10) * 16 + (dat%10) 32 | 33 | def HexToDec(self, dat): 34 | return (dat//16) * 10 + (dat%16) 35 | 36 | def write_byte(self, dat): 37 | self.dio.init(Pin.OUT) 38 | for i in range(8): 39 | self.dio.value((dat >> i) & 1) 40 | self.clk.value(1) 41 | self.clk.value(0) 42 | 43 | def read_byte(self): 44 | d = 0 45 | self.dio.init(Pin.IN) 46 | for i in range(8): 47 | d = d | (self.dio.value()<> i) & 1) 40 | self.clk.value(1) 41 | self.clk.value(0) 42 | 43 | def read_byte(self): 44 | d = 0 45 | self.dio.init(Pin.IN) 46 | for i in range(8): 47 | d = d | (self.dio.value()< 1: 41 | cmd |= self.LCD_FUNCTION_2LINES 42 | self.hal_write_command(cmd) 43 | 44 | def hal_write_init_nibble(self, nibble): 45 | """Writes an initialization nibble to the LCD. 46 | 47 | This particular function is only used during initialization. 48 | """ 49 | byte = ((nibble >> 4) & 0x0f) << SHIFT_DATA 50 | self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E])) 51 | self.i2c.writeto(self.i2c_addr, bytearray([byte])) 52 | 53 | def hal_backlight_on(self): 54 | """Allows the hal layer to turn the backlight on.""" 55 | self.i2c.writeto(self.i2c_addr, bytearray([1 << SHIFT_BACKLIGHT])) 56 | 57 | def hal_backlight_off(self): 58 | """Allows the hal layer to turn the backlight off.""" 59 | self.i2c.writeto(self.i2c_addr, bytearray([0])) 60 | 61 | def hal_write_command(self, cmd): 62 | """Writes a command to the LCD. 63 | 64 | Data is latched on the falling edge of E. 65 | """ 66 | byte = ((self.backlight << SHIFT_BACKLIGHT) | (((cmd >> 4) & 0x0f) << SHIFT_DATA)) 67 | self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E])) 68 | self.i2c.writeto(self.i2c_addr, bytearray([byte])) 69 | byte = ((self.backlight << SHIFT_BACKLIGHT) | ((cmd & 0x0f) << SHIFT_DATA)) 70 | self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E])) 71 | self.i2c.writeto(self.i2c_addr, bytearray([byte])) 72 | if cmd <= 3: 73 | # The home and clear commands require a worst case delay of 4.1 msec 74 | sleep_ms(5) 75 | 76 | def hal_write_data(self, data): 77 | """Write data to the LCD.""" 78 | byte = (MASK_RS | (self.backlight << SHIFT_BACKLIGHT) | (((data >> 4) & 0x0f) << SHIFT_DATA)) 79 | self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E])) 80 | self.i2c.writeto(self.i2c_addr, bytearray([byte])) 81 | byte = (MASK_RS | (self.backlight << SHIFT_BACKLIGHT) | ((data & 0x0f) << SHIFT_DATA)) 82 | self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E])) 83 | self.i2c.writeto(self.i2c_addr, bytearray([byte])) 84 | -------------------------------------------------------------------------------- /DS1302/LCD1602_clock/lcd_api.py: -------------------------------------------------------------------------------- 1 | """Provides an API for talking to HD44780 compatible character LCDs.""" 2 | 3 | import time 4 | 5 | class LcdApi: 6 | """Implements the API for talking with HD44780 compatible character LCDs. 7 | This class only knows what commands to send to the LCD, and not how to get 8 | them to the LCD. 9 | 10 | It is expected that a derived class will implement the hal_xxx functions. 11 | """ 12 | 13 | # The following constant names were lifted from the avrlib lcd.h 14 | # header file, however, I changed the definitions from bit numbers 15 | # to bit masks. 16 | # 17 | # HD44780 LCD controller command set 18 | 19 | LCD_CLR = 0x01 # DB0: clear display 20 | LCD_HOME = 0x02 # DB1: return to home position 21 | 22 | LCD_ENTRY_MODE = 0x04 # DB2: set entry mode 23 | LCD_ENTRY_INC = 0x02 # --DB1: increment 24 | LCD_ENTRY_SHIFT = 0x01 # --DB0: shift 25 | 26 | LCD_ON_CTRL = 0x08 # DB3: turn lcd/cursor on 27 | LCD_ON_DISPLAY = 0x04 # --DB2: turn display on 28 | LCD_ON_CURSOR = 0x02 # --DB1: turn cursor on 29 | LCD_ON_BLINK = 0x01 # --DB0: blinking cursor 30 | 31 | LCD_MOVE = 0x10 # DB4: move cursor/display 32 | LCD_MOVE_DISP = 0x08 # --DB3: move display (0-> move cursor) 33 | LCD_MOVE_RIGHT = 0x04 # --DB2: move right (0-> left) 34 | 35 | LCD_FUNCTION = 0x20 # DB5: function set 36 | LCD_FUNCTION_8BIT = 0x10 # --DB4: set 8BIT mode (0->4BIT mode) 37 | LCD_FUNCTION_2LINES = 0x08 # --DB3: two lines (0->one line) 38 | LCD_FUNCTION_10DOTS = 0x04 # --DB2: 5x10 font (0->5x7 font) 39 | LCD_FUNCTION_RESET = 0x30 # See "Initializing by Instruction" section 40 | 41 | LCD_CGRAM = 0x40 # DB6: set CG RAM address 42 | LCD_DDRAM = 0x80 # DB7: set DD RAM address 43 | 44 | LCD_RS_CMD = 0 45 | LCD_RS_DATA = 1 46 | 47 | LCD_RW_WRITE = 0 48 | LCD_RW_READ = 1 49 | 50 | def __init__(self, num_lines, num_columns): 51 | self.num_lines = num_lines 52 | if self.num_lines > 4: 53 | self.num_lines = 4 54 | self.num_columns = num_columns 55 | if self.num_columns > 40: 56 | self.num_columns = 40 57 | self.cursor_x = 0 58 | self.cursor_y = 0 59 | self.backlight = True 60 | self.display_off() 61 | self.backlight_on() 62 | self.clear() 63 | self.hal_write_command(self.LCD_ENTRY_MODE | self.LCD_ENTRY_INC) 64 | self.hide_cursor() 65 | self.display_on() 66 | 67 | def clear(self): 68 | """Clears the LCD display and moves the cursor to the top left 69 | corner. 70 | """ 71 | self.hal_write_command(self.LCD_CLR) 72 | self.hal_write_command(self.LCD_HOME) 73 | self.cursor_x = 0 74 | self.cursor_y = 0 75 | 76 | def show_cursor(self): 77 | """Causes the cursor to be made visible.""" 78 | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | 79 | self.LCD_ON_CURSOR) 80 | 81 | def hide_cursor(self): 82 | """Causes the cursor to be hidden.""" 83 | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY) 84 | 85 | def blink_cursor_on(self): 86 | """Turns on the cursor, and makes it blink.""" 87 | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | 88 | self.LCD_ON_CURSOR | self.LCD_ON_BLINK) 89 | 90 | def blink_cursor_off(self): 91 | """Turns on the cursor, and makes it no blink (i.e. be solid).""" 92 | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | 93 | self.LCD_ON_CURSOR) 94 | 95 | def display_on(self): 96 | """Turns on (i.e. unblanks) the LCD.""" 97 | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY) 98 | 99 | def display_off(self): 100 | """Turns off (i.e. blanks) the LCD.""" 101 | self.hal_write_command(self.LCD_ON_CTRL) 102 | 103 | def backlight_on(self): 104 | """Turns the backlight on. 105 | 106 | This isn't really an LCD command, but some modules have backlight 107 | controls, so this allows the hal to pass through the command. 108 | """ 109 | self.backlight = True 110 | self.hal_backlight_on() 111 | 112 | def backlight_off(self): 113 | """Turns the backlight off. 114 | 115 | This isn't really an LCD command, but some modules have backlight 116 | controls, so this allows the hal to pass through the command. 117 | """ 118 | self.backlight = False 119 | self.hal_backlight_off() 120 | 121 | def move_to(self, cursor_x, cursor_y): 122 | """Moves the cursor position to the indicated position. The cursor 123 | position is zero based (i.e. cursor_x == 0 indicates first column). 124 | """ 125 | self.cursor_x = cursor_x 126 | self.cursor_y = cursor_y 127 | addr = cursor_x & 0x3f 128 | if cursor_y & 1: 129 | addr += 0x40 # Lines 1 & 3 add 0x40 130 | if cursor_y & 2: 131 | addr += 0x14 # Lines 2 & 3 add 0x14 132 | self.hal_write_command(self.LCD_DDRAM | addr) 133 | 134 | def putchar(self, char): 135 | """Writes the indicated character to the LCD at the current cursor 136 | position, and advances the cursor by one position. 137 | """ 138 | if char != '\n': 139 | self.hal_write_data(ord(char)) 140 | self.cursor_x += 1 141 | if self.cursor_x >= self.num_columns or char == '\n': 142 | self.cursor_x = 0 143 | self.cursor_y += 1 144 | if self.cursor_y >= self.num_lines: 145 | self.cursor_y = 0 146 | self.move_to(self.cursor_x, self.cursor_y) 147 | 148 | def putstr(self, string): 149 | """Write the indicated string to the LCD at the current cursor 150 | position and advances the cursor position appropriately. 151 | """ 152 | for char in string: 153 | self.putchar(char) 154 | 155 | def custom_char(self, location, charmap): 156 | """Write a character to one of the 8 CGRAM locations, available 157 | as chr(0) through chr(7). 158 | """ 159 | location &= 0x7 160 | self.hal_write_command(self.LCD_CGRAM | (location << 3)) 161 | time.sleep_us(40) 162 | for i in range(8): 163 | self.hal_write_data(charmap[i]) 164 | time.sleep_us(40) 165 | self.move_to(self.cursor_x, self.cursor_y) 166 | 167 | def hal_backlight_on(self): 168 | """Allows the hal layer to turn the backlight on. 169 | 170 | If desired, a derived HAL class will implement this function. 171 | """ 172 | pass 173 | 174 | def hal_backlight_off(self): 175 | """Allows the hal layer to turn the backlight off. 176 | 177 | If desired, a derived HAL class will implement this function. 178 | """ 179 | pass 180 | 181 | def hal_write_command(self, cmd): 182 | """Write a command to the LCD. 183 | 184 | It is expected that a derived HAL class will implement this 185 | function. 186 | """ 187 | raise NotImplementedError 188 | 189 | def hal_write_data(self, data): 190 | """Write data to the LCD. 191 | 192 | It is expected that a derived HAL class will implement this 193 | function. 194 | """ 195 | raise NotImplementedError 196 | -------------------------------------------------------------------------------- /DS1302/LCD1602_clock/main.py: -------------------------------------------------------------------------------- 1 | import DS1302 2 | from machine import I2C, Pin 3 | from esp8266_i2c_lcd import I2cLcd 4 | import time 5 | DEFAULT_I2C_ADDR = 0x27 6 | i2c = I2C(0,sda=Pin(0),scl=Pin(1),freq=400000) 7 | lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16) 8 | 9 | ds = DS1302.DS1302(Pin(2),Pin(3),Pin(4)) 10 | 11 | while True: 12 | d=ds.DateTime() 13 | date1=str(d[0])+"-"+str(d[1])+"-"+str(d[2])+" "+str(d[4])+":"+str(d[5]) 14 | lcd.putstr(date1) 15 | time.sleep(1) 16 | lcd.clear() 17 | d=ds.DateTime() 18 | date2=str(d[0])+"-"+str(d[1])+"-"+str(d[2])+" "+str(d[4])+" "+str(d[5]) 19 | lcd.putstr(date2) 20 | time.sleep(1) 21 | lcd.clear() 22 | -------------------------------------------------------------------------------- /DS1302/ds1302.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/DS1302/ds1302.jpg -------------------------------------------------------------------------------- /DS1302/main.py: -------------------------------------------------------------------------------- 1 | from machine import Pin 2 | import DS1302 3 | 4 | ds = DS1302.DS1302(Pin(2),Pin(3),Pin(4)) 5 | 6 | # CLK GPIO2 7 | # DAT GPIO3 8 | # RST GPIO4 9 | ds.DateTime() 10 | # 返回 :[2021, 1, 31, 7, 11, 10, 56] 11 | 12 | ds.Year() #获取今天的年份 13 | ds.Month() #获取今天的月份 14 | ds.Day() #获取今天的日期 15 | ds.Weekday() #获取当前周几 16 | ds.Hour() #获取当前小时 17 | ds.Minute() #获取当前分钟 18 | ds.Second() #获取当前的秒 19 | 20 | # 可以在以上函数中添加数字来设置时间 21 | # 例如 22 | ds.Year(2021) #设置今年为2021年 23 | ds.Day(2)#设置今天为2号 24 | -------------------------------------------------------------------------------- /HC-SR04/hcsr04.py: -------------------------------------------------------------------------------- 1 | import machine, time 2 | from machine import Pin 3 | 4 | __version__ = '0.2.0' 5 | __author__ = 'Roberto Sánchez' 6 | __license__ = "Apache License 2.0. https://www.apache.org/licenses/LICENSE-2.0" 7 | 8 | class HCSR04: 9 | """ 10 | Driver to use the untrasonic sensor HC-SR04. 11 | The sensor range is between 2cm and 4m. 12 | 13 | The timeouts received listening to echo pin are converted to OSError('Out of range') 14 | 15 | """ 16 | # echo_timeout_us is based in chip range limit (400cm) 17 | def __init__(self, trigger_pin, echo_pin, echo_timeout_us=500*2*30): 18 | """ 19 | trigger_pin: Output pin to send pulses 20 | echo_pin: Readonly pin to measure the distance. The pin should be protected with 1k resistor 21 | echo_timeout_us: Timeout in microseconds to listen to echo pin. 22 | By default is based in sensor limit range (4m) 23 | """ 24 | self.echo_timeout_us = echo_timeout_us 25 | # Init trigger pin (out) 26 | self.trigger = Pin(trigger_pin, mode=Pin.OUT, pull=None) 27 | self.trigger.value(0) 28 | 29 | # Init echo pin (in) 30 | self.echo = Pin(echo_pin, mode=Pin.IN, pull=None) 31 | 32 | def _send_pulse_and_wait(self): 33 | """ 34 | Send the pulse to trigger and listen on echo pin. 35 | We use the method `machine.time_pulse_us()` to get the microseconds until the echo is received. 36 | """ 37 | self.trigger.value(0) # Stabilize the sensor 38 | time.sleep_us(5) 39 | self.trigger.value(1) 40 | # Send a 10us pulse. 41 | time.sleep_us(10) 42 | self.trigger.value(0) 43 | try: 44 | pulse_time = machine.time_pulse_us(self.echo, 1, self.echo_timeout_us) 45 | return pulse_time 46 | except OSError as ex: 47 | if ex.args[0] == 110: # 110 = ETIMEDOUT 48 | raise OSError('Out of range') 49 | raise ex 50 | 51 | def distance_mm(self): 52 | """ 53 | Get the distance in milimeters without floating point operations. 54 | """ 55 | pulse_time = self._send_pulse_and_wait() 56 | 57 | # To calculate the distance we get the pulse_time and divide it by 2 58 | # (the pulse walk the distance twice) and by 29.1 becasue 59 | # the sound speed on air (343.2 m/s), that It's equivalent to 60 | # 0.34320 mm/us that is 1mm each 2.91us 61 | # pulse_time // 2 // 2.91 -> pulse_time // 5.82 -> pulse_time * 100 // 582 62 | mm = pulse_time * 100 // 582 63 | return mm 64 | 65 | def distance_cm(self): 66 | """ 67 | Get the distance in centimeters with floating point operations. 68 | It returns a float 69 | """ 70 | pulse_time = self._send_pulse_and_wait() 71 | 72 | # To calculate the distance we get the pulse_time and divide it by 2 73 | # (the pulse walk the distance twice) and by 29.1 becasue 74 | # the sound speed on air (343.2 m/s), that It's equivalent to 75 | # 0.034320 cm/us that is 1cm each 29.1us 76 | cms = (pulse_time / 2) / 29.1 77 | return cms 78 | 79 | -------------------------------------------------------------------------------- /HC-SR04/main.py: -------------------------------------------------------------------------------- 1 | from hcsr04 import HCSR04 2 | sensor = HCSR04(trigger_pin=0, echo_pin=1) 3 | 4 | 5 | # HCSR04 树莓派pico 6 | #vcc--- 5v VBUS 7 | #GND--- GND 8 | #Trig--- 0 9 | #echo--- 1 10 | sensor.distance_cm() 11 | sensor.distance_mm() -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /MG996R/main.py: -------------------------------------------------------------------------------- 1 | from machine import PWM,Pin 2 | 3 | s=PWM(Pin(0)) 4 | s.freq(50) 5 | 6 | 7 | s.duty_u16(1638) 8 | #0度 9 | s.duty_u16(4915) 10 | #90度 11 | s.duty_u16(8192) 12 | #180度 13 | 14 | #树莓派pico 一共可以使用16个PWM 本次使用GPIO0作为PWM 15 | #MG996R 为5V 工作电压 16 | # 红色 5V vbus 17 | # 棕线 GND 18 | # 黄线为 数据线 19 | # --------------- 20 | #20ms 一个周期 0.02==50hz 21 | #0.5ms--0度 22 | # 1.0ms--45度 23 | # 1.5ms--90度 24 | # 2.0ms--135度 25 | # 2.5ms--180度 26 | # 树莓派 为16位 27 | # 即 28 | # 0度 duty=65535*0.5/20 29 | # duty=1638 30 | 31 | # 90度 32 | # duty=65535*1.5/20 33 | # duty=4915 34 | 35 | # 180度 36 | # duty=65535*2.5/20 37 | # duty=8192 38 | 39 | -------------------------------------------------------------------------------- /NEC_遥控/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/NEC_遥控/.keep -------------------------------------------------------------------------------- /NEC_遥控/README.md: -------------------------------------------------------------------------------- 1 | ## NEC串口模块 2 | 3 | 4 | 接收 940nm 的 38K NEC 编码 5 | 6 | 7 | 波特率9600 8 | 9 | 直接使用学习功能 学习发射 10 | 11 | 12 | ### 接收NEC命令 13 | 14 | 用户码1 用户码2 命令码 15 | 16 | 17 | 18 | ### 发射 19 | 20 | 21 | A1(默认) F1(操作码,F1为发射) 用户码1 用户码2 命令码 22 | 23 | 24 | 25 | 26 | ### 发射后接收信息分析 27 | 28 | F1 发射成功 29 | F2 串口地址修改成功 30 | F3 波特率设置成功 31 | 32 | 33 | -------------------------------------------------------------------------------- /Pico-R3-A4-Pinout.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/Pico-R3-A4-Pinout.pdf -------------------------------------------------------------------------------- /Pin/main.py: -------------------------------------------------------------------------------- 1 | from machine import Pin 2 | 3 | l=Pin(0,Pin.OUT) 4 | l.high() 5 | l.low() 6 | 7 | l.value() 8 | l.value(1) 9 | l.value(0) 10 | 11 | #按钮 12 | 13 | #GPIO 1 和 按钮 和 GND相连 14 | 15 | 16 | b=Pin(1,Pin.OUT) 17 | b.value(1) 18 | 19 | b.value() 20 | 21 | #按钮按下 22 | 23 | b.value() 24 | 25 | 26 | # 应用 按钮按下灯亮 27 | 28 | 29 | while 1: 30 | if b.value(): 31 | l.value(1) 32 | else: 33 | l.value(0) 34 | 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | raspberrypi PICO 下 micropython的学习 2 | 3 | 目前基于micropython1.18 版本,请使用的同学先将pico刷成该版本 4 | 5 | 6 | 7 | 请使用Thonny 来编程及操作 8 | 9 | 10 | -------------------------------------------------------------------------------- /SG90/main.py: -------------------------------------------------------------------------------- 1 | from machine import PWM,Pin 2 | 3 | s=PWM(Pin(0)) 4 | s.freq(50) 5 | s.duty_u16(4915) 6 | 7 | 8 | 9 | #树莓派pico 一共可以使用16个PWM 本次使用GPIO0作为PWM 10 | #SG90 为5V 工作电压 11 | # 红色 5V vbus 12 | # 棕线 GND 13 | # 黄线为 数据线 14 | # --------------- 15 | #20ms 一个周期 0.02==50hz 16 | #0.5ms--0度 17 | # 1.0ms--45度 18 | # 1.5ms--90度 19 | # 2.0ms--135度 20 | # 2.5ms--180度 21 | # 树莓派 为16位 22 | # 即 23 | # 0度 duty=65535*0.5/20 24 | # duty=1638 25 | 26 | # 90度 27 | # duty=65535*1.5/20 28 | # duty=4915 29 | 30 | # 180度 31 | # duty=65535*2.5/20 32 | # duty=8192` 33 | 34 | -------------------------------------------------------------------------------- /ULN2003/Stepper.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | # only test for uln2003 4 | class Stepper: 5 | FULL_ROTATION = int(4075.7728395061727 / 8) # http://www.jangeox.be/2013/10/stepper-motor-28byj-48_25.html 6 | 7 | HALF_STEP = [ 8 | [0, 0, 0, 1], 9 | [0, 0, 1, 1], 10 | [0, 0, 1, 0], 11 | [0, 1, 1, 0], 12 | [0, 1, 0, 0], 13 | [1, 1, 0, 0], 14 | [1, 0, 0, 0], 15 | [1, 0, 0, 1], 16 | ] 17 | 18 | FULL_STEP = [ 19 | [1, 0, 1, 0], 20 | [0, 1, 1, 0], 21 | [0, 1, 0, 1], 22 | [1, 0, 0, 1] 23 | ] 24 | def __init__(self, mode, pin1, pin2, pin3, pin4, delay): 25 | if mode=='FULL_STEP': 26 | self.mode = self.FULL_STEP 27 | else: 28 | self.mode = self.HALF_STEP 29 | self.pin1 = pin1 30 | self.pin2 = pin2 31 | self.pin3 = pin3 32 | self.pin4 = pin4 33 | self.delay = delay # Recommend 10+ for FULL_STEP, 1 is OK for HALF_STEP 34 | 35 | # Initialize all to 0 36 | self.reset() 37 | 38 | def step(self, count, direction=1): 39 | """Rotate count steps. direction = -1 means backwards""" 40 | for x in range(count): 41 | for bit in self.mode[::direction]: 42 | self.pin1(bit[0]) 43 | self.pin2(bit[1]) 44 | self.pin3(bit[2]) 45 | self.pin4(bit[3]) 46 | time.sleep_ms(self.delay) 47 | self.reset() 48 | def angle(self, r, direction=1): 49 | self.step(int(self.FULL_ROTATION * r / 360), direction) 50 | def reset(self): 51 | # Reset to 0, no holding, these are geared, you can't move them 52 | self.pin1(0) 53 | self.pin2(0) 54 | self.pin3(0) 55 | self.pin4(0) 56 | 57 | def create(pin1, pin2, pin3, pin4, delay=2, mode='HALF_STEP'): 58 | return Stepper(mode, pin1, pin2, pin3, pin4, delay) 59 | -------------------------------------------------------------------------------- /ULN2003/main.py: -------------------------------------------------------------------------------- 1 | import Stepper 2 | from machine import Pin 3 | s1 = Stepper.create(Pin(0,Pin.OUT),Pin(1,Pin.OUT),Pin(2,Pin.OUT),Pin(3,Pin.OUT), delay=2) 4 | 5 | # 接线 6 | # ULN2003-树莓派Pico 7 | # IN1-GPIO0 8 | # IN2-GPIO1 9 | # IN3-GPIO2 10 | # IN4-GPIO3 11 | 12 | 13 | s1.step(100) 14 | 15 | s1.step(100,-1) 16 | 17 | s1.angle(180) 18 | 19 | s1.angle(360,-1) 20 | 21 | 22 | # step设置509差不多angle设置360 -------------------------------------------------------------------------------- /VL53L0X/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/VL53L0X/.keep -------------------------------------------------------------------------------- /VL53L0X/README.md: -------------------------------------------------------------------------------- 1 | VL53L0X 激光测距传感器 2 | 3 | 0-2m 范围 4 | ## demo 5 | ```python 6 | from machine import Pin 7 | from machine import SoftI2c as I2C 8 | import vl53l0x 9 | 10 | i2c = I2C(sda=Pin(4),scl=Pin(5), freq = 400000) 11 | vl53 = vl53l0x.VL53L0X(i2c) 12 | 13 | vl53.measurement_timing_budget = 20000 # microseconds 14 | 15 | vl53.start_continuous() 16 | 17 | while True: 18 | print("Range: {0}mm".format(vl53.range)) 19 | time.sleep(1) 20 | ``` 21 | 22 | 库:https://github.com/kapetan/MicroPython_VL53L0X 23 | -------------------------------------------------------------------------------- /bmp180/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/bmp180/.keep -------------------------------------------------------------------------------- /bmp180/bmp180.py: -------------------------------------------------------------------------------- 1 | ''' 2 | bmp180 is a micropython module for the Bosch BMP180 sensor. It measures 3 | temperature as well as pressure, with a high enough resolution to calculate 4 | altitude. 5 | Breakoutboard: http://www.adafruit.com/products/1603 6 | data-sheet: http://ae-bst.resource.bosch.com/media/products/dokumente/ 7 | bmp180/BST-BMP180-DS000-09.pdf 8 | The MIT License (MIT) 9 | Copyright (c) 2014 Sebastian Plamauer, oeplse@gmail.com 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | The above copyright notice and this permission notice shall be included in 17 | all copies or substantial portions of the Software. 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | THE SOFTWARE. 25 | ''' 26 | 27 | from ustruct import unpack as unp 28 | from machine import I2C, Pin 29 | import math 30 | import time 31 | 32 | # BMP180 class 33 | class BMP180(): 34 | ''' 35 | Module for the BMP180 pressure sensor. 36 | ''' 37 | 38 | _bmp_addr = 119 # adress of BMP180 is hardcoded on the sensor 39 | 40 | # init 41 | def __init__(self, i2c_bus): 42 | 43 | # create i2c obect 44 | _bmp_addr = self._bmp_addr 45 | self._bmp_i2c = i2c_bus 46 | self._bmp_i2c.start() 47 | self.chip_id = self._bmp_i2c.readfrom_mem(_bmp_addr, 0xD0, 2) 48 | # read calibration data from EEPROM 49 | self._AC1 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xAA, 2))[0] 50 | self._AC2 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xAC, 2))[0] 51 | self._AC3 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xAE, 2))[0] 52 | self._AC4 = unp('>H', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB0, 2))[0] 53 | self._AC5 = unp('>H', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB2, 2))[0] 54 | self._AC6 = unp('>H', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB4, 2))[0] 55 | self._B1 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB6, 2))[0] 56 | self._B2 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB8, 2))[0] 57 | self._MB = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xBA, 2))[0] 58 | self._MC = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xBC, 2))[0] 59 | self._MD = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xBE, 2))[0] 60 | 61 | # settings to be adjusted by user 62 | self.oversample_setting = 3 63 | self.baseline = 101325.0 64 | 65 | # output raw 66 | self.UT_raw = None 67 | self.B5_raw = None 68 | self.MSB_raw = None 69 | self.LSB_raw = None 70 | self.XLSB_raw = None 71 | self.gauge = self.makegauge() # Generator instance 72 | for _ in range(128): 73 | next(self.gauge) 74 | time.sleep_ms(1) 75 | 76 | def compvaldump(self): 77 | ''' 78 | Returns a list of all compensation values 79 | ''' 80 | return [self._AC1, self._AC2, self._AC3, self._AC4, self._AC5, self._AC6, 81 | self._B1, self._B2, self._MB, self._MC, self._MD, self.oversample_setting] 82 | 83 | # gauge raw 84 | def makegauge(self): 85 | ''' 86 | Generator refreshing the raw measurments. 87 | ''' 88 | delays = (5, 8, 14, 25) 89 | while True: 90 | self._bmp_i2c.writeto_mem(self._bmp_addr, 0xF4, bytearray([0x2E])) 91 | t_start = time.ticks_ms() 92 | while (time.ticks_ms() - t_start) <= 5: # 5mS delay 93 | yield None 94 | try: 95 | self.UT_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF6, 2) 96 | except: 97 | yield None 98 | self._bmp_i2c.writeto_mem(self._bmp_addr, 0xF4, bytearray([0x34+(self.oversample_setting << 6)])) 99 | t_pressure_ready = delays[self.oversample_setting] 100 | t_start = time.ticks_ms() 101 | while (time.ticks_ms() - t_start) <= t_pressure_ready: 102 | yield None 103 | try: 104 | self.MSB_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF6, 1) 105 | self.LSB_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF7, 1) 106 | self.XLSB_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF8, 1) 107 | except: 108 | yield None 109 | yield True 110 | 111 | def blocking_read(self): 112 | if next(self.gauge) is not None: # Discard old data 113 | pass 114 | while next(self.gauge) is None: 115 | pass 116 | 117 | @property 118 | def oversample_sett(self): 119 | return self.oversample_setting 120 | 121 | @oversample_sett.setter 122 | def oversample_sett(self, value): 123 | if value in range(4): 124 | self.oversample_setting = value 125 | else: 126 | print('oversample_sett can only be 0, 1, 2 or 3, using 3 instead') 127 | self.oversample_setting = 3 128 | 129 | @property 130 | def temperature(self): 131 | ''' 132 | Temperature in degree C. 133 | ''' 134 | next(self.gauge) 135 | try: 136 | UT = unp('>H', self.UT_raw)[0] 137 | except: 138 | return 0.0 139 | X1 = (UT-self._AC6)*self._AC5/2**15 140 | X2 = self._MC*2**11/(X1+self._MD) 141 | self.B5_raw = X1+X2 142 | return (((X1+X2)+8)/2**4)/10 143 | 144 | @property 145 | def pressure(self): 146 | ''' 147 | Pressure in mbar. 148 | ''' 149 | next(self.gauge) 150 | self.temperature # Populate self.B5_raw 151 | try: 152 | MSB = unp('B', self.MSB_raw)[0] 153 | LSB = unp('B', self.LSB_raw)[0] 154 | XLSB = unp('B', self.XLSB_raw)[0] 155 | except: 156 | return 0.0 157 | UP = ((MSB << 16)+(LSB << 8)+XLSB) >> (8-self.oversample_setting) 158 | B6 = self.B5_raw-4000 159 | X1 = (self._B2*(B6**2/2**12))/2**11 160 | X2 = self._AC2*B6/2**11 161 | X3 = X1+X2 162 | B3 = ((int((self._AC1*4+X3)) << self.oversample_setting)+2)/4 163 | X1 = self._AC3*B6/2**13 164 | X2 = (self._B1*(B6**2/2**12))/2**16 165 | X3 = ((X1+X2)+2)/2**2 166 | B4 = abs(self._AC4)*(X3+32768)/2**15 167 | B7 = (abs(UP)-B3) * (50000 >> self.oversample_setting) 168 | if B7 < 0x80000000: 169 | pressure = (B7*2)/B4 170 | else: 171 | pressure = (B7/B4)*2 172 | X1 = (pressure/2**8)**2 173 | X1 = (X1*3038)/2**16 174 | X2 = (-7357*pressure)/2**16 175 | return pressure+(X1+X2+3791)/2**4 176 | 177 | @property 178 | def altitude(self): 179 | ''' 180 | Altitude in m. 181 | ''' 182 | try: 183 | p = -7990.0*math.log(self.pressure/self.baseline) 184 | except: 185 | p = 0.0 186 | return 187 | -------------------------------------------------------------------------------- /bmp180/demo.py: -------------------------------------------------------------------------------- 1 | from bmp180 import BMP180 2 | from machine import Pin 3 | from machine import SoftI2C as I2C 4 | from time import sleep 5 | 6 | i2c = I2C(scl=Pin(1), sda=Pin(0),freq=100000) 7 | bmp = BMP180(i2c) 8 | 9 | 10 | print(bmp.temperature) 11 | print(bmp.pressure) -------------------------------------------------------------------------------- /bmp180/readme.md: -------------------------------------------------------------------------------- 1 | BMP180 2 | 3 | |BMP180|PICO| 4 | |-|-| 5 | |VCC|vcc 3v3-OUT| 6 | |GND|GND| 7 | |SDA|0| 8 | |SCL|1| 9 | 10 | 11 | 注意树莓派pico (1.18) 必须使用 SoftI2C 才能使用。 12 | freq=100000 13 | 14 | ``` 15 | from bmp180 import BMP180 16 | from machine import Pin 17 | from machine import SoftI2C as I2C 18 | from time import sleep 19 | 20 | i2c = I2C(scl=Pin(1), sda=Pin(0),freq=100000) 21 | bmp = BMP180(i2c) 22 | 23 | 24 | print(bmp.temperature) #温度 25 | print(bmp.pressure) #气压 26 | 27 | ``` 28 | 29 | 库来自于:https://github.com/micropython-IMU/micropython-bmp180 30 | -------------------------------------------------------------------------------- /bmp280/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/bmp280/.keep -------------------------------------------------------------------------------- /bmp280/bmp280.py: -------------------------------------------------------------------------------- 1 | from micropython import const 2 | from ustruct import unpack as unp 3 | 4 | # Author David Stenwall Wahlund (david at dafnet.se) 5 | 6 | # Power Modes 7 | BMP280_POWER_SLEEP = const(0) 8 | BMP280_POWER_FORCED = const(1) 9 | BMP280_POWER_NORMAL = const(3) 10 | 11 | BMP280_SPI3W_ON = const(1) 12 | BMP280_SPI3W_OFF = const(0) 13 | 14 | BMP280_TEMP_OS_SKIP = const(0) 15 | BMP280_TEMP_OS_1 = const(1) 16 | BMP280_TEMP_OS_2 = const(2) 17 | BMP280_TEMP_OS_4 = const(3) 18 | BMP280_TEMP_OS_8 = const(4) 19 | BMP280_TEMP_OS_16 = const(5) 20 | 21 | BMP280_PRES_OS_SKIP = const(0) 22 | BMP280_PRES_OS_1 = const(1) 23 | BMP280_PRES_OS_2 = const(2) 24 | BMP280_PRES_OS_4 = const(3) 25 | BMP280_PRES_OS_8 = const(4) 26 | BMP280_PRES_OS_16 = const(5) 27 | 28 | # Standby settings in ms 29 | BMP280_STANDBY_0_5 = const(0) 30 | BMP280_STANDBY_62_5 = const(1) 31 | BMP280_STANDBY_125 = const(2) 32 | BMP280_STANDBY_250 = const(3) 33 | BMP280_STANDBY_500 = const(4) 34 | BMP280_STANDBY_1000 = const(5) 35 | BMP280_STANDBY_2000 = const(6) 36 | BMP280_STANDBY_4000 = const(7) 37 | 38 | # IIR Filter setting 39 | BMP280_IIR_FILTER_OFF = const(0) 40 | BMP280_IIR_FILTER_2 = const(1) 41 | BMP280_IIR_FILTER_4 = const(2) 42 | BMP280_IIR_FILTER_8 = const(3) 43 | BMP280_IIR_FILTER_16 = const(4) 44 | 45 | # Oversampling setting 46 | BMP280_OS_ULTRALOW = const(0) 47 | BMP280_OS_LOW = const(1) 48 | BMP280_OS_STANDARD = const(2) 49 | BMP280_OS_HIGH = const(3) 50 | BMP280_OS_ULTRAHIGH = const(4) 51 | 52 | # Oversampling matrix 53 | # (PRESS_OS, TEMP_OS, sample time in ms) 54 | _BMP280_OS_MATRIX = [ 55 | [BMP280_PRES_OS_1, BMP280_TEMP_OS_1, 7], 56 | [BMP280_PRES_OS_2, BMP280_TEMP_OS_1, 9], 57 | [BMP280_PRES_OS_4, BMP280_TEMP_OS_1, 14], 58 | [BMP280_PRES_OS_8, BMP280_TEMP_OS_1, 23], 59 | [BMP280_PRES_OS_16, BMP280_TEMP_OS_2, 44] 60 | ] 61 | 62 | # Use cases 63 | BMP280_CASE_HANDHELD_LOW = const(0) 64 | BMP280_CASE_HANDHELD_DYN = const(1) 65 | BMP280_CASE_WEATHER = const(2) 66 | BMP280_CASE_FLOOR = const(3) 67 | BMP280_CASE_DROP = const(4) 68 | BMP280_CASE_INDOOR = const(5) 69 | 70 | _BMP280_CASE_MATRIX = [ 71 | [BMP280_POWER_NORMAL, BMP280_OS_ULTRAHIGH, BMP280_IIR_FILTER_4, BMP280_STANDBY_62_5], 72 | [BMP280_POWER_NORMAL, BMP280_OS_STANDARD, BMP280_IIR_FILTER_16, BMP280_STANDBY_0_5], 73 | [BMP280_POWER_FORCED, BMP280_OS_ULTRALOW, BMP280_IIR_FILTER_OFF, BMP280_STANDBY_0_5], 74 | [BMP280_POWER_NORMAL, BMP280_OS_STANDARD, BMP280_IIR_FILTER_4, BMP280_STANDBY_125], 75 | [BMP280_POWER_NORMAL, BMP280_OS_LOW, BMP280_IIR_FILTER_OFF, BMP280_STANDBY_0_5], 76 | [BMP280_POWER_NORMAL, BMP280_OS_ULTRAHIGH, BMP280_IIR_FILTER_16, BMP280_STANDBY_0_5] 77 | ] 78 | 79 | _BMP280_REGISTER_ID = const(0xD0) 80 | _BMP280_REGISTER_RESET = const(0xE0) 81 | _BMP280_REGISTER_STATUS = const(0xF3) 82 | _BMP280_REGISTER_CONTROL = const(0xF4) 83 | _BMP280_REGISTER_CONFIG = const(0xF5) # IIR filter config 84 | 85 | _BMP280_REGISTER_DATA = const(0xF7) 86 | 87 | 88 | class BMP280: 89 | def __init__(self, i2c_bus, addr=0x76, use_case=BMP280_CASE_HANDHELD_DYN): 90 | self._bmp_i2c = i2c_bus 91 | self._i2c_addr = addr 92 | 93 | # read calibration data 94 | # < little-endian 95 | # H unsigned short 96 | # h signed short 97 | self._T1 = unp('> 4) 139 | self._t_raw = (d[3] << 12) + (d[4] << 4) + (d[5] >> 4) 140 | 141 | self._t_fine = 0 142 | self._t = 0 143 | self._p = 0 144 | 145 | def reset(self): 146 | self._write(_BMP280_REGISTER_RESET, 0xB6) 147 | 148 | def load_test_calibration(self): 149 | self._T1 = 27504 150 | self._T2 = 26435 151 | self._T3 = -1000 152 | self._P1 = 36477 153 | self._P2 = -10685 154 | self._P3 = 3024 155 | self._P4 = 2855 156 | self._P5 = 140 157 | self._P6 = -7 158 | self._P7 = 15500 159 | self._P8 = -14600 160 | self._P9 = 6000 161 | 162 | def load_test_data(self): 163 | self._t_raw = 519888 164 | self._p_raw = 415148 165 | 166 | def print_calibration(self): 167 | print("T1: {} {}".format(self._T1, type(self._T1))) 168 | print("T2: {} {}".format(self._T2, type(self._T2))) 169 | print("T3: {} {}".format(self._T3, type(self._T3))) 170 | print("P1: {} {}".format(self._P1, type(self._P1))) 171 | print("P2: {} {}".format(self._P2, type(self._P2))) 172 | print("P3: {} {}".format(self._P3, type(self._P3))) 173 | print("P4: {} {}".format(self._P4, type(self._P4))) 174 | print("P5: {} {}".format(self._P5, type(self._P5))) 175 | print("P6: {} {}".format(self._P6, type(self._P6))) 176 | print("P7: {} {}".format(self._P7, type(self._P7))) 177 | print("P8: {} {}".format(self._P8, type(self._P8))) 178 | print("P9: {} {}".format(self._P9, type(self._P9))) 179 | 180 | def _calc_t_fine(self): 181 | # From datasheet page 22 182 | self._gauge() 183 | if self._t_fine == 0: 184 | var1 = (((self._t_raw >> 3) - (self._T1 << 1)) * self._T2) >> 11 185 | var2 = (((((self._t_raw >> 4) - self._T1) 186 | * ((self._t_raw >> 4) 187 | - self._T1)) >> 12) 188 | * self._T3) >> 14 189 | self._t_fine = var1 + var2 190 | 191 | @property 192 | def temperature(self): 193 | self._calc_t_fine() 194 | if self._t == 0: 195 | self._t = ((self._t_fine * 5 + 128) >> 8) / 100. 196 | return self._t 197 | 198 | @property 199 | def pressure(self): 200 | # From datasheet page 22 201 | self._calc_t_fine() 202 | if self._p == 0: 203 | var1 = self._t_fine - 128000 204 | var2 = var1 * var1 * self._P6 205 | var2 = var2 + ((var1 * self._P5) << 17) 206 | var2 = var2 + (self._P4 << 35) 207 | var1 = ((var1 * var1 * self._P3) >> 8) + ((var1 * self._P2) << 12) 208 | var1 = (((1 << 47) + var1) * self._P1) >> 33 209 | 210 | if var1 == 0: 211 | return 0 212 | 213 | p = 1048576 - self._p_raw 214 | p = int((((p << 31) - var2) * 3125) / var1) 215 | var1 = (self._P9 * (p >> 13) * (p >> 13)) >> 25 216 | var2 = (self._P8 * p) >> 19 217 | 218 | p = ((p + var1 + var2) >> 8) + (self._P7 << 4) 219 | self._p = p / 256.0 220 | return self._p 221 | 222 | def _write_bits(self, address, value, length, shift=0): 223 | d = self._read(address)[0] 224 | m = int('1' * length, 2) << shift 225 | d &= ~m 226 | d |= m & value << shift 227 | self._write(address, d) 228 | 229 | def _read_bits(self, address, length, shift=0): 230 | d = self._read(address)[0] 231 | return d >> shift & int('1' * length, 2) 232 | 233 | @property 234 | def standby(self): 235 | return self._read_bits(_BMP280_REGISTER_CONFIG, 3, 5) 236 | 237 | @standby.setter 238 | def standby(self, v): 239 | assert 0 <= v <= 7 240 | self._write_bits(_BMP280_REGISTER_CONFIG, v, 3, 5) 241 | 242 | @property 243 | def iir(self): 244 | return self._read_bits(_BMP280_REGISTER_CONFIG, 3, 2) 245 | 246 | @iir.setter 247 | def iir(self, v): 248 | assert 0 <= v <= 4 249 | self._write_bits(_BMP280_REGISTER_CONFIG, v, 3, 2) 250 | 251 | @property 252 | def spi3w(self): 253 | return self._read_bits(_BMP280_REGISTER_CONFIG, 1) 254 | 255 | @spi3w.setter 256 | def spi3w(self, v): 257 | assert v in (0, 1) 258 | self._write_bits(_BMP280_REGISTER_CONFIG, v, 1) 259 | 260 | @property 261 | def temp_os(self): 262 | return self._read_bits(_BMP280_REGISTER_CONTROL, 3, 5) 263 | 264 | @temp_os.setter 265 | def temp_os(self, v): 266 | assert 0 <= v <= 5 267 | self._write_bits(_BMP280_REGISTER_CONTROL, v, 3, 5) 268 | 269 | @property 270 | def press_os(self): 271 | return self._read_bits(_BMP280_REGISTER_CONTROL, 3, 2) 272 | 273 | @press_os.setter 274 | def press_os(self, v): 275 | assert 0 <= v <= 5 276 | self._write_bits(_BMP280_REGISTER_CONTROL, v, 3, 2) 277 | 278 | @property 279 | def power_mode(self): 280 | return self._read_bits(_BMP280_REGISTER_CONTROL, 2) 281 | 282 | @power_mode.setter 283 | def power_mode(self, v): 284 | assert 0 <= v <= 3 285 | self._write_bits(_BMP280_REGISTER_CONTROL, v, 2) 286 | 287 | @property 288 | def is_measuring(self): 289 | return bool(self._read_bits(_BMP280_REGISTER_STATUS, 1, 3)) 290 | 291 | @property 292 | def is_updating(self): 293 | return bool(self._read_bits(_BMP280_REGISTER_STATUS, 1)) 294 | 295 | @property 296 | def chip_id(self): 297 | return self._read(_BMP280_REGISTER_ID, 2) 298 | 299 | @property 300 | def in_normal_mode(self): 301 | return self.power_mode == BMP280_POWER_NORMAL 302 | 303 | def force_measure(self): 304 | self.power_mode = BMP280_POWER_FORCED 305 | 306 | def normal_measure(self): 307 | self.power_mode = BMP280_POWER_NORMAL 308 | 309 | def sleep(self): 310 | self.power_mode = BMP280_POWER_SLEEP 311 | 312 | def use_case(self, uc): 313 | assert 0 <= uc <= 5 314 | pm, oss, iir, sb = _BMP280_CASE_MATRIX[uc] 315 | p_os, t_os, self.read_wait_ms = _BMP280_OS_MATRIX[oss] 316 | self._write(_BMP280_REGISTER_CONFIG, (iir << 2) + (sb << 5)) 317 | self._write(_BMP280_REGISTER_CONTROL, pm + (p_os << 2) + (t_os << 5)) 318 | 319 | def oversample(self, oss): 320 | assert 0 <= oss <= 4 321 | p_os, t_os, self.read_wait_ms = _BMP280_OS_MATRIX[oss] 322 | self._write_bits(_BMP280_REGISTER_CONTROL, p_os + (t_os << 3), 2) 323 | -------------------------------------------------------------------------------- /bmp280/main.py: -------------------------------------------------------------------------------- 1 | from machine import I2C,Pin 2 | from bmp280 import * 3 | 4 | bus = I2C(0,sda=Pin(0),scl=Pin(1), freq=400000) 5 | bmp = BMP280(bus) 6 | 7 | print(bmp.temperature) 8 | print(bmp.pressure) 9 | -------------------------------------------------------------------------------- /bmp280/readme.md: -------------------------------------------------------------------------------- 1 | 请务必使用micropython1.18固件,1.19使用时候无法操作 2 | 3 | IIC 4 | 5 | |BMP180|PICO| 6 | |-|-| 7 | |SCL|1| 8 | |SDA|0| 9 | |CSB|VCC 3v3-OUT| 10 | |SDO|GND| 11 | 12 | ``` 13 | from machine import I2C,Pin 14 | from bmp280 import * 15 | 16 | bus = I2C(0,sda=Pin(0),scl=Pin(1), freq=400000) 17 | bmp = BMP280(bus) 18 | 19 | print(bmp.temperature) #温度 20 | print(bmp.pressure) #气压 21 | ``` 22 | 23 | 库来自: https://github.com/Dafvid/micropython-bmp280 24 | -------------------------------------------------------------------------------- /empty/boot.py: -------------------------------------------------------------------------------- 1 | # -------------------------------------------------------------------------------- /empty/main.py: -------------------------------------------------------------------------------- 1 | # -------------------------------------------------------------------------------- /esp01/esp01.py: -------------------------------------------------------------------------------- 1 | from machine import UART 2 | import utime 3 | 4 | """ 5 | The MicroPython port for Pi Pico has no timeout for readline() at this moment. 6 | We use this hack to make sure it won't get stuck forever. 7 | """ 8 | 9 | class uartTimeOut(UART): 10 | 11 | def readline(self, timeOut=100): 12 | if timeOut is None: 13 | return super().readline() 14 | else: 15 | now = utime.ticks_ms() 16 | data = b'' 17 | while True: 18 | if utime.ticks_ms()-now > timeOut: 19 | break 20 | else: 21 | if super().any(): 22 | _d = super().read(1) 23 | data += _d 24 | if "\n" in _d: 25 | break 26 | return data 27 | 28 | class wifi(object): 29 | def __init__(self,uart=1,baud_rate=9600): 30 | self.uart=uartTimeOut(uart, baud_rate) 31 | def _set_command(self,command): 32 | re=self.uart.write(command) 33 | if self.uart.any(): 34 | return re.readline() 35 | def set_cwmode(self,inx=1): 36 | cod="AT+CWMODE="+str(inx)+"\r" 37 | return self._set_command(cod) 38 | def wifi(self,name,password): 39 | cod='AT+CWJAP="%s","%s"' % (name,password) 40 | return self._set_command(cod) 41 | def cipmux(self,num=0): 42 | cod='AT+CIPMUX="%s"' % (num,) 43 | return self._set_command(cod) 44 | def http(self,url,) 45 | 46 | -------------------------------------------------------------------------------- /gps/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/gps/.keep -------------------------------------------------------------------------------- /gps/demo.py: -------------------------------------------------------------------------------- 1 | from machine import UART,Pin 2 | import time 3 | from neo6 import GPS 4 | 5 | uart=UART(0,9600) 6 | 7 | 8 | gps=GPS(uart) 9 | 10 | while True: 11 | gps.getGPS() 12 | if(gps.FIX_STATUS == True): 13 | print("----------------------") 14 | print("维度: ","北纬" if "-" not in gps.latitude else "南纬",gps.latitude) 15 | print("经度: ","东经" if "-" not in gps.longitude else "西经",gps.longitude) 16 | print("卫星数: " ,gps.satellites) 17 | print("GPS时间: ",gps.GPStime) 18 | print("----------------------") 19 | gps.FIX_STATUS = False 20 | 21 | if(gps.TIMEOUT == True): 22 | print("无GPS信号") 23 | gps.TIMEOUT = False 24 | -------------------------------------------------------------------------------- /gps/neo6.py: -------------------------------------------------------------------------------- 1 | 2 | import time 3 | class GPS(object): 4 | def __init__(self,gpsModule): 5 | self.buff = bytearray(255) 6 | self.TIMEOUT = False 7 | self.FIX_STATUS = False 8 | self.latitude = "" 9 | self.longitude = "" 10 | self.satellites = "" 11 | self.GPStime = "" 12 | self.gpsModule=gpsModule 13 | def getGPS(self): 14 | # global FIX_STATUS, TIMEOUT, latitude, longitude, satellites, GPStime 15 | gpsModule=self.gpsModule 16 | timeout = time.time() + 8 17 | while True: 18 | gpsModule.readline() 19 | buff = str(gpsModule.readline()) 20 | parts = buff.split(',') 21 | 22 | if (parts[0] == "b'$GPGGA" and len(parts) == 15): 23 | if(parts[1] and parts[2] and parts[3] and parts[4] and parts[5] and parts[6] and parts[7]): 24 | # print(buff) 25 | 26 | self.latitude = self.convertToDegree(parts[2]) 27 | if (parts[3] == 'S'): 28 | self.latitude = -self.latitude 29 | self.longitude = self.convertToDegree(parts[4]) 30 | if (parts[5] == 'W'): 31 | self.longitude = -self.longitude 32 | self.satellites = parts[7] 33 | self.GPStime = parts[1][0:2] + ":" + parts[1][2:4] + ":" + parts[1][4:6] 34 | self.FIX_STATUS = True 35 | break 36 | 37 | if (time.time() > timeout): 38 | self.TIMEOUT = True 39 | break 40 | time.sleep_ms(500) 41 | 42 | def convertToDegree(self,RawDegrees): 43 | 44 | RawAsFloat = float(RawDegrees) 45 | firstdigits = int(RawAsFloat/100) 46 | nexttwodigits = RawAsFloat - float(firstdigits*100) 47 | 48 | Converted = float(firstdigits + nexttwodigits/60.0) 49 | Converted = '{0:.6f}'.format(Converted) 50 | return str(Converted) 51 | 52 | -------------------------------------------------------------------------------- /gps/readme.md: -------------------------------------------------------------------------------- 1 | ### 接线 2 | |GPS|PICO| 3 | |-|-| 4 | |3v3|3v3| 5 | |gnd|gnd| 6 | |tx|1| 7 | |rx|0| 8 | 9 | ### demo 10 | demo.py 11 | 12 | 来自:https://microcontrollerslab.com/neo-6m-gps-module-esp32-micropython/ -------------------------------------------------------------------------------- /hc12/HC-HID_V1.2.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/hc12/HC-HID_V1.2.exe -------------------------------------------------------------------------------- /hc12/MYUART.py: -------------------------------------------------------------------------------- 1 | from time import sleep_us,ticks_ms 2 | from machine import UART 3 | class myUART(UART): 4 | def readUntil(self, termination, maxlen=-1, includeTermination=True): 5 | result = '' 6 | start = ticks_ms() 7 | while maxlen < 0 or len(result) < maxlen : 8 | if self.any(): 9 | #print("here") 10 | result += chr(self.read(1)[0]) 11 | #print(result) 12 | if result.endswith(termination): 13 | if not includeTermination: 14 | result = result[:-len(termination)] 15 | break 16 | sleep_us(10) 17 | return result 18 | def readall(self): 19 | e='' 20 | while self.any(): 21 | e=e+chr(self.read(1)[0]) 22 | self.allr=e 23 | return e 24 | -------------------------------------------------------------------------------- /hc12/uar.py: -------------------------------------------------------------------------------- 1 | from machine import Pin 2 | from MYUART import myUART 3 | import time 4 | 5 | c=Pin(2,Pin.OUT) 6 | uart = myUART(0, baudrate=9600, tx=Pin(0), rx=Pin(1), bits=8, parity=None, stop=1) 7 | c.value(0) 8 | uart.write("AT C007") 9 | time.sleep(1) 10 | c.value(1) 11 | 12 | 13 | -------------------------------------------------------------------------------- /ina3221/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/ina3221/.keep -------------------------------------------------------------------------------- /ina3221/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/ina3221/1.jpg -------------------------------------------------------------------------------- /ina3221/address.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/ina3221/address.jpg -------------------------------------------------------------------------------- /ina3221/ex3221.py: -------------------------------------------------------------------------------- 1 | import time 2 | from machine import Pin, I2C 3 | import ina3221 4 | 5 | i2c = I2C(0,scl=Pin(1), sda=Pin(0), freq=100000) 6 | ina = ina3221.INA3221(i2c,0x40) #64 7 | 8 | while True: 9 | print("CH1:",ina.getI(0),"mA") 10 | print("CH2:",ina.getI(1),"mA") 11 | print("CH3:",ina.getI(2),"mA") 12 | print("--------------------------") 13 | print("--------------------------") 14 | time.sleep(0.5) 15 | 16 | 17 | -------------------------------------------------------------------------------- /ina3221/ina3221.py: -------------------------------------------------------------------------------- 1 | from machine import I2C, Pin, Timer 2 | from micropython import const 3 | import time 4 | 5 | _REG_CONFIG = const(0x00) 6 | 7 | _RESET = const(0x8000) 8 | _ENABLE_CH = (None,const(0x4000),const(0x2000),const(0x1000)) # default set 9 | 10 | _AVERAGING_MASK = const(0x0E00) 11 | _AVERAGING_NONE = const(0x0000) # 1 sample, default 12 | _AVERAGING_4_SAMPLES = const(0x0200) 13 | _AVERAGING_16_SAMPLES = const(0x0400) 14 | _AVERAGING_64_SAMPLES = const(0x0600) 15 | _AVERAGING_128_SAMPLES = const(0x0800) 16 | _AVERAGING_256_SAMPLES = const(0x0A00) 17 | _AVERAGING_512_SAMPLES = const(0x0C00) 18 | _AVERAGING_1024_SAMPLES = const(0x0E00) 19 | 20 | _VBUS_CONV_TIME_MASK = const(0x01C0) 21 | _VBUS_CONV_TIME_140US = const(0x0000) 22 | _VBUS_CONV_TIME_204US = const(0x0040) 23 | _VBUS_CONV_TIME_332US = const(0x0080) 24 | _VBUS_CONV_TIME_588US = const(0x00C0) 25 | _VBUS_CONV_TIME_1MS = const(0x0100) # 1.1ms, default 26 | _VBUS_CONV_TIME_2MS = const(0x0140) # 2.116ms 27 | _VBUS_CONV_TIME_4MS = const(0x0180) # 4.156ms 28 | _VBUS_CONV_TIME_8MS = const(0x01C0) # 8.244ms 29 | 30 | _SHUNT_CONV_TIME_MASK = const(0x0038) 31 | _SHUNT_CONV_TIME_140US = const(0x0000) 32 | _SHUNT_CONV_TIME_204US = const(0x0008) 33 | _SHUNT_CONV_TIME_332US = const(0x0010) 34 | _SHUNT_CONV_TIME_588US = const(0x0018) 35 | _SHUNT_CONV_TIME_1MS = const(0x0020) # 1.1ms, default 36 | _SHUNT_CONV_TIME_2MS = const(0x0028) # 2.116ms 37 | _SHUNT_CONV_TIME_4MS = const(0x0030) # 4.156ms 38 | _SHUNT_CONV_TIME_8MS = const(0x0038) # 8.244ms 39 | 40 | _MODE_MASK = const(0x0007) 41 | _MODE_POWER_DOWN = const(0x0000) # Power-down 42 | _MODE_SHUNT_VOLTAGE_TRIGGERED = const(0x0001) # Shunt voltage, single-shot (triggered) 43 | _MODE_BUS_VOLTAGE_TRIGGERED = const(0x0002) # Bus voltage, single-shot (triggered) 44 | _MODE_SHUNT_AND_BUS_TRIGGERED = const(0x0003) # Shunt and bus, single-shot (triggered) 45 | _MODE_POWER_DOWN2 = const(0x0004) # Power-down 46 | _MODE_SHUNT_VOLTAGE_CONTINUOUS = const(0x0005) # Shunt voltage, continous 47 | _MODE_BUS_VOLTAGE_CONTINUOUS = const(0x0006) # Bus voltage, continuous 48 | _MODE_SHUNT_AND_BUS_CONTINOUS = const(0x0007) # Shunt and bus, continuous (default) 49 | 50 | # Other registers 51 | _REG_SHUNT_VOLTAGE_CH = (None, const(0x01), const(0x03), const(0x05)) 52 | _REG_BUS_VOLTAGE_CH = (None, const(0x02), const(0x04), const(0x06)) 53 | _REG_CRITICAL_ALERT_LIMIT_CH = (None, const(0x07), const(0x09), const(0x0B)) 54 | _REG_WARNING_ALERT_LIMIT_CH = (None, const(0x08), const(0x0A), const(0x0C)) 55 | _REG_SHUNT_VOLTAGE_SUM = const(0x0D) 56 | _REG_SHUNT_VOLTAGE_SUM_LIMIT = const(0x0E) 57 | 58 | # Mask/enable register 59 | _REG_MASK_ENABLE = const(0x0F) 60 | _SUM_CONTROL_CH = (None,const(0x4000),const(0x2000),const(0x1000)) #default not set 61 | _WARNING_LATCH_ENABLE = const(0x0800) # default not set 62 | _CRITICAL_LATCH_ENABLE = const(0x0400) # default not set 63 | _CRITICAL_FLAG_CH = (None,const(0x0200),const(0x0100),const(0x0080)) 64 | _SUM_ALERT_FLAG = const(0x0040) 65 | _WARNING_FLAG_CH = (None,const(0x0020),const(0x0010),const(0x0008)) 66 | _POWER_ALERT_FLAG = const(0x0004) 67 | _TIMING_ALERT_FLAG = const(0x0002) 68 | _CONV_READY_FLAG = const(0x0001) 69 | 70 | # Other registers 71 | _REG_POWER_VALID_UPPER_LIMIT = const(0x10) 72 | _REG_POWER_VALID_LOWER_LIMIT = const(0x11) 73 | _REG_MANUFACTURER_ID = const(0xFE) 74 | _REG_DIE_ID = const(0xFF) 75 | 76 | # Constants for manufacturer and device ID 77 | _MANUFACTURER_ID = const(0x5449) # "TI" 78 | _DIE_ID = const(0x3220) 79 | 80 | 81 | 82 | INA3221_I2C_ADDR = const(64) 83 | INA3221_REG_MASK = const(0x0f) 84 | INA3221_REG_ID = const(0xff) 85 | R_SHUNT = [100, 100, 100] 86 | 87 | class INA3221: 88 | def __init__(self, i2c,addr = INA3221_I2C_ADDR): 89 | self.addr = addr 90 | self.i2c = i2c 91 | self.buf = bytearray(2) 92 | def getVShuntRaw(self, chl_num = 0): 93 | """获取分压电阻原始数据""" 94 | reg = 1 + chl_num * 2 95 | v_shunt_raw = self.i2c.readfrom_mem(self.addr, reg, 2) 96 | return v_shunt_raw 97 | 98 | def getVBusRaw(self, chl_num = 0): 99 | """获取输入电压原始数据""" 100 | reg = (chl_num + 1)* 2 101 | v_bus_raw = self.i2c.readfrom_mem(self.addr, reg, 2) 102 | return v_bus_raw 103 | 104 | def getVBus(self, chl_num = 0): 105 | """获取输入电压""" 106 | v_bus_raw = self.getVBusRaw(chl_num) 107 | v_bus = v_bus_raw[0] * 256 + v_bus_raw[1] 108 | return v_bus 109 | 110 | 111 | def getIShunt(self, chl_num = 0): 112 | """获取chl_num电流""" 113 | v_shunt_raw = self.getVShuntRaw(chl_num) 114 | v_shunt = v_shunt_raw[1] * 5 + v_shunt_raw[0] * 1280 115 | i_shunt = v_shunt / R_SHUNT[chl_num] 116 | return i_shunt 117 | def getI(self,chl_num=0,avg=20): 118 | """获取电流,默认1ms一次,20ms为一次测量电流,0.02秒一次""" 119 | a=0 120 | for i in range(avg): 121 | while True: 122 | i_o=self.getIShunt(chl_num) 123 | if i_o<2000: 124 | #inna最大电流为1.6A,超过2A的为错误值 125 | break 126 | a=a+i_o 127 | # time.sleep_ms(1) 128 | return round(a/avg,1) -------------------------------------------------------------------------------- /ina3221/readme.md: -------------------------------------------------------------------------------- 1 | 注意:必须使用3.3V 2 | 建议将A0与GND一起焊接(用焊锡相连)这样IIC地址就为)0x40 3 | 4 | 相关地址 5 | 6 | |A0连接|address| 7 | |-|-| 8 | |GND|0x40| 9 | |VS|0x41| 10 | |SDA|0x42| 11 | |SCL|0x43| 12 | 13 | 接线(目前使用IIC 0 可以根据PICO的引脚自行切换对应的IIC) 14 | 15 | CH1 CH2 CH3 接线请不要接错 VIN+ VIN- 16 | 17 | |INA3221|PICO| 18 | |-|-| 19 | |SDA|GP0| 20 | |SCL|GP1| 21 | |vcc(vs)|3v3| 22 | |gnd|gnd| 23 | 24 | 25 | 上传文件ina3221.py,接线后使用测试文件ex3221.py。 26 | 27 | 每次测量电流为12ms左右,默认取20次测量平均数。 28 | 29 | 相关信息参考: 30 | https://github.com/roarnyg/INA3221-Micropython 31 | https://github.com/PilotXing/ina3221 32 | https://e2e.ti.com/support/amplifiers-group/amplifiers/f/amplifiers-forum/470422/ina3221---i2c-comms-issue#:~:text=The%20addressing%20is%20done%20as%20shown%20below%2C%20IC8,has%20the%20address%200x43%20%28according%20to%20the%20datasheet%29. 33 | 34 | 35 | 4电机测量 36 | 37 | A-INA3221与B-INA3221 之间的地址必须不一样,(即必须修改AO的地址) 38 | |A-INA3221|B-INA3221|PICO| 39 | |-|-|-| 40 | |SDA|SDA|GP0| 41 | |SDA|SDA|GP1| 42 | |vcc(vs)|vcc(vs)|3v3| 43 | |gnd|gnd|gnd| 44 | -------------------------------------------------------------------------------- /ina3221/建议将A0与GND相连.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/ina3221/建议将A0与GND相连.jpg -------------------------------------------------------------------------------- /lcd1602/esp8266_i2c_lcd.py: -------------------------------------------------------------------------------- 1 | """Implements a HD44780 character LCD connected via PCF8574 on I2C. 2 | This was tested with: https://www.wemos.cc/product/d1-mini.html""" 3 | 4 | from lcd_api import LcdApi 5 | from machine import I2C 6 | from time import sleep_ms 7 | 8 | # The PCF8574 has a jumper selectable address: 0x20 - 0x27 9 | DEFAULT_I2C_ADDR = 0x27 10 | 11 | # Defines shifts or masks for the various LCD line attached to the PCF8574 12 | 13 | MASK_RS = 0x01 14 | MASK_RW = 0x02 15 | MASK_E = 0x04 16 | SHIFT_BACKLIGHT = 3 17 | SHIFT_DATA = 4 18 | 19 | 20 | class I2cLcd(LcdApi): 21 | """Implements a HD44780 character LCD connected via PCF8574 on I2C.""" 22 | 23 | def __init__(self, i2c, i2c_addr, num_lines, num_columns): 24 | self.i2c = i2c 25 | self.i2c_addr = i2c_addr 26 | self.i2c.writeto(self.i2c_addr, bytearray([0])) 27 | sleep_ms(20) # Allow LCD time to powerup 28 | # Send reset 3 times 29 | self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) 30 | sleep_ms(5) # need to delay at least 4.1 msec 31 | self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) 32 | sleep_ms(1) 33 | self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) 34 | sleep_ms(1) 35 | # Put LCD into 4 bit mode 36 | self.hal_write_init_nibble(self.LCD_FUNCTION) 37 | sleep_ms(1) 38 | LcdApi.__init__(self, num_lines, num_columns) 39 | cmd = self.LCD_FUNCTION 40 | if num_lines > 1: 41 | cmd |= self.LCD_FUNCTION_2LINES 42 | self.hal_write_command(cmd) 43 | 44 | def hal_write_init_nibble(self, nibble): 45 | """Writes an initialization nibble to the LCD. 46 | 47 | This particular function is only used during initialization. 48 | """ 49 | byte = ((nibble >> 4) & 0x0f) << SHIFT_DATA 50 | self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E])) 51 | self.i2c.writeto(self.i2c_addr, bytearray([byte])) 52 | 53 | def hal_backlight_on(self): 54 | """Allows the hal layer to turn the backlight on.""" 55 | self.i2c.writeto(self.i2c_addr, bytearray([1 << SHIFT_BACKLIGHT])) 56 | 57 | def hal_backlight_off(self): 58 | """Allows the hal layer to turn the backlight off.""" 59 | self.i2c.writeto(self.i2c_addr, bytearray([0])) 60 | 61 | def hal_write_command(self, cmd): 62 | """Writes a command to the LCD. 63 | 64 | Data is latched on the falling edge of E. 65 | """ 66 | byte = ((self.backlight << SHIFT_BACKLIGHT) | (((cmd >> 4) & 0x0f) << SHIFT_DATA)) 67 | self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E])) 68 | self.i2c.writeto(self.i2c_addr, bytearray([byte])) 69 | byte = ((self.backlight << SHIFT_BACKLIGHT) | ((cmd & 0x0f) << SHIFT_DATA)) 70 | self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E])) 71 | self.i2c.writeto(self.i2c_addr, bytearray([byte])) 72 | if cmd <= 3: 73 | # The home and clear commands require a worst case delay of 4.1 msec 74 | sleep_ms(5) 75 | 76 | def hal_write_data(self, data): 77 | """Write data to the LCD.""" 78 | byte = (MASK_RS | (self.backlight << SHIFT_BACKLIGHT) | (((data >> 4) & 0x0f) << SHIFT_DATA)) 79 | self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E])) 80 | self.i2c.writeto(self.i2c_addr, bytearray([byte])) 81 | byte = (MASK_RS | (self.backlight << SHIFT_BACKLIGHT) | ((data & 0x0f) << SHIFT_DATA)) 82 | self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E])) 83 | self.i2c.writeto(self.i2c_addr, bytearray([byte])) 84 | -------------------------------------------------------------------------------- /lcd1602/lcd_api.py: -------------------------------------------------------------------------------- 1 | """Provides an API for talking to HD44780 compatible character LCDs.""" 2 | 3 | import time 4 | 5 | class LcdApi: 6 | """Implements the API for talking with HD44780 compatible character LCDs. 7 | This class only knows what commands to send to the LCD, and not how to get 8 | them to the LCD. 9 | 10 | It is expected that a derived class will implement the hal_xxx functions. 11 | """ 12 | 13 | # The following constant names were lifted from the avrlib lcd.h 14 | # header file, however, I changed the definitions from bit numbers 15 | # to bit masks. 16 | # 17 | # HD44780 LCD controller command set 18 | 19 | LCD_CLR = 0x01 # DB0: clear display 20 | LCD_HOME = 0x02 # DB1: return to home position 21 | 22 | LCD_ENTRY_MODE = 0x04 # DB2: set entry mode 23 | LCD_ENTRY_INC = 0x02 # --DB1: increment 24 | LCD_ENTRY_SHIFT = 0x01 # --DB0: shift 25 | 26 | LCD_ON_CTRL = 0x08 # DB3: turn lcd/cursor on 27 | LCD_ON_DISPLAY = 0x04 # --DB2: turn display on 28 | LCD_ON_CURSOR = 0x02 # --DB1: turn cursor on 29 | LCD_ON_BLINK = 0x01 # --DB0: blinking cursor 30 | 31 | LCD_MOVE = 0x10 # DB4: move cursor/display 32 | LCD_MOVE_DISP = 0x08 # --DB3: move display (0-> move cursor) 33 | LCD_MOVE_RIGHT = 0x04 # --DB2: move right (0-> left) 34 | 35 | LCD_FUNCTION = 0x20 # DB5: function set 36 | LCD_FUNCTION_8BIT = 0x10 # --DB4: set 8BIT mode (0->4BIT mode) 37 | LCD_FUNCTION_2LINES = 0x08 # --DB3: two lines (0->one line) 38 | LCD_FUNCTION_10DOTS = 0x04 # --DB2: 5x10 font (0->5x7 font) 39 | LCD_FUNCTION_RESET = 0x30 # See "Initializing by Instruction" section 40 | 41 | LCD_CGRAM = 0x40 # DB6: set CG RAM address 42 | LCD_DDRAM = 0x80 # DB7: set DD RAM address 43 | 44 | LCD_RS_CMD = 0 45 | LCD_RS_DATA = 1 46 | 47 | LCD_RW_WRITE = 0 48 | LCD_RW_READ = 1 49 | 50 | def __init__(self, num_lines, num_columns): 51 | self.num_lines = num_lines 52 | if self.num_lines > 4: 53 | self.num_lines = 4 54 | self.num_columns = num_columns 55 | if self.num_columns > 40: 56 | self.num_columns = 40 57 | self.cursor_x = 0 58 | self.cursor_y = 0 59 | self.backlight = True 60 | self.display_off() 61 | self.backlight_on() 62 | self.clear() 63 | self.hal_write_command(self.LCD_ENTRY_MODE | self.LCD_ENTRY_INC) 64 | self.hide_cursor() 65 | self.display_on() 66 | 67 | def clear(self): 68 | """Clears the LCD display and moves the cursor to the top left 69 | corner. 70 | """ 71 | self.hal_write_command(self.LCD_CLR) 72 | self.hal_write_command(self.LCD_HOME) 73 | self.cursor_x = 0 74 | self.cursor_y = 0 75 | 76 | def show_cursor(self): 77 | """Causes the cursor to be made visible.""" 78 | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | 79 | self.LCD_ON_CURSOR) 80 | 81 | def hide_cursor(self): 82 | """Causes the cursor to be hidden.""" 83 | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY) 84 | 85 | def blink_cursor_on(self): 86 | """Turns on the cursor, and makes it blink.""" 87 | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | 88 | self.LCD_ON_CURSOR | self.LCD_ON_BLINK) 89 | 90 | def blink_cursor_off(self): 91 | """Turns on the cursor, and makes it no blink (i.e. be solid).""" 92 | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY | 93 | self.LCD_ON_CURSOR) 94 | 95 | def display_on(self): 96 | """Turns on (i.e. unblanks) the LCD.""" 97 | self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY) 98 | 99 | def display_off(self): 100 | """Turns off (i.e. blanks) the LCD.""" 101 | self.hal_write_command(self.LCD_ON_CTRL) 102 | 103 | def backlight_on(self): 104 | """Turns the backlight on. 105 | 106 | This isn't really an LCD command, but some modules have backlight 107 | controls, so this allows the hal to pass through the command. 108 | """ 109 | self.backlight = True 110 | self.hal_backlight_on() 111 | 112 | def backlight_off(self): 113 | """Turns the backlight off. 114 | 115 | This isn't really an LCD command, but some modules have backlight 116 | controls, so this allows the hal to pass through the command. 117 | """ 118 | self.backlight = False 119 | self.hal_backlight_off() 120 | 121 | def move_to(self, cursor_x, cursor_y): 122 | """Moves the cursor position to the indicated position. The cursor 123 | position is zero based (i.e. cursor_x == 0 indicates first column). 124 | """ 125 | self.cursor_x = cursor_x 126 | self.cursor_y = cursor_y 127 | addr = cursor_x & 0x3f 128 | if cursor_y & 1: 129 | addr += 0x40 # Lines 1 & 3 add 0x40 130 | if cursor_y & 2: 131 | addr += 0x14 # Lines 2 & 3 add 0x14 132 | self.hal_write_command(self.LCD_DDRAM | addr) 133 | 134 | def putchar(self, char): 135 | """Writes the indicated character to the LCD at the current cursor 136 | position, and advances the cursor by one position. 137 | """ 138 | if char != '\n': 139 | self.hal_write_data(ord(char)) 140 | self.cursor_x += 1 141 | if self.cursor_x >= self.num_columns or char == '\n': 142 | self.cursor_x = 0 143 | self.cursor_y += 1 144 | if self.cursor_y >= self.num_lines: 145 | self.cursor_y = 0 146 | self.move_to(self.cursor_x, self.cursor_y) 147 | 148 | def putstr(self, string): 149 | """Write the indicated string to the LCD at the current cursor 150 | position and advances the cursor position appropriately. 151 | """ 152 | for char in string: 153 | self.putchar(char) 154 | 155 | def custom_char(self, location, charmap): 156 | """Write a character to one of the 8 CGRAM locations, available 157 | as chr(0) through chr(7). 158 | """ 159 | location &= 0x7 160 | self.hal_write_command(self.LCD_CGRAM | (location << 3)) 161 | time.sleep_us(40) 162 | for i in range(8): 163 | self.hal_write_data(charmap[i]) 164 | time.sleep_us(40) 165 | self.move_to(self.cursor_x, self.cursor_y) 166 | 167 | def hal_backlight_on(self): 168 | """Allows the hal layer to turn the backlight on. 169 | 170 | If desired, a derived HAL class will implement this function. 171 | """ 172 | pass 173 | 174 | def hal_backlight_off(self): 175 | """Allows the hal layer to turn the backlight off. 176 | 177 | If desired, a derived HAL class will implement this function. 178 | """ 179 | pass 180 | 181 | def hal_write_command(self, cmd): 182 | """Write a command to the LCD. 183 | 184 | It is expected that a derived HAL class will implement this 185 | function. 186 | """ 187 | raise NotImplementedError 188 | 189 | def hal_write_data(self, data): 190 | """Write data to the LCD. 191 | 192 | It is expected that a derived HAL class will implement this 193 | function. 194 | """ 195 | raise NotImplementedError 196 | -------------------------------------------------------------------------------- /lcd1602/main.py: -------------------------------------------------------------------------------- 1 | from machine import I2C, Pin 2 | from esp8266_i2c_lcd import I2cLcd 3 | DEFAULT_I2C_ADDR = 0x27 4 | i2c = I2C(0,sda=Pin(0),scl=Pin(1),freq=400000) 5 | lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16) 6 | #创建lcd对象用于显示 7 | lcd.putstr("bilibili \n2021-1-30 20:35") 8 | #显示文字 “bilibili \n2021-1-30 20:35” \n为换行符号 9 | # lcd.clear() 10 | 11 | 12 | 13 | #SDA GPIO0 14 | #SCL GPIO1 15 | #Vcc 5V (3V显示不清楚) 16 | #GND GND 17 | 18 | #若外置5V电源的话,请把外置电源的GND与树莓派的GND相连。 19 | -------------------------------------------------------------------------------- /lcd2004/lcd2004.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Modifed mpy drive for I2C LCD1602 - Now LCD2004 3 | 4 | Original Author: shaoziyang 5 | Date: 2018.2 6 | Modified 2020.9 - cleaned up char and puts 7 | https://www.micropython.org.cn 8 | 9 | ''' 10 | from utime import sleep_ms 11 | from machine import I2C 12 | import uasyncio 13 | 14 | class LCD2004(): 15 | def __init__(self, i2c, addr = 0): 16 | self.i2c=i2c 17 | self.buf = bytearray(1) 18 | self.BK, self.RS, self.E = 0x08, 0x00, 0x04 19 | self.ADDR = addr if addr else self.autoaddr() 20 | self.setcmd(0x33) 21 | sleep_ms(5) 22 | self.send(0x30) 23 | sleep_ms(5) 24 | self.send(0x20) 25 | sleep_ms(5) 26 | for i in [0x28, 0x0C, 0x06, 0x01]: 27 | self.setcmd(i) 28 | self.px, self.py = 0, 0 29 | self.line_length = 20 30 | self.pb = bytearray(' '*self.line_length) 31 | self.version='2.0' 32 | 33 | def setReg(self, dat): 34 | self.buf[0] = dat 35 | self.i2c.writeto(self.ADDR, self.buf) 36 | sleep_ms(1) #is this sleep even required ? 37 | 38 | def send(self, dat): 39 | d=(dat&0xF0)|self.BK|self.RS 40 | self.setReg(d) 41 | self.setReg(d|0x04) 42 | self.setReg(d) 43 | 44 | def setcmd(self, cmd): 45 | self.RS=0 46 | self.send(cmd) 47 | self.send(cmd<<4) 48 | 49 | def setdat(self, dat): 50 | self.RS=1 51 | self.send(dat) 52 | self.send(dat<<4) 53 | 54 | def autoaddr(self): 55 | for i in (32, 63): 56 | try: 57 | if self.i2c.readfrom(i, 1): 58 | return i 59 | except: 60 | pass 61 | raise Exception('I2C address detect error!') 62 | 63 | def clear(self): 64 | self.setcmd(1) 65 | 66 | def backlight(self, on): 67 | if on: 68 | self.BK=0x08 69 | else: 70 | self.BK=0 71 | self.setcmd(0) 72 | 73 | def on(self): 74 | self.setcmd(0x0C) 75 | 76 | def off(self): 77 | self.setcmd(0x08) 78 | 79 | def shl(self): 80 | self.setcmd(0x18) 81 | 82 | def shr(self): 83 | self.setcmd(0x1C) 84 | 85 | def char(self, ch, x, y): 86 | self.setcmd([0x80,0xC0,0x94,0xD4][y]+x) 87 | self.setdat(ch) 88 | 89 | def puts(self, s, x=0, y=0): 90 | if type(s) is not str: 91 | s = str(s) 92 | if len(s)>0: 93 | for i in range(0, len(s)): 94 | self.char(ord(s[i]),x+i,y) 95 | 96 | def newline(self): 97 | self.px = 0 98 | if self.py < 1: 99 | self.py += 1 100 | else: 101 | for i in range(16): 102 | self.char(self.pb[i], i) 103 | self.char(32, i, 1) 104 | self.pb[i] = 32 105 | 106 | def print(self, s): 107 | if type(s) is not str: 108 | s = str(s) 109 | for i in range(len(s)): 110 | d = ord(s[i]) 111 | if d == ord('\n'): 112 | self.newline() 113 | else: 114 | self.char(d, self.px, self.py) 115 | if self.py: 116 | self.pb[self.px] = d 117 | self.px += 1 118 | if self.px > (self.line_length-1): 119 | self.newline() -------------------------------------------------------------------------------- /lcd2004/main.py: -------------------------------------------------------------------------------- 1 | from machine import I2C, Pin 2 | from lcd2004 import LCD2004 3 | i2c = I2C(0, freq=400000) 4 | lcd = LCD2004(i2c, addr=39) -------------------------------------------------------------------------------- /max7219/main.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | import max7219 5 | from machine import Pin,SPI 6 | spi=SPI(0,baudrate=800000,sck=Pin(2), mosi=Pin(3), miso=Pin(0)) 7 | ss = Pin(1, Pin.OUT) 8 | display = max7219.Matrix8x8(spi, ss, 4) 9 | display.text('pico',0,0,1) 10 | display.show() 11 | display.pixel(20,7,1) 12 | display.show() 13 | display.line(0,0,31,4,1) 14 | display.show() 15 | display.hline(0,0,32,1) 16 | display.show() 17 | display.vline(2,3,4,1) 18 | display.show() 19 | display.fill(0) 20 | display.show() 21 | -------------------------------------------------------------------------------- /max7219/max7219.py: -------------------------------------------------------------------------------- 1 | """ 2 | MicroPython max7219 cascadable 8x8 LED matrix driver 3 | https://github.com/mcauser/micropython-max7219 4 | 5 | MIT License 6 | Copyright (c) 2017 Mike Causer 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in all 16 | copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | SOFTWARE. 25 | """ 26 | 27 | from micropython import const 28 | import framebuf 29 | 30 | _NOOP = const(0) 31 | _DIGIT0 = const(1) 32 | _DECODEMODE = const(9) 33 | _INTENSITY = const(10) 34 | _SCANLIMIT = const(11) 35 | _SHUTDOWN = const(12) 36 | _DISPLAYTEST = const(15) 37 | 38 | class Matrix8x8: 39 | def __init__(self, spi, cs, num): 40 | """ 41 | Driver for cascading MAX7219 8x8 LED matrices. 42 | 43 | >>> import max7219 44 | >>> from machine import Pin, SPI 45 | >>> spi = SPI(1) 46 | >>> display = max7219.Matrix8x8(spi, Pin('X5'), 4) 47 | >>> display.text('1234',0,0,1) 48 | >>> display.show() 49 | 50 | """ 51 | self.spi = spi 52 | self.cs = cs 53 | self.cs.init(cs.OUT, True) 54 | self.buffer = bytearray(8 * num) 55 | self.num = num 56 | fb = framebuf.FrameBuffer(self.buffer, 8 * num, 8, framebuf.MONO_HLSB) 57 | self.framebuf = fb 58 | # Provide methods for accessing FrameBuffer graphics primitives. This is a workround 59 | # because inheritance from a native class is currently unsupported. 60 | # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html 61 | self.fill = fb.fill # (col) 62 | self.pixel = fb.pixel # (x, y[, c]) 63 | self.hline = fb.hline # (x, y, w, col) 64 | self.vline = fb.vline # (x, y, h, col) 65 | self.line = fb.line # (x1, y1, x2, y2, col) 66 | self.rect = fb.rect # (x, y, w, h, col) 67 | self.fill_rect = fb.fill_rect # (x, y, w, h, col) 68 | self.text = fb.text # (string, x, y, col=1) 69 | self.scroll = fb.scroll # (dx, dy) 70 | self.blit = fb.blit # (fbuf, x, y[, key]) 71 | self.init() 72 | 73 | def _write(self, command, data): 74 | self.cs(0) 75 | for m in range(self.num): 76 | self.spi.write(bytearray([command, data])) 77 | self.cs(1) 78 | 79 | def init(self): 80 | for command, data in ( 81 | (_SHUTDOWN, 0), 82 | (_DISPLAYTEST, 0), 83 | (_SCANLIMIT, 7), 84 | (_DECODEMODE, 0), 85 | (_SHUTDOWN, 1), 86 | ): 87 | self._write(command, data) 88 | 89 | def brightness(self, value): 90 | if not 0 <= value <= 15: 91 | raise ValueError("Brightness out of range") 92 | self._write(_INTENSITY, value) 93 | 94 | def show(self): 95 | for y in range(8): 96 | self.cs(0) 97 | for m in range(self.num): 98 | self.spi.write(bytearray([_DIGIT0 + y, self.buffer[(y * self.num) + m]])) 99 | self.cs(1) 100 | -------------------------------------------------------------------------------- /max7219/连线.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/max7219/连线.jpg -------------------------------------------------------------------------------- /mlx90614/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/mlx90614/.keep -------------------------------------------------------------------------------- /mlx90614/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ### 连线 5 | 6 | |MLX90614|PICO| 7 | |-|-| 8 | |vcc|3v3| 9 | |gnd|gnd| 10 | |sda|5| 11 | |scl|4| 12 | 13 | PICO使用softI2C,与MLX90614 (GY-906)连接,波特率100000 14 | 15 | ### DEMO 16 | ```python 17 | import mlx90614 18 | from machine import Pin 19 | 20 | from machine import SoftI2C as I2C 21 | i2c = I2C(scl=Pin(4), sda=Pin(5),freq=100000) 22 | 23 | sensor = mlx90614.MLX90614(i2c) 24 | 25 | print(sensor.read_ambient_temp())#环境温度 26 | print(sensor.read_object_temp()) #物体温度 27 | 28 | ``` 29 | 30 | 31 | 原始库: 32 | https://github.com/mcauser/micropython-mlx90614 -------------------------------------------------------------------------------- /mlx90614/mlx90614.py: -------------------------------------------------------------------------------- 1 | """ 2 | MicroPython MLX90614 IR temperature sensor driver 3 | https://github.com/mcauser/micropython-mlx90614 4 | MIT License 5 | Copyright (c) 2016 Mike Causer 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | SOFTWARE. 21 | """ 22 | 23 | import ustruct 24 | 25 | class SensorBase: 26 | 27 | def read16(self, register): 28 | data = self.i2c.readfrom_mem(self.address, register, 2) 29 | return ustruct.unpack('/dev/null 2>&1; echo $$?), 1) 12 | $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) 13 | endif 14 | 15 | # Internal variables. 16 | PAPEROPT_a4 = -D latex_paper_size=a4 17 | PAPEROPT_letter = -D latex_paper_size=letter 18 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 19 | # the i18n builder cannot share the environment and doctrees with the others 20 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 21 | 22 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 23 | 24 | help: 25 | @echo "Please use \`make ' where is one of" 26 | @echo " html to make standalone HTML files" 27 | @echo " dirhtml to make HTML files named index.html in directories" 28 | @echo " singlehtml to make a single large HTML file" 29 | @echo " pickle to make pickle files" 30 | @echo " json to make JSON files" 31 | @echo " htmlhelp to make HTML files and a HTML help project" 32 | @echo " qthelp to make HTML files and a qthelp project" 33 | @echo " devhelp to make HTML files and a Devhelp project" 34 | @echo " epub to make an epub" 35 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 36 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 37 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 38 | @echo " text to make text files" 39 | @echo " man to make manual pages" 40 | @echo " texinfo to make Texinfo files" 41 | @echo " info to make Texinfo files and run them through makeinfo" 42 | @echo " gettext to make PO message catalogs" 43 | @echo " changes to make an overview of all changed/added/deprecated items" 44 | @echo " xml to make Docutils-native XML files" 45 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 46 | @echo " linkcheck to check all external links for integrity" 47 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 48 | 49 | clean: 50 | rm -rf $(BUILDDIR)/* 51 | 52 | html: 53 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 54 | @echo 55 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 56 | 57 | dirhtml: 58 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 59 | @echo 60 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 61 | 62 | singlehtml: 63 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 64 | @echo 65 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 66 | 67 | pickle: 68 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 69 | @echo 70 | @echo "Build finished; now you can process the pickle files." 71 | 72 | json: 73 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 74 | @echo 75 | @echo "Build finished; now you can process the JSON files." 76 | 77 | htmlhelp: 78 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 79 | @echo 80 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 81 | ".hhp project file in $(BUILDDIR)/htmlhelp." 82 | 83 | qthelp: 84 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 85 | @echo 86 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 87 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 88 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/AdafruitPCA9685.qhcp" 89 | @echo "To view the help file:" 90 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/AdafruitPCA9685.qhc" 91 | 92 | devhelp: 93 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 94 | @echo 95 | @echo "Build finished." 96 | @echo "To view the help file:" 97 | @echo "# mkdir -p $$HOME/.local/share/devhelp/AdafruitPCA9685" 98 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/AdafruitPCA9685" 99 | @echo "# devhelp" 100 | 101 | epub: 102 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 103 | @echo 104 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 105 | 106 | latex: 107 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 108 | @echo 109 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 110 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 111 | "(use \`make latexpdf' here to do that automatically)." 112 | 113 | latexpdf: 114 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 115 | @echo "Running LaTeX files through pdflatex..." 116 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 117 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 118 | 119 | latexpdfja: 120 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 121 | @echo "Running LaTeX files through platex and dvipdfmx..." 122 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 123 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 124 | 125 | text: 126 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 127 | @echo 128 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 129 | 130 | man: 131 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 132 | @echo 133 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 134 | 135 | texinfo: 136 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 137 | @echo 138 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 139 | @echo "Run \`make' in that directory to run these through makeinfo" \ 140 | "(use \`make info' here to do that automatically)." 141 | 142 | info: 143 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 144 | @echo "Running Texinfo files through makeinfo..." 145 | make -C $(BUILDDIR)/texinfo info 146 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 147 | 148 | gettext: 149 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 150 | @echo 151 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 152 | 153 | changes: 154 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 155 | @echo 156 | @echo "The overview file is in $(BUILDDIR)/changes." 157 | 158 | linkcheck: 159 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 160 | @echo 161 | @echo "Link check complete; look for any errors in the above output " \ 162 | "or in $(BUILDDIR)/linkcheck/output.txt." 163 | 164 | doctest: 165 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 166 | @echo "Testing of doctests in the sources finished, look at the " \ 167 | "results in $(BUILDDIR)/doctest/output.txt." 168 | 169 | xml: 170 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 171 | @echo 172 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 173 | 174 | pseudoxml: 175 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 176 | @echo 177 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 178 | -------------------------------------------------------------------------------- /pca9685/docs/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Adafruit PCA9685 documentation build configuration file, created by 4 | # sphinx-quickstart on Tue Oct 18 23:27:48 2016. 5 | # 6 | # This file is execfile()d with the current directory set to its 7 | # containing dir. 8 | # 9 | # Note that not all possible configuration values are present in this 10 | # autogenerated file. 11 | # 12 | # All configuration values have a default; values that are commented out 13 | # serve to show the default. 14 | 15 | import sys 16 | import os 17 | 18 | # If extensions (or modules to document with autodoc) are in another directory, 19 | # add these directories to sys.path here. If the directory is relative to the 20 | # documentation root, use os.path.abspath to make it absolute, like shown here. 21 | #sys.path.insert(0, os.path.abspath('.')) 22 | 23 | # -- General configuration ------------------------------------------------ 24 | 25 | # If your documentation needs a minimal Sphinx version, state it here. 26 | #needs_sphinx = '1.0' 27 | 28 | # Add any Sphinx extension module names here, as strings. They can be 29 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 30 | # ones. 31 | extensions = [ 32 | 'sphinx.ext.viewcode', 33 | ] 34 | 35 | # Add any paths that contain templates here, relative to this directory. 36 | templates_path = ['_templates'] 37 | 38 | # The suffix of source filenames. 39 | source_suffix = '.rst' 40 | 41 | # The encoding of source files. 42 | #source_encoding = 'utf-8-sig' 43 | 44 | # The master toctree document. 45 | master_doc = 'index' 46 | 47 | # General information about the project. 48 | project = u'Adafruit PCA9685' 49 | copyright = u'2016, Radomir Dopieralski' 50 | 51 | # The version info for the project you're documenting, acts as replacement for 52 | # |version| and |release|, also used in various other places throughout the 53 | # built documents. 54 | # 55 | # The short X.Y version. 56 | version = '1.0.0' 57 | # The full version, including alpha/beta/rc tags. 58 | release = '1.0.0' 59 | 60 | # The language for content autogenerated by Sphinx. Refer to documentation 61 | # for a list of supported languages. 62 | #language = None 63 | 64 | # There are two options for replacing |today|: either, you set today to some 65 | # non-false value, then it is used: 66 | #today = '' 67 | # Else, today_fmt is used as the format for a strftime call. 68 | #today_fmt = '%B %d, %Y' 69 | 70 | # List of patterns, relative to source directory, that match files and 71 | # directories to ignore when looking for source files. 72 | exclude_patterns = ['_build'] 73 | 74 | # The reST default role (used for this markup: `text`) to use for all 75 | # documents. 76 | #default_role = None 77 | 78 | # If true, '()' will be appended to :func: etc. cross-reference text. 79 | #add_function_parentheses = True 80 | 81 | # If true, the current module name will be prepended to all description 82 | # unit titles (such as .. function::). 83 | #add_module_names = True 84 | 85 | # If true, sectionauthor and moduleauthor directives will be shown in the 86 | # output. They are ignored by default. 87 | #show_authors = False 88 | 89 | # The name of the Pygments (syntax highlighting) style to use. 90 | pygments_style = 'sphinx' 91 | 92 | # A list of ignored prefixes for module index sorting. 93 | #modindex_common_prefix = [] 94 | 95 | # If true, keep warnings as "system message" paragraphs in the built documents. 96 | #keep_warnings = False 97 | 98 | 99 | # -- Options for HTML output ---------------------------------------------- 100 | 101 | # The theme to use for HTML and HTML Help pages. See the documentation for 102 | # a list of builtin themes. 103 | html_theme = 'default' 104 | 105 | # Theme options are theme-specific and customize the look and feel of a theme 106 | # further. For a list of options available for each theme, see the 107 | # documentation. 108 | #html_theme_options = {} 109 | 110 | # Add any paths that contain custom themes here, relative to this directory. 111 | #html_theme_path = [] 112 | 113 | # The name for this set of Sphinx documents. If None, it defaults to 114 | # " v documentation". 115 | #html_title = None 116 | 117 | # A shorter title for the navigation bar. Default is the same as html_title. 118 | #html_short_title = None 119 | 120 | # The name of an image file (relative to this directory) to place at the top 121 | # of the sidebar. 122 | #html_logo = None 123 | 124 | # The name of an image file (within the static path) to use as favicon of the 125 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 126 | # pixels large. 127 | #html_favicon = None 128 | 129 | # Add any paths that contain custom static files (such as style sheets) here, 130 | # relative to this directory. They are copied after the builtin static files, 131 | # so a file named "default.css" will overwrite the builtin "default.css". 132 | html_static_path = ['_static'] 133 | 134 | # Add any extra paths that contain custom files (such as robots.txt or 135 | # .htaccess) here, relative to this directory. These files are copied 136 | # directly to the root of the documentation. 137 | #html_extra_path = [] 138 | 139 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 140 | # using the given strftime format. 141 | #html_last_updated_fmt = '%b %d, %Y' 142 | 143 | # If true, SmartyPants will be used to convert quotes and dashes to 144 | # typographically correct entities. 145 | #html_use_smartypants = True 146 | 147 | # Custom sidebar templates, maps document names to template names. 148 | #html_sidebars = {} 149 | 150 | # Additional templates that should be rendered to pages, maps page names to 151 | # template names. 152 | #html_additional_pages = {} 153 | 154 | # If false, no module index is generated. 155 | #html_domain_indices = True 156 | 157 | # If false, no index is generated. 158 | #html_use_index = True 159 | 160 | # If true, the index is split into individual pages for each letter. 161 | #html_split_index = False 162 | 163 | # If true, links to the reST sources are added to the pages. 164 | #html_show_sourcelink = True 165 | 166 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 167 | #html_show_sphinx = True 168 | 169 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 170 | #html_show_copyright = True 171 | 172 | # If true, an OpenSearch description file will be output, and all pages will 173 | # contain a tag referring to it. The value of this option must be the 174 | # base URL from which the finished HTML is served. 175 | #html_use_opensearch = '' 176 | 177 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 178 | #html_file_suffix = None 179 | 180 | # Output file base name for HTML help builder. 181 | htmlhelp_basename = 'AdafruitPCA9685doc' 182 | 183 | 184 | # -- Options for LaTeX output --------------------------------------------- 185 | 186 | latex_elements = { 187 | # The paper size ('letterpaper' or 'a4paper'). 188 | #'papersize': 'letterpaper', 189 | 190 | # The font size ('10pt', '11pt' or '12pt'). 191 | #'pointsize': '10pt', 192 | 193 | # Additional stuff for the LaTeX preamble. 194 | #'preamble': '', 195 | } 196 | 197 | # Grouping the document tree into LaTeX files. List of tuples 198 | # (source start file, target name, title, 199 | # author, documentclass [howto, manual, or own class]). 200 | latex_documents = [ 201 | ('index', 'AdafruitPCA9685.tex', u'Adafruit PCA9685 Documentation', 202 | u'Radomir Dopieralski', 'manual'), 203 | ] 204 | 205 | # The name of an image file (relative to this directory) to place at the top of 206 | # the title page. 207 | #latex_logo = None 208 | 209 | # For "manual" documents, if this is true, then toplevel headings are parts, 210 | # not chapters. 211 | #latex_use_parts = False 212 | 213 | # If true, show page references after internal links. 214 | #latex_show_pagerefs = False 215 | 216 | # If true, show URL addresses after external links. 217 | #latex_show_urls = False 218 | 219 | # Documents to append as an appendix to all manuals. 220 | #latex_appendices = [] 221 | 222 | # If false, no module index is generated. 223 | #latex_domain_indices = True 224 | 225 | 226 | # -- Options for manual page output --------------------------------------- 227 | 228 | # One entry per manual page. List of tuples 229 | # (source start file, name, description, authors, manual section). 230 | man_pages = [ 231 | ('index', 'adafruitpca9685', u'Adafruit PCA9685 Documentation', 232 | [u'Radomir Dopieralski'], 1) 233 | ] 234 | 235 | # If true, show URL addresses after external links. 236 | #man_show_urls = False 237 | 238 | 239 | # -- Options for Texinfo output ------------------------------------------- 240 | 241 | # Grouping the document tree into Texinfo files. List of tuples 242 | # (source start file, target name, title, author, 243 | # dir menu entry, description, category) 244 | texinfo_documents = [ 245 | ('index', 'AdafruitPCA9685', u'Adafruit PCA9685 Documentation', 246 | u'Radomir Dopieralski', 'AdafruitPCA9685', 'One line description of project.', 247 | 'Miscellaneous'), 248 | ] 249 | 250 | # Documents to append as an appendix to all manuals. 251 | #texinfo_appendices = [] 252 | 253 | # If false, no module index is generated. 254 | #texinfo_domain_indices = True 255 | 256 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 257 | #texinfo_show_urls = 'footnote' 258 | 259 | # If true, do not generate a @detailmenu in the "Top" node's menu. 260 | #texinfo_no_detailmenu = False 261 | -------------------------------------------------------------------------------- /pca9685/docs/index.rst: -------------------------------------------------------------------------------- 1 | .. Adafruit PCA9685 documentation master file, created by 2 | sphinx-quickstart on Tue Oct 18 23:27:48 2016. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to Adafruit PCA9685's documentation! 7 | ============================================ 8 | 9 | Contents: 10 | 11 | .. toctree:: 12 | :maxdepth: 2 13 | 14 | pca9685 15 | servo 16 | motor 17 | 18 | 19 | Indices and tables 20 | ================== 21 | 22 | * :ref:`genindex` 23 | * :ref:`modindex` 24 | * :ref:`search` 25 | 26 | -------------------------------------------------------------------------------- /pca9685/docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | REM Command file for Sphinx documentation 4 | 5 | if "%SPHINXBUILD%" == "" ( 6 | set SPHINXBUILD=sphinx-build 7 | ) 8 | set BUILDDIR=_build 9 | set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . 10 | set I18NSPHINXOPTS=%SPHINXOPTS% . 11 | if NOT "%PAPER%" == "" ( 12 | set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% 13 | set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% 14 | ) 15 | 16 | if "%1" == "" goto help 17 | 18 | if "%1" == "help" ( 19 | :help 20 | echo.Please use `make ^` where ^ is one of 21 | echo. html to make standalone HTML files 22 | echo. dirhtml to make HTML files named index.html in directories 23 | echo. singlehtml to make a single large HTML file 24 | echo. pickle to make pickle files 25 | echo. json to make JSON files 26 | echo. htmlhelp to make HTML files and a HTML help project 27 | echo. qthelp to make HTML files and a qthelp project 28 | echo. devhelp to make HTML files and a Devhelp project 29 | echo. epub to make an epub 30 | echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter 31 | echo. text to make text files 32 | echo. man to make manual pages 33 | echo. texinfo to make Texinfo files 34 | echo. gettext to make PO message catalogs 35 | echo. changes to make an overview over all changed/added/deprecated items 36 | echo. xml to make Docutils-native XML files 37 | echo. pseudoxml to make pseudoxml-XML files for display purposes 38 | echo. linkcheck to check all external links for integrity 39 | echo. doctest to run all doctests embedded in the documentation if enabled 40 | goto end 41 | ) 42 | 43 | if "%1" == "clean" ( 44 | for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i 45 | del /q /s %BUILDDIR%\* 46 | goto end 47 | ) 48 | 49 | 50 | %SPHINXBUILD% 2> nul 51 | if errorlevel 9009 ( 52 | echo. 53 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 54 | echo.installed, then set the SPHINXBUILD environment variable to point 55 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 56 | echo.may add the Sphinx directory to PATH. 57 | echo. 58 | echo.If you don't have Sphinx installed, grab it from 59 | echo.http://sphinx-doc.org/ 60 | exit /b 1 61 | ) 62 | 63 | if "%1" == "html" ( 64 | %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html 65 | if errorlevel 1 exit /b 1 66 | echo. 67 | echo.Build finished. The HTML pages are in %BUILDDIR%/html. 68 | goto end 69 | ) 70 | 71 | if "%1" == "dirhtml" ( 72 | %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml 73 | if errorlevel 1 exit /b 1 74 | echo. 75 | echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. 76 | goto end 77 | ) 78 | 79 | if "%1" == "singlehtml" ( 80 | %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml 81 | if errorlevel 1 exit /b 1 82 | echo. 83 | echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. 84 | goto end 85 | ) 86 | 87 | if "%1" == "pickle" ( 88 | %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle 89 | if errorlevel 1 exit /b 1 90 | echo. 91 | echo.Build finished; now you can process the pickle files. 92 | goto end 93 | ) 94 | 95 | if "%1" == "json" ( 96 | %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json 97 | if errorlevel 1 exit /b 1 98 | echo. 99 | echo.Build finished; now you can process the JSON files. 100 | goto end 101 | ) 102 | 103 | if "%1" == "htmlhelp" ( 104 | %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp 105 | if errorlevel 1 exit /b 1 106 | echo. 107 | echo.Build finished; now you can run HTML Help Workshop with the ^ 108 | .hhp project file in %BUILDDIR%/htmlhelp. 109 | goto end 110 | ) 111 | 112 | if "%1" == "qthelp" ( 113 | %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp 114 | if errorlevel 1 exit /b 1 115 | echo. 116 | echo.Build finished; now you can run "qcollectiongenerator" with the ^ 117 | .qhcp project file in %BUILDDIR%/qthelp, like this: 118 | echo.^> qcollectiongenerator %BUILDDIR%\qthelp\AdafruitPCA9685.qhcp 119 | echo.To view the help file: 120 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\AdafruitPCA9685.ghc 121 | goto end 122 | ) 123 | 124 | if "%1" == "devhelp" ( 125 | %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp 126 | if errorlevel 1 exit /b 1 127 | echo. 128 | echo.Build finished. 129 | goto end 130 | ) 131 | 132 | if "%1" == "epub" ( 133 | %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub 134 | if errorlevel 1 exit /b 1 135 | echo. 136 | echo.Build finished. The epub file is in %BUILDDIR%/epub. 137 | goto end 138 | ) 139 | 140 | if "%1" == "latex" ( 141 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 142 | if errorlevel 1 exit /b 1 143 | echo. 144 | echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. 145 | goto end 146 | ) 147 | 148 | if "%1" == "latexpdf" ( 149 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 150 | cd %BUILDDIR%/latex 151 | make all-pdf 152 | cd %BUILDDIR%/.. 153 | echo. 154 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 155 | goto end 156 | ) 157 | 158 | if "%1" == "latexpdfja" ( 159 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 160 | cd %BUILDDIR%/latex 161 | make all-pdf-ja 162 | cd %BUILDDIR%/.. 163 | echo. 164 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 165 | goto end 166 | ) 167 | 168 | if "%1" == "text" ( 169 | %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text 170 | if errorlevel 1 exit /b 1 171 | echo. 172 | echo.Build finished. The text files are in %BUILDDIR%/text. 173 | goto end 174 | ) 175 | 176 | if "%1" == "man" ( 177 | %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man 178 | if errorlevel 1 exit /b 1 179 | echo. 180 | echo.Build finished. The manual pages are in %BUILDDIR%/man. 181 | goto end 182 | ) 183 | 184 | if "%1" == "texinfo" ( 185 | %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo 186 | if errorlevel 1 exit /b 1 187 | echo. 188 | echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. 189 | goto end 190 | ) 191 | 192 | if "%1" == "gettext" ( 193 | %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale 194 | if errorlevel 1 exit /b 1 195 | echo. 196 | echo.Build finished. The message catalogs are in %BUILDDIR%/locale. 197 | goto end 198 | ) 199 | 200 | if "%1" == "changes" ( 201 | %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes 202 | if errorlevel 1 exit /b 1 203 | echo. 204 | echo.The overview file is in %BUILDDIR%/changes. 205 | goto end 206 | ) 207 | 208 | if "%1" == "linkcheck" ( 209 | %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck 210 | if errorlevel 1 exit /b 1 211 | echo. 212 | echo.Link check complete; look for any errors in the above output ^ 213 | or in %BUILDDIR%/linkcheck/output.txt. 214 | goto end 215 | ) 216 | 217 | if "%1" == "doctest" ( 218 | %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest 219 | if errorlevel 1 exit /b 1 220 | echo. 221 | echo.Testing of doctests in the sources finished, look at the ^ 222 | results in %BUILDDIR%/doctest/output.txt. 223 | goto end 224 | ) 225 | 226 | if "%1" == "xml" ( 227 | %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml 228 | if errorlevel 1 exit /b 1 229 | echo. 230 | echo.Build finished. The XML files are in %BUILDDIR%/xml. 231 | goto end 232 | ) 233 | 234 | if "%1" == "pseudoxml" ( 235 | %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml 236 | if errorlevel 1 exit /b 1 237 | echo. 238 | echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. 239 | goto end 240 | ) 241 | 242 | :end 243 | -------------------------------------------------------------------------------- /pca9685/docs/motor.rst: -------------------------------------------------------------------------------- 1 | Motor Driver 2 | ************ 3 | 4 | .. module:: motor 5 | 6 | .. class:: DCMotors(i2c, address=0x40, freq=1600) 7 | 8 | Control the motor driver. 9 | 10 | The `freq` argument allows setting PWM frequency. Most motors should be 11 | fine with the default. 12 | 13 | .. method:: speed(index, [value]) 14 | 15 | Set or get the motor's speed. 16 | 17 | Negative values make the motor spin backwards. Zero releases the motor. 18 | 19 | .. method:: brake(index) 20 | 21 | Brake the motor. 22 | -------------------------------------------------------------------------------- /pca9685/docs/pca9685.rst: -------------------------------------------------------------------------------- 1 | PCA9685 PWM Driver 2 | ****************** 3 | 4 | .. module:: pca9685 5 | 6 | .. class:: PCA9685(i2c, address=0x40) 7 | 8 | Allows controlling the PWM chip directly. 9 | 10 | .. method:: reset() 11 | 12 | Reset the chip. 13 | 14 | .. method:: freq([freq]) 15 | 16 | Get or set the PWM frequency. 17 | 18 | .. method:: pwm(index, [on], [off]) 19 | 20 | Get or set the PWM signal's on and off timings for the channel `index`. 21 | 22 | .. method:: duty(index, [value], [invert]) 23 | 24 | Get or set the PWM duty cycle in range 0-4095 (4095 corresponds to 100% 25 | duty cycle). If `invert` is `True`, 4095 corresponds to 0 and 0 26 | corresponds to 100%. 27 | -------------------------------------------------------------------------------- /pca9685/docs/servo.rst: -------------------------------------------------------------------------------- 1 | Servo Driver 2 | ************ 3 | 4 | .. module:: servo 5 | 6 | .. class:: Servos(i2c, address=0x40, freq=50, min_us=600, max_us=2400, degrees=180) 7 | 8 | Allows controlling up to 16 hobby servos. 9 | 10 | The `freq` argument sets the PWM signal frequency in Hz. Analog servos 11 | usually expect this to be 50, but digital servos can often handle higher 12 | frequencies, resulting in smoother movements. 13 | 14 | The `min_us` and `max_us` arguments set the range of the singnal's duty 15 | that the servo accepts. This is different between different servo models, 16 | but usually they are centerd at 1500µs. 17 | 18 | The `degrees` argument specifies the physical range of the servo 19 | corresponding to the signal's duty range specified before. It is used to 20 | calculate signal's duty when the angle is specified in degrees or radians. 21 | 22 | 23 | .. method:: position(index, [degrees|radians|us|duty]) 24 | 25 | Get or set the servo position. The position can be specified in 26 | degrees (the default), radians, microseconds or directly as a number 27 | between 0 and 4095 signifying the duty cycle. It will be automatically 28 | clamped to the minimum and maximum range allowed. 29 | 30 | When getting the position, it will always be returned in duty cycle. 31 | 32 | .. method:: release(index) 33 | 34 | Stop sending a signal to the given servomechanism, releasing it. 35 | -------------------------------------------------------------------------------- /pca9685/main.py: -------------------------------------------------------------------------------- 1 | from machine import I2C,Pin 2 | import time 3 | from servo import Servos 4 | i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=10000) 5 | s=Servos(i2c,address=0x40) 6 | s.position(0,0) 7 | s.position(1,0) 8 | s.position(0,90) 9 | s.position(1,90) 10 | 11 | #pico-PCA9685 12 | 13 | #3v3 OUT--VCC 14 | #GND--GND 15 | #GPIO0--SDA 16 | #GPIO1--SCL 17 | 18 | 19 | #PCA 9685 接线 20 | #0-15为外接舵机 21 | #外接5V电源 22 | #黄色 数据 23 | #红色 5V -------------------------------------------------------------------------------- /pca9685/motor.py: -------------------------------------------------------------------------------- 1 | import pca9685 2 | 3 | 4 | _DC_MOTORS = ((8, 9, 10), (13, 12, 11), (2, 3, 4), (7, 6, 5)) 5 | 6 | 7 | class DCMotors: 8 | def __init__(self, i2c, address=0x60, freq=1600): 9 | self.pca9685 = pca9685.PCA9685(i2c, address) 10 | self.pca9685.freq(freq) 11 | 12 | def _pin(self, pin, value=None): 13 | if value is None: 14 | return bool(self.pca9685.pwm(pin)[0]) 15 | if value: 16 | self.pca9685.pwm(pin, 4096, 0) 17 | else: 18 | self.pca9685.pwm(pin, 0, 0) 19 | 20 | def speed(self, index, value=None): 21 | pwm, in2, in1 = _DC_MOTORS[index] 22 | if value is None: 23 | value = self.pca9685.duty(pwm) 24 | if self._pin(in2) and not self._pin(in1): 25 | value = -value 26 | return value 27 | if value > 0: 28 | # Forward 29 | self._pin(in2, False) 30 | self._pin(in1, True) 31 | elif value < 0: 32 | # Backward 33 | self._pin(in1, False) 34 | self._pin(in2, True) 35 | else: 36 | # Release 37 | self._pin(in1, False) 38 | self._pin(in2, False) 39 | self.pca9685.duty(pwm, abs(value)) 40 | 41 | def brake(self, index): 42 | pwm, in2, in1 = _DC_MOTORS[index] 43 | self._pin(in1, True) 44 | self._pin(in2, True) 45 | self.pca9685.duty(pwm, 0) 46 | -------------------------------------------------------------------------------- /pca9685/pca9685.py: -------------------------------------------------------------------------------- 1 | import ustruct 2 | import time 3 | 4 | 5 | class PCA9685: 6 | def __init__(self, i2c, address=0x40): 7 | self.i2c = i2c 8 | self.address = address 9 | self.reset() 10 | 11 | def _write(self, address, value): 12 | self.i2c.writeto_mem(self.address, address, bytearray([value])) 13 | 14 | def _read(self, address): 15 | return self.i2c.readfrom_mem(self.address, address, 1)[0] 16 | 17 | def reset(self): 18 | self._write(0x00, 0x00) # Mode1 19 | 20 | def freq(self, freq=None): 21 | if freq is None: 22 | return int(25000000.0 / 4096 / (self._read(0xfe) - 0.5)) 23 | prescale = int(25000000.0 / 4096.0 / freq + 0.5) 24 | old_mode = self._read(0x00) # Mode 1 25 | self._write(0x00, (old_mode & 0x7F) | 0x10) # Mode 1, sleep 26 | self._write(0xfe, prescale) # Prescale 27 | self._write(0x00, old_mode) # Mode 1 28 | time.sleep_us(5) 29 | self._write(0x00, old_mode | 0xa1) # Mode 1, autoincrement on 30 | 31 | def pwm(self, index, on=None, off=None): 32 | if on is None or off is None: 33 | data = self.i2c.readfrom_mem(self.address, 0x06 + 4 * index, 4) 34 | return ustruct.unpack(' 4095: 41 | self.pca9685.pwm(pin, 4096, 0) 42 | else: 43 | self.pca9685.pwm(pin, 0, value) 44 | 45 | def _pin(self, pin, value): 46 | if value: 47 | self.pca9685.pwm(pin, 4096, 0) 48 | else: 49 | self.pca9685.pwm(pin, 0, 0) 50 | 51 | def onestep(self, direction, style): 52 | ocra = 255 53 | ocrb = 255 54 | # Adjust current steps based on the direction and type of step. 55 | if style == SINGLE: 56 | if (self.currentstep//(MICROSTEPS//2)) % 2: 57 | if direction == FORWARD: 58 | self.currentstep += MICROSTEPS//2 59 | else: 60 | self.currentstep -= MICROSTEPS//2 61 | else: 62 | if direction == FORWARD: 63 | self.currentstep += MICROSTEPS 64 | else: 65 | self.currentstep -= MICROSTEPS 66 | elif style == DOUBLE: 67 | if not (self.currentstep//(MICROSTEPS//2)) % 2: 68 | if direction == FORWARD: 69 | self.currentstep += MICROSTEPS//2 70 | else: 71 | self.currentstep -= MICROSTEPS//2 72 | else: 73 | if direction == FORWARD: 74 | self.currentstep += MICROSTEPS 75 | else: 76 | self.currentstep -= MICROSTEPS 77 | elif style == INTERLEAVE: 78 | if direction == FORWARD: 79 | self.currentstep += MICROSTEPS//2 80 | else: 81 | self.currentstep -= MICROSTEPS//2 82 | elif style == MICROSTEP: 83 | if direction == FORWARD: 84 | self.currentstep += 1 85 | else: 86 | self.currentstep -= 1 87 | self.currentstep += MICROSTEPS*4 88 | self.currentstep %= MICROSTEPS*4 89 | ocra = 0 90 | ocrb = 0 91 | if MICROSTEPS == 8: 92 | curve = _MICROSTEPCURVE8 93 | elif MICROSTEPS == 16: 94 | curve = _MICROSTEPCURVE16 95 | else: 96 | raise RuntimeError('MICROSTEPS must be 8 or 16!') 97 | if 0 <= self.currentstep < MICROSTEPS: 98 | ocra = curve[MICROSTEPS - self.currentstep] 99 | ocrb = curve[self.currentstep] 100 | elif MICROSTEPS <= self.currentstep < MICROSTEPS*2: 101 | ocra = curve[self.currentstep - MICROSTEPS] 102 | ocrb = curve[MICROSTEPS*2 - self.currentstep] 103 | elif MICROSTEPS*2 <= self.currentstep < MICROSTEPS*3: 104 | ocra = curve[MICROSTEPS*3 - self.currentstep] 105 | ocrb = curve[self.currentstep - MICROSTEPS*2] 106 | elif MICROSTEPS*3 <= self.currentstep < MICROSTEPS*4: 107 | ocra = curve[self.currentstep - MICROSTEPS*3] 108 | ocrb = curve[MICROSTEPS*4 - self.currentstep] 109 | self.currentstep += MICROSTEPS*4 110 | self.currentstep %= MICROSTEPS*4 111 | # Set PWM outputs. 112 | self._pwm(self.pwma, ocra*16) 113 | self._pwm(self.pwmb, ocrb*16) 114 | latch_state = 0 115 | # Determine which coils to energize: 116 | if style == MICROSTEP: 117 | if 0 <= self.currentstep < MICROSTEPS: 118 | latch_state |= 0x3 119 | elif MICROSTEPS <= self.currentstep < MICROSTEPS*2: 120 | latch_state |= 0x6 121 | elif MICROSTEPS*2 <= self.currentstep < MICROSTEPS*3: 122 | latch_state |= 0xC 123 | elif MICROSTEPS*3 <= self.currentstep < MICROSTEPS*4: 124 | latch_state |= 0x9 125 | else: 126 | latch_step = self.currentstep//(MICROSTEPS//2) 127 | if latch_step == 0: 128 | latch_state |= 0x1 # energize coil 1 only 129 | elif latch_step == 1: 130 | latch_state |= 0x3 # energize coil 1+2 131 | elif latch_step == 2: 132 | latch_state |= 0x2 # energize coil 2 only 133 | elif latch_step == 3: 134 | latch_state |= 0x6 # energize coil 2+3 135 | elif latch_step == 4: 136 | latch_state |= 0x4 # energize coil 3 only 137 | elif latch_step == 5: 138 | latch_state |= 0xC # energize coil 3+4 139 | elif latch_step == 6: 140 | latch_state |= 0x8 # energize coil 4 only 141 | elif latch_step == 7: 142 | latch_state |= 0x9 # energize coil 1+4 143 | # Energize coils as appropriate: 144 | if latch_state & 0x1: 145 | self._pin(self.ain2, True) 146 | else: 147 | self._pin(self.ain2, False) 148 | if latch_state & 0x2: 149 | self._pin(self.bin1, True) 150 | else: 151 | self._pin(self.bin1, False) 152 | if latch_state & 0x4: 153 | self._pin(self.ain1, True) 154 | else: 155 | self._pin(self.ain1, False) 156 | if latch_state & 0x8: 157 | self._pin(self.bin2, True) 158 | else: 159 | self._pin(self.bin2, False) 160 | return self.currentstep 161 | 162 | 163 | class Steppers: 164 | def __init__(self, i2c, address=0x60, freq=1600): 165 | self.pca9685 = pca9685.PCA9685(i2c, address) 166 | self.pca9685.freq(freq) 167 | 168 | def get_stepper(self, num): 169 | pwma, ain2, ain1, pwmb, bin2, bin1 = _STEPPERS[num] 170 | return StepperMotor(self.pca9685, pwma, ain2, ain1, pwmb, bin2, bin1) 171 | -------------------------------------------------------------------------------- /pca9685/接线.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/pca9685/接线.png -------------------------------------------------------------------------------- /rotary/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/rotary/.keep -------------------------------------------------------------------------------- /rotary/readme.md: -------------------------------------------------------------------------------- 1 | ## 使用 2 | 3 | ### 接线方式 4 | 5 | |HW-040|Pico| 6 | |-|-| 7 | |GND|GND| 8 | |3v3|3v3| 9 | |CLK|2| 10 | |DT|1| 11 | |SW|0| 12 | 13 | ### 测试代码 14 | 15 | ```python 16 | import time 17 | from machine import Pin 18 | from rotary_irq_rp2 import RotaryIRQ 19 | #根据不同的设备导入不同的rotrotary_irq_xxx,请自行查看。 20 | r = RotaryIRQ(pin_num_clk=2, 21 | pin_num_dt=1, 22 | min_val=0, 23 | max_val=10, 24 | reverse=False, 25 | range_mode=RotaryIRQ.RANGE_WRAP) 26 | 27 | button=Pin(0,Pin.IN,Pin.PULL_UP) 28 | val_old = r.value() 29 | while True: 30 | val_new = r.value() 31 | 32 | if val_old != val_new: 33 | val_old = val_new 34 | print('result =', val_new) 35 | if button.value() ==0: 36 | print("按钮被按下") 37 | 38 | time.sleep_ms(50) 39 | ``` 40 | 41 | 42 | 43 | 库来自于 https://github.com/miketeachman/micropython-rotary -------------------------------------------------------------------------------- /rotary/rotary.py: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | # Copyright (c) 2022 Mike Teachman 3 | # https://opensource.org/licenses/MIT 4 | 5 | # Platform-independent MicroPython code for the rotary encoder module 6 | 7 | # Documentation: 8 | # https://github.com/MikeTeachman/micropython-rotary 9 | 10 | import micropython 11 | 12 | _DIR_CW = const(0x10) # Clockwise step 13 | _DIR_CCW = const(0x20) # Counter-clockwise step 14 | 15 | # Rotary Encoder States 16 | _R_START = const(0x0) 17 | _R_CW_1 = const(0x1) 18 | _R_CW_2 = const(0x2) 19 | _R_CW_3 = const(0x3) 20 | _R_CCW_1 = const(0x4) 21 | _R_CCW_2 = const(0x5) 22 | _R_CCW_3 = const(0x6) 23 | _R_ILLEGAL = const(0x7) 24 | 25 | _transition_table = [ 26 | 27 | # |------------- NEXT STATE -------------| |CURRENT STATE| 28 | # CLK/DT CLK/DT CLK/DT CLK/DT 29 | # 00 01 10 11 30 | [_R_START, _R_CCW_1, _R_CW_1, _R_START], # _R_START 31 | [_R_CW_2, _R_START, _R_CW_1, _R_START], # _R_CW_1 32 | [_R_CW_2, _R_CW_3, _R_CW_1, _R_START], # _R_CW_2 33 | [_R_CW_2, _R_CW_3, _R_START, _R_START | _DIR_CW], # _R_CW_3 34 | [_R_CCW_2, _R_CCW_1, _R_START, _R_START], # _R_CCW_1 35 | [_R_CCW_2, _R_CCW_1, _R_CCW_3, _R_START], # _R_CCW_2 36 | [_R_CCW_2, _R_START, _R_CCW_3, _R_START | _DIR_CCW], # _R_CCW_3 37 | [_R_START, _R_START, _R_START, _R_START]] # _R_ILLEGAL 38 | 39 | _transition_table_half_step = [ 40 | [_R_CW_3, _R_CW_2, _R_CW_1, _R_START], 41 | [_R_CW_3 | _DIR_CCW, _R_START, _R_CW_1, _R_START], 42 | [_R_CW_3 | _DIR_CW, _R_CW_2, _R_START, _R_START], 43 | [_R_CW_3, _R_CCW_2, _R_CCW_1, _R_START], 44 | [_R_CW_3, _R_CW_2, _R_CCW_1, _R_START | _DIR_CW], 45 | [_R_CW_3, _R_CCW_2, _R_CW_3, _R_START | _DIR_CCW], 46 | [_R_START, _R_START, _R_START, _R_START], 47 | [_R_START, _R_START, _R_START, _R_START]] 48 | 49 | _STATE_MASK = const(0x07) 50 | _DIR_MASK = const(0x30) 51 | 52 | 53 | def _wrap(value, incr, lower_bound, upper_bound): 54 | range = upper_bound - lower_bound + 1 55 | value = value + incr 56 | 57 | if value < lower_bound: 58 | value += range * ((lower_bound - value) // range + 1) 59 | 60 | return lower_bound + (value - lower_bound) % range 61 | 62 | 63 | def _bound(value, incr, lower_bound, upper_bound): 64 | return min(upper_bound, max(lower_bound, value + incr)) 65 | 66 | 67 | def _trigger(rotary_instance): 68 | for listener in rotary_instance._listener: 69 | listener() 70 | 71 | 72 | class Rotary(object): 73 | 74 | RANGE_UNBOUNDED = const(1) 75 | RANGE_WRAP = const(2) 76 | RANGE_BOUNDED = const(3) 77 | 78 | def __init__(self, min_val, max_val, reverse, range_mode, half_step, invert): 79 | self._min_val = min_val 80 | self._max_val = max_val 81 | self._reverse = -1 if reverse else 1 82 | self._range_mode = range_mode 83 | self._value = min_val 84 | self._state = _R_START 85 | self._half_step = half_step 86 | self._invert = invert 87 | self._listener = [] 88 | 89 | def set(self, value=None, min_val=None, 90 | max_val=None, reverse=None, range_mode=None): 91 | # disable DT and CLK pin interrupts 92 | self._hal_disable_irq() 93 | 94 | if value is not None: 95 | self._value = value 96 | if min_val is not None: 97 | self._min_val = min_val 98 | if max_val is not None: 99 | self._max_val = max_val 100 | if reverse is not None: 101 | self._reverse = -1 if reverse else 1 102 | if range_mode is not None: 103 | self._range_mode = range_mode 104 | self._state = _R_START 105 | 106 | # enable DT and CLK pin interrupts 107 | self._hal_enable_irq() 108 | 109 | def value(self): 110 | return self._value 111 | 112 | def reset(self): 113 | self._value = 0 114 | 115 | def close(self): 116 | self._hal_close() 117 | 118 | def add_listener(self, l): 119 | self._listener.append(l) 120 | 121 | def remove_listener(self, l): 122 | if l not in self._listener: 123 | raise ValueError('{} is not an installed listener'.format(l)) 124 | self._listener.remove(l) 125 | 126 | def _process_rotary_pins(self, pin): 127 | old_value = self._value 128 | clk_dt_pins = (self._hal_get_clk_value() << 129 | 1) | self._hal_get_dt_value() 130 | 131 | if self._invert: 132 | clk_dt_pins = ~clk_dt_pins & 0x03 133 | 134 | # Determine next state 135 | if self._half_step: 136 | self._state = _transition_table_half_step[self._state & 137 | _STATE_MASK][clk_dt_pins] 138 | else: 139 | self._state = _transition_table[self._state & 140 | _STATE_MASK][clk_dt_pins] 141 | direction = self._state & _DIR_MASK 142 | 143 | incr = 0 144 | if direction == _DIR_CW: 145 | incr = 1 146 | elif direction == _DIR_CCW: 147 | incr = -1 148 | 149 | incr *= self._reverse 150 | 151 | if self._range_mode == self.RANGE_WRAP: 152 | self._value = _wrap( 153 | self._value, 154 | incr, 155 | self._min_val, 156 | self._max_val) 157 | elif self._range_mode == self.RANGE_BOUNDED: 158 | self._value = _bound( 159 | self._value, 160 | incr, 161 | self._min_val, 162 | self._max_val) 163 | else: 164 | self._value = self._value + incr 165 | 166 | try: 167 | if old_value != self._value and len(self._listener) != 0: 168 | micropython.schedule(_trigger, self) 169 | except: 170 | pass 171 | -------------------------------------------------------------------------------- /rotary/rotary_irq_esp.py: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | # Copyright (c) 2020 Mike Teachman 3 | # https://opensource.org/licenses/MIT 4 | 5 | # Platform-specific MicroPython code for the rotary encoder module 6 | # ESP8266/ESP32 implementation 7 | 8 | # Documentation: 9 | # https://github.com/MikeTeachman/micropython-rotary 10 | 11 | from machine import Pin 12 | from rotary import Rotary 13 | from sys import platform 14 | 15 | _esp8266_deny_pins = [16] 16 | 17 | 18 | class RotaryIRQ(Rotary): 19 | 20 | def __init__(self, pin_num_clk, pin_num_dt, min_val=0, max_val=10, 21 | reverse=False, range_mode=Rotary.RANGE_UNBOUNDED, pull_up=False, half_step=False, invert=False): 22 | 23 | if platform == 'esp8266': 24 | if pin_num_clk in _esp8266_deny_pins: 25 | raise ValueError( 26 | '%s: Pin %d not allowed. Not Available for Interrupt: %s' % 27 | (platform, pin_num_clk, _esp8266_deny_pins)) 28 | if pin_num_dt in _esp8266_deny_pins: 29 | raise ValueError( 30 | '%s: Pin %d not allowed. Not Available for Interrupt: %s' % 31 | (platform, pin_num_dt, _esp8266_deny_pins)) 32 | 33 | super().__init__(min_val, max_val, reverse, range_mode, half_step, invert) 34 | 35 | if pull_up == True: 36 | self._pin_clk = Pin(pin_num_clk, Pin.IN, Pin.PULL_UP) 37 | self._pin_dt = Pin(pin_num_dt, Pin.IN, Pin.PULL_UP) 38 | else: 39 | self._pin_clk = Pin(pin_num_clk, Pin.IN) 40 | self._pin_dt = Pin(pin_num_dt, Pin.IN) 41 | 42 | self._enable_clk_irq(self._process_rotary_pins) 43 | self._enable_dt_irq(self._process_rotary_pins) 44 | 45 | def _enable_clk_irq(self, callback=None): 46 | self._pin_clk.irq( 47 | trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, 48 | handler=callback) 49 | 50 | def _enable_dt_irq(self, callback=None): 51 | self._pin_dt.irq( 52 | trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, 53 | handler=callback) 54 | 55 | def _disable_clk_irq(self): 56 | self._pin_clk.irq(handler=None) 57 | 58 | def _disable_dt_irq(self): 59 | self._pin_dt.irq(handler=None) 60 | 61 | def _hal_get_clk_value(self): 62 | return self._pin_clk.value() 63 | 64 | def _hal_get_dt_value(self): 65 | return self._pin_dt.value() 66 | 67 | def _hal_enable_irq(self): 68 | self._enable_clk_irq(self._process_rotary_pins) 69 | self._enable_dt_irq(self._process_rotary_pins) 70 | 71 | def _hal_disable_irq(self): 72 | self._disable_clk_irq() 73 | self._disable_dt_irq() 74 | 75 | def _hal_close(self): 76 | self._hal_disable_irq() 77 | -------------------------------------------------------------------------------- /rotary/rotary_irq_pyb.py: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | # Copyright (c) 2020 Mike Teachman 3 | # https://opensource.org/licenses/MIT 4 | 5 | # Platform-specific MicroPython code for the rotary encoder module 6 | # Pyboard implementation 7 | 8 | # Documentation: 9 | # https://github.com/MikeTeachman/micropython-rotary 10 | 11 | import os 12 | from pyb import Pin 13 | from pyb import ExtInt 14 | from rotary import Rotary 15 | 16 | 17 | class RotaryIRQ(Rotary): 18 | 19 | def __init__(self, pin_num_clk, pin_num_dt, min_val=0, max_val=10, 20 | reverse=False, range_mode=Rotary.RANGE_UNBOUNDED, pull_up=False, half_step=False, invert=False): 21 | super().__init__(min_val, max_val, reverse, range_mode, half_step, invert) 22 | 23 | if pull_up == True: 24 | self._pin_clk = Pin(pin_num_clk, Pin.IN, Pin.PULL_UP) 25 | self._pin_dt = Pin(pin_num_dt, Pin.IN, Pin.PULL_UP) 26 | else: 27 | self._pin_clk = Pin(pin_num_clk, Pin.IN) 28 | self._pin_dt = Pin(pin_num_dt, Pin.IN) 29 | 30 | self._pin_clk_irq = ExtInt( 31 | pin_num_clk, 32 | ExtInt.IRQ_RISING_FALLING, 33 | Pin.PULL_NONE, 34 | self._process_rotary_pins) 35 | self._pin_dt_irq = ExtInt( 36 | pin_num_dt, 37 | ExtInt.IRQ_RISING_FALLING, 38 | Pin.PULL_NONE, 39 | self._process_rotary_pins) 40 | 41 | # turn on 3.3V output to power the rotary encoder (pyboard D only) 42 | if 'PYBD' in os.uname().machine: 43 | Pin('EN_3V3').value(1) 44 | 45 | def _enable_clk_irq(self): 46 | self._pin_clk_irq.enable() 47 | 48 | def _enable_dt_irq(self): 49 | self._pin_dt_irq.enable() 50 | 51 | def _disable_clk_irq(self): 52 | self._pin_clk_irq.disable() 53 | 54 | def _disable_dt_irq(self): 55 | self._pin_dt_irq.disable() 56 | 57 | def _hal_get_clk_value(self): 58 | return self._pin_clk.value() 59 | 60 | def _hal_get_dt_value(self): 61 | return self._pin_dt.value() 62 | 63 | def _hal_enable_irq(self): 64 | self._enable_clk_irq() 65 | self._enable_dt_irq() 66 | 67 | def _hal_disable_irq(self): 68 | self._disable_clk_irq() 69 | self._disable_dt_irq() 70 | 71 | def _hal_close(self): 72 | self._hal_disable_irq() 73 | -------------------------------------------------------------------------------- /rotary/rotary_irq_rp2.py: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | # Copyright (c) 2020 Mike Teachman 3 | # Copyright (c) 2021 Eric Moyer 4 | # https://opensource.org/licenses/MIT 5 | 6 | # Platform-specific MicroPython code for the rotary encoder module 7 | # Raspberry Pi Pico implementation 8 | 9 | # Documentation: 10 | # https://github.com/MikeTeachman/micropython-rotary 11 | 12 | from machine import Pin 13 | from rotary import Rotary 14 | 15 | IRQ_RISING_FALLING = Pin.IRQ_RISING | Pin.IRQ_FALLING 16 | 17 | 18 | class RotaryIRQ(Rotary): 19 | def __init__( 20 | self, 21 | pin_num_clk, 22 | pin_num_dt, 23 | min_val=0, 24 | max_val=10, 25 | reverse=False, 26 | range_mode=Rotary.RANGE_UNBOUNDED, 27 | pull_up=False, 28 | half_step=False, 29 | invert=False, 30 | ): 31 | super().__init__(min_val, max_val, reverse, range_mode, half_step, invert) 32 | 33 | if pull_up: 34 | self._pin_clk = Pin(pin_num_clk, Pin.IN, Pin.PULL_UP) 35 | self._pin_dt = Pin(pin_num_dt, Pin.IN, Pin.PULL_UP) 36 | else: 37 | self._pin_clk = Pin(pin_num_clk, Pin.IN) 38 | self._pin_dt = Pin(pin_num_dt, Pin.IN) 39 | 40 | self._hal_enable_irq() 41 | 42 | def _enable_clk_irq(self): 43 | self._pin_clk.irq(self._process_rotary_pins, IRQ_RISING_FALLING) 44 | 45 | def _enable_dt_irq(self): 46 | self._pin_dt.irq(self._process_rotary_pins, IRQ_RISING_FALLING) 47 | 48 | def _disable_clk_irq(self): 49 | self._pin_clk.irq(None, 0) 50 | 51 | def _disable_dt_irq(self): 52 | self._pin_dt.irq(None, 0) 53 | 54 | def _hal_get_clk_value(self): 55 | return self._pin_clk.value() 56 | 57 | def _hal_get_dt_value(self): 58 | return self._pin_dt.value() 59 | 60 | def _hal_enable_irq(self): 61 | self._enable_clk_irq() 62 | self._enable_dt_irq() 63 | 64 | def _hal_disable_irq(self): 65 | self._disable_clk_irq() 66 | self._disable_dt_irq() 67 | 68 | def _hal_close(self): 69 | self._hal_disable_irq() 70 | -------------------------------------------------------------------------------- /rp2-pico-20210206-unstable-v1.14-9-g9dedcf122.uf2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/rp2-pico-20210206-unstable-v1.14-9-g9dedcf122.uf2 -------------------------------------------------------------------------------- /rp2-pico-20220117-v1.18.uf2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/rp2-pico-20220117-v1.18.uf2 -------------------------------------------------------------------------------- /ssd1306/badapple25s/main.py: -------------------------------------------------------------------------------- 1 | from machine import I2C,Pin 2 | from ssd1306 import SSD1306_I2C#I2C的oled选该方法 3 | import framebuf 4 | import ujson as json 5 | import time 6 | i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000) 7 | oled = SSD1306_I2C(128, 64, i2c) #你的OLED分辨率,使用I2C 8 | oled.fill(1) #清空屏幕 9 | oled.show() 10 | time.sleep(3) 11 | def image(img_list): 12 | e=time.ticks_ms() 13 | oled.fill(0) 14 | for i in img_list: 15 | oled.hline(i[0],i[1],i[2],1) 16 | oled.show() 17 | d=time.ticks_ms() 18 | q=100-(d-e) 19 | if q>0: 20 | time.sleep(q/1000) 21 | with open('bad.data','r') as f: 22 | c=0 23 | for i in f: 24 | c=c+1 25 | try: 26 | z=json.loads(i) 27 | image(z) 28 | except Exception as e: 29 | pass -------------------------------------------------------------------------------- /ssd1306/badapple25s/ssd1306.py: -------------------------------------------------------------------------------- 1 | # MicroPython SSD1306 OLED driver, I2C and SPI interfaces 2 | 3 | from micropython import const 4 | import framebuf 5 | 6 | 7 | # register definitions 8 | SET_CONTRAST = const(0x81) 9 | SET_ENTIRE_ON = const(0xA4) 10 | SET_NORM_INV = const(0xA6) 11 | SET_DISP = const(0xAE) 12 | SET_MEM_ADDR = const(0x20) 13 | SET_COL_ADDR = const(0x21) 14 | SET_PAGE_ADDR = const(0x22) 15 | SET_DISP_START_LINE = const(0x40) 16 | SET_SEG_REMAP = const(0xA0) 17 | SET_MUX_RATIO = const(0xA8) 18 | SET_COM_OUT_DIR = const(0xC0) 19 | SET_DISP_OFFSET = const(0xD3) 20 | SET_COM_PIN_CFG = const(0xDA) 21 | SET_DISP_CLK_DIV = const(0xD5) 22 | SET_PRECHARGE = const(0xD9) 23 | SET_VCOM_DESEL = const(0xDB) 24 | SET_CHARGE_PUMP = const(0x8D) 25 | 26 | # Subclassing FrameBuffer provides support for graphics primitives 27 | # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html 28 | class SSD1306(framebuf.FrameBuffer): 29 | def __init__(self, width, height, external_vcc): 30 | self.width = width 31 | self.height = height 32 | self.external_vcc = external_vcc 33 | self.pages = self.height // 8 34 | self.buffer = bytearray(self.pages * self.width) 35 | super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB) 36 | self.init_display() 37 | 38 | def init_display(self): 39 | for cmd in ( 40 | SET_DISP | 0x00, # off 41 | # address setting 42 | SET_MEM_ADDR, 43 | 0x00, # horizontal 44 | # resolution and layout 45 | SET_DISP_START_LINE | 0x00, 46 | SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 47 | SET_MUX_RATIO, 48 | self.height - 1, 49 | SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0 50 | SET_DISP_OFFSET, 51 | 0x00, 52 | SET_COM_PIN_CFG, 53 | 0x02 if self.width > 2 * self.height else 0x12, 54 | # timing and driving scheme 55 | SET_DISP_CLK_DIV, 56 | 0x80, 57 | SET_PRECHARGE, 58 | 0x22 if self.external_vcc else 0xF1, 59 | SET_VCOM_DESEL, 60 | 0x30, # 0.83*Vcc 61 | # display 62 | SET_CONTRAST, 63 | 0xFF, # maximum 64 | SET_ENTIRE_ON, # output follows RAM contents 65 | SET_NORM_INV, # not inverted 66 | # charge pump 67 | SET_CHARGE_PUMP, 68 | 0x10 if self.external_vcc else 0x14, 69 | SET_DISP | 0x01, 70 | ): # on 71 | self.write_cmd(cmd) 72 | self.fill(0) 73 | self.show() 74 | 75 | def poweroff(self): 76 | self.write_cmd(SET_DISP | 0x00) 77 | 78 | def poweron(self): 79 | self.write_cmd(SET_DISP | 0x01) 80 | 81 | def contrast(self, contrast): 82 | self.write_cmd(SET_CONTRAST) 83 | self.write_cmd(contrast) 84 | 85 | def invert(self, invert): 86 | self.write_cmd(SET_NORM_INV | (invert & 1)) 87 | 88 | def show(self): 89 | x0 = 0 90 | x1 = self.width - 1 91 | if self.width == 64: 92 | # displays with width of 64 pixels are shifted by 32 93 | x0 += 32 94 | x1 += 32 95 | self.write_cmd(SET_COL_ADDR) 96 | self.write_cmd(x0) 97 | self.write_cmd(x1) 98 | self.write_cmd(SET_PAGE_ADDR) 99 | self.write_cmd(0) 100 | self.write_cmd(self.pages - 1) 101 | self.write_data(self.buffer) 102 | 103 | 104 | class SSD1306_I2C(SSD1306): 105 | def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False): 106 | self.i2c = i2c 107 | self.addr = addr 108 | self.temp = bytearray(2) 109 | self.write_list = [b"\x40", None] # Co=0, D/C#=1 110 | super().__init__(width, height, external_vcc) 111 | 112 | def write_cmd(self, cmd): 113 | self.temp[0] = 0x80 # Co=1, D/C#=0 114 | self.temp[1] = cmd 115 | self.i2c.writeto(self.addr, self.temp) 116 | 117 | def write_data(self, buf): 118 | self.write_list[1] = buf 119 | self.i2c.writevto(self.addr, self.write_list) 120 | 121 | 122 | class SSD1306_SPI(SSD1306): 123 | def __init__(self, width, height, spi, dc, res, cs, external_vcc=False): 124 | self.rate = 10 * 1024 * 1024 125 | dc.init(dc.OUT, value=0) 126 | res.init(res.OUT, value=0) 127 | cs.init(cs.OUT, value=1) 128 | self.spi = spi 129 | self.dc = dc 130 | self.res = res 131 | self.cs = cs 132 | import time 133 | 134 | self.res(1) 135 | time.sleep_ms(1) 136 | self.res(0) 137 | time.sleep_ms(10) 138 | self.res(1) 139 | super().__init__(width, height, external_vcc) 140 | 141 | def write_cmd(self, cmd): 142 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 143 | self.cs(1) 144 | self.dc(0) 145 | self.cs(0) 146 | self.spi.write(bytearray([cmd])) 147 | self.cs(1) 148 | 149 | def write_data(self, buf): 150 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 151 | self.cs(1) 152 | self.dc(1) 153 | self.cs(0) 154 | self.spi.write(buf) 155 | self.cs(1) -------------------------------------------------------------------------------- /ssd1306/badapplecz/main.py: -------------------------------------------------------------------------------- 1 | from machine import I2C,Pin 2 | from ssd1306 import SSD1306_I2C#I2C的oled选该方法 3 | import framebuf 4 | import ujson as json 5 | import time 6 | i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=2000000) 7 | oled = SSD1306_I2C(128, 64, i2c) #你的OLED分辨率,使用I2C 8 | oled.fill(1) #清空屏幕 9 | oled.show() 10 | 11 | def image(img_list): 12 | e=time.ticks_ms() 13 | oled.fill(0) 14 | for i in img_list: 15 | oled.hline(2*i[0],2*i[1],2*i[2],1) 16 | oled.hline(2*i[0],2*i[1]+1,2*i[2],1) 17 | oled.show() 18 | d=time.ticks_ms() 19 | q=100-(d-e) 20 | if q>0: 21 | time.sleep(q/1000) 22 | with open('bad.data','r') as f: 23 | c=0 24 | for i in f: 25 | c=c+1 26 | try: 27 | z=json.loads(i) 28 | image(z) 29 | except Exception as e: 30 | pass -------------------------------------------------------------------------------- /ssd1306/main.py: -------------------------------------------------------------------------------- 1 | from machine import I2C,Pin 2 | from ssd1306 import SSD1306_I2C#I2C的oled选该方法 3 | i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000) 4 | oled = SSD1306_I2C(128, 64, i2c) #你的OLED分辨率,使用I2C 5 | oled.fill(1) #清空屏幕 6 | oled.show() 7 | oled.fill(0) 8 | oled.show() 9 | 10 | oled.text("123",0,9,1) 11 | oled.show() 12 | 13 | oled.pixel(15,15,1) 14 | oled.show() 15 | 16 | 17 | 18 | 19 | # oled.pixel(x,y,z) 20 | # x y处画点 21 | # oled.line(x,y,x2,y2,c)直线 22 | # oled.vline(x,y,l,c) 23 | # oled.hline(x,y,l,c) -------------------------------------------------------------------------------- /ssd1306/pbm/WeAct Studio Download Tool 烧录教程.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/ssd1306/pbm/WeAct Studio Download Tool 烧录教程.mp4 -------------------------------------------------------------------------------- /ssd1306/pbm/a.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/ssd1306/pbm/a.jpg -------------------------------------------------------------------------------- /ssd1306/pbm/a.pbm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/ssd1306/pbm/a.pbm -------------------------------------------------------------------------------- /ssd1306/pbm/b.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/ssd1306/pbm/b.jpg -------------------------------------------------------------------------------- /ssd1306/pbm/b.pbm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/ssd1306/pbm/b.pbm -------------------------------------------------------------------------------- /ssd1306/pbm/lifegame-main (1).zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/ssd1306/pbm/lifegame-main (1).zip -------------------------------------------------------------------------------- /ssd1306/pbm/lifegame-main.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/ssd1306/pbm/lifegame-main.zip -------------------------------------------------------------------------------- /ssd1306/pbm/main.py: -------------------------------------------------------------------------------- 1 | from machine import I2C,Pin 2 | from ssd1306 import SSD1306_I2C#I2C的oled选该方法 3 | import framebuf 4 | 5 | i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000) 6 | oled = SSD1306_I2C(128, 64, i2c) #你的OLED分辨率,使用I2C 7 | oled.fill(1) #清空屏幕 8 | oled.show() 9 | 10 | with open("a.pbm",'rb') as f : 11 | f.readline() 12 | f.readline() 13 | data = bytearray(f.read()) 14 | fbuf = framebuf.FrameBuffer(data,86,64,framebuf.MONO_HLSB) 15 | # fbuf = framebuf.FrameBuffer(data,128,64,framebuf.MONO_HLSB) 16 | oled.fill(0) 17 | oled.blit(fbuf,0,0) 18 | oled.show() -------------------------------------------------------------------------------- /ssd1306/pbm/pbm.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | from pathlib import Path 3 | 4 | def savePBM(filename,x=128,y=64): 5 | e=Path(filename) 6 | if e.is_file: 7 | if e.suffix.upper() in [".JPG",".JPEG",".PNG",".BMP",".GIF"]: 8 | f=Image.open(filename) 9 | f=f.resize((x,y)) 10 | l=f.convert('1') 11 | l.show() 12 | pbm=e.stem+".pbm" 13 | l.save(pbm,"PPM") 14 | print(pbm,Path(pbm).stat().st_size) 15 | return True 16 | else: 17 | print("请导入图片文件!") 18 | else: 19 | print("文件不存在") 20 | return False 21 | 22 | 23 | savePBM("b.jpg",86,64) -------------------------------------------------------------------------------- /ssd1306/ssd1306.py: -------------------------------------------------------------------------------- 1 | # MicroPython SSD1306 OLED driver, I2C and SPI interfaces 2 | 3 | from micropython import const 4 | import framebuf 5 | 6 | 7 | # register definitions 8 | SET_CONTRAST = const(0x81) 9 | SET_ENTIRE_ON = const(0xA4) 10 | SET_NORM_INV = const(0xA6) 11 | SET_DISP = const(0xAE) 12 | SET_MEM_ADDR = const(0x20) 13 | SET_COL_ADDR = const(0x21) 14 | SET_PAGE_ADDR = const(0x22) 15 | SET_DISP_START_LINE = const(0x40) 16 | SET_SEG_REMAP = const(0xA0) 17 | SET_MUX_RATIO = const(0xA8) 18 | SET_COM_OUT_DIR = const(0xC0) 19 | SET_DISP_OFFSET = const(0xD3) 20 | SET_COM_PIN_CFG = const(0xDA) 21 | SET_DISP_CLK_DIV = const(0xD5) 22 | SET_PRECHARGE = const(0xD9) 23 | SET_VCOM_DESEL = const(0xDB) 24 | SET_CHARGE_PUMP = const(0x8D) 25 | 26 | # Subclassing FrameBuffer provides support for graphics primitives 27 | # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html 28 | class SSD1306(framebuf.FrameBuffer): 29 | def __init__(self, width, height, external_vcc): 30 | self.width = width 31 | self.height = height 32 | self.external_vcc = external_vcc 33 | self.pages = self.height // 8 34 | self.buffer = bytearray(self.pages * self.width) 35 | super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB) 36 | self.init_display() 37 | 38 | def init_display(self): 39 | for cmd in ( 40 | SET_DISP | 0x00, # off 41 | # address setting 42 | SET_MEM_ADDR, 43 | 0x00, # horizontal 44 | # resolution and layout 45 | SET_DISP_START_LINE | 0x00, 46 | SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 47 | SET_MUX_RATIO, 48 | self.height - 1, 49 | SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0 50 | SET_DISP_OFFSET, 51 | 0x00, 52 | SET_COM_PIN_CFG, 53 | 0x02 if self.width > 2 * self.height else 0x12, 54 | # timing and driving scheme 55 | SET_DISP_CLK_DIV, 56 | 0x80, 57 | SET_PRECHARGE, 58 | 0x22 if self.external_vcc else 0xF1, 59 | SET_VCOM_DESEL, 60 | 0x30, # 0.83*Vcc 61 | # display 62 | SET_CONTRAST, 63 | 0xFF, # maximum 64 | SET_ENTIRE_ON, # output follows RAM contents 65 | SET_NORM_INV, # not inverted 66 | # charge pump 67 | SET_CHARGE_PUMP, 68 | 0x10 if self.external_vcc else 0x14, 69 | SET_DISP | 0x01, 70 | ): # on 71 | self.write_cmd(cmd) 72 | self.fill(0) 73 | self.show() 74 | 75 | def poweroff(self): 76 | self.write_cmd(SET_DISP | 0x00) 77 | 78 | def poweron(self): 79 | self.write_cmd(SET_DISP | 0x01) 80 | 81 | def contrast(self, contrast): 82 | self.write_cmd(SET_CONTRAST) 83 | self.write_cmd(contrast) 84 | 85 | def invert(self, invert): 86 | self.write_cmd(SET_NORM_INV | (invert & 1)) 87 | 88 | def show(self): 89 | x0 = 0 90 | x1 = self.width - 1 91 | if self.width == 64: 92 | # displays with width of 64 pixels are shifted by 32 93 | x0 += 32 94 | x1 += 32 95 | self.write_cmd(SET_COL_ADDR) 96 | self.write_cmd(x0) 97 | self.write_cmd(x1) 98 | self.write_cmd(SET_PAGE_ADDR) 99 | self.write_cmd(0) 100 | self.write_cmd(self.pages - 1) 101 | self.write_data(self.buffer) 102 | 103 | 104 | class SSD1306_I2C(SSD1306): 105 | def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False): 106 | self.i2c = i2c 107 | self.addr = addr 108 | self.temp = bytearray(2) 109 | self.write_list = [b"\x40", None] # Co=0, D/C#=1 110 | super().__init__(width, height, external_vcc) 111 | 112 | def write_cmd(self, cmd): 113 | self.temp[0] = 0x80 # Co=1, D/C#=0 114 | self.temp[1] = cmd 115 | self.i2c.writeto(self.addr, self.temp) 116 | 117 | def write_data(self, buf): 118 | self.write_list[1] = buf 119 | self.i2c.writevto(self.addr, self.write_list) 120 | 121 | 122 | class SSD1306_SPI(SSD1306): 123 | def __init__(self, width, height, spi, dc, res, cs, external_vcc=False): 124 | self.rate = 10 * 1024 * 1024 125 | dc.init(dc.OUT, value=0) 126 | res.init(res.OUT, value=0) 127 | cs.init(cs.OUT, value=1) 128 | self.spi = spi 129 | self.dc = dc 130 | self.res = res 131 | self.cs = cs 132 | import time 133 | 134 | self.res(1) 135 | time.sleep_ms(1) 136 | self.res(0) 137 | time.sleep_ms(10) 138 | self.res(1) 139 | super().__init__(width, height, external_vcc) 140 | 141 | def write_cmd(self, cmd): 142 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 143 | self.cs(1) 144 | self.dc(0) 145 | self.cs(0) 146 | self.spi.write(bytearray([cmd])) 147 | self.cs(1) 148 | 149 | def write_data(self, buf): 150 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 151 | self.cs(1) 152 | self.dc(1) 153 | self.cs(0) 154 | self.spi.write(buf) 155 | self.cs(1) -------------------------------------------------------------------------------- /tm1637/main.py: -------------------------------------------------------------------------------- 1 | import tm1637 2 | from machine import Pin 3 | tm = tm1637.TM1637(clk=Pin(0), dio=Pin(1)) 4 | 5 | tm.numbers(12, 59) 6 | tm.number(-123) 7 | # tm.temperature(24) 不建议使用显示温度 8 | 9 | # all LEDS on "88:88" 10 | tm.write([127, 255, 127, 127]) 11 | # all LEDS off 12 | tm.write([0, 0, 0, 0]) 13 | #一个列表每个代表一个led晶体管 14 | # 数字8 由7个led管组成 15 | # 8: 由8个led管组成 -------------------------------------------------------------------------------- /tm1637/tm1637.py: -------------------------------------------------------------------------------- 1 | """ 2 | MicroPython TM1637 quad 7-segment LED display driver 3 | https://github.com/mcauser/micropython-tm1637 4 | 5 | MIT License 6 | Copyright (c) 2016 Mike Causer 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in all 16 | copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | SOFTWARE. 25 | """ 26 | 27 | from micropython import const 28 | from machine import Pin 29 | from time import sleep_us, sleep_ms 30 | 31 | TM1637_CMD1 = const(64) # 0x40 data command 32 | TM1637_CMD2 = const(192) # 0xC0 address command 33 | TM1637_CMD3 = const(128) # 0x80 display control command 34 | TM1637_DSP_ON = const(8) # 0x08 display on 35 | TM1637_DELAY = const(10) # 10us delay between clk/dio pulses 36 | TM1637_MSB = const(128) # msb is the decimal point or the colon depending on your display 37 | 38 | # 0-9, a-z, blank, dash, star 39 | _SEGMENTS = bytearray(b'\x3F\x06\x5B\x4F\x66\x6D\x7D\x07\x7F\x6F\x77\x7C\x39\x5E\x79\x71\x3D\x76\x06\x1E\x76\x38\x55\x54\x3F\x73\x67\x50\x6D\x78\x3E\x1C\x2A\x76\x6E\x5B\x00\x40\x63') 40 | 41 | class TM1637(object): 42 | """Library for quad 7-segment LED modules based on the TM1637 LED driver.""" 43 | def __init__(self, clk, dio, brightness=7): 44 | self.clk = clk 45 | self.dio = dio 46 | 47 | if not 0 <= brightness <= 7: 48 | raise ValueError("Brightness out of range") 49 | self._brightness = brightness 50 | 51 | self.clk.init(Pin.OUT, value=0) 52 | self.dio.init(Pin.OUT, value=0) 53 | sleep_us(TM1637_DELAY) 54 | 55 | self._write_data_cmd() 56 | self._write_dsp_ctrl() 57 | 58 | def _start(self): 59 | self.dio(0) 60 | sleep_us(TM1637_DELAY) 61 | self.clk(0) 62 | sleep_us(TM1637_DELAY) 63 | 64 | def _stop(self): 65 | self.dio(0) 66 | sleep_us(TM1637_DELAY) 67 | self.clk(1) 68 | sleep_us(TM1637_DELAY) 69 | self.dio(1) 70 | 71 | def _write_data_cmd(self): 72 | # automatic address increment, normal mode 73 | self._start() 74 | self._write_byte(TM1637_CMD1) 75 | self._stop() 76 | 77 | def _write_dsp_ctrl(self): 78 | # display on, set brightness 79 | self._start() 80 | self._write_byte(TM1637_CMD3 | TM1637_DSP_ON | self._brightness) 81 | self._stop() 82 | 83 | def _write_byte(self, b): 84 | for i in range(8): 85 | self.dio((b >> i) & 1) 86 | sleep_us(TM1637_DELAY) 87 | self.clk(1) 88 | sleep_us(TM1637_DELAY) 89 | self.clk(0) 90 | sleep_us(TM1637_DELAY) 91 | self.clk(0) 92 | sleep_us(TM1637_DELAY) 93 | self.clk(1) 94 | sleep_us(TM1637_DELAY) 95 | self.clk(0) 96 | sleep_us(TM1637_DELAY) 97 | 98 | def brightness(self, val=None): 99 | """Set the display brightness 0-7.""" 100 | # brightness 0 = 1/16th pulse width 101 | # brightness 7 = 14/16th pulse width 102 | if val is None: 103 | return self._brightness 104 | if not 0 <= val <= 7: 105 | raise ValueError("Brightness out of range") 106 | 107 | self._brightness = val 108 | self._write_data_cmd() 109 | self._write_dsp_ctrl() 110 | 111 | def write(self, segments, pos=0): 112 | """Display up to 6 segments moving right from a given position. 113 | The MSB in the 2nd segment controls the colon between the 2nd 114 | and 3rd segments.""" 115 | if not 0 <= pos <= 5: 116 | raise ValueError("Position out of range") 117 | self._write_data_cmd() 118 | self._start() 119 | 120 | self._write_byte(TM1637_CMD2 | pos) 121 | for seg in segments: 122 | self._write_byte(seg) 123 | self._stop() 124 | self._write_dsp_ctrl() 125 | 126 | def encode_digit(self, digit): 127 | """Convert a character 0-9, a-f to a segment.""" 128 | return _SEGMENTS[digit & 0x0f] 129 | 130 | def encode_string(self, string): 131 | """Convert an up to 4 character length string containing 0-9, a-z, 132 | space, dash, star to an array of segments, matching the length of the 133 | source string.""" 134 | segments = bytearray(len(string)) 135 | for i in range(len(string)): 136 | segments[i] = self.encode_char(string[i]) 137 | return segments 138 | 139 | def encode_char(self, char): 140 | """Convert a character 0-9, a-z, space, dash or star to a segment.""" 141 | o = ord(char) 142 | if o == 32: 143 | return _SEGMENTS[36] # space 144 | if o == 42: 145 | return _SEGMENTS[38] # star/degrees 146 | if o == 45: 147 | return _SEGMENTS[37] # dash 148 | if o >= 65 and o <= 90: 149 | return _SEGMENTS[o-55] # uppercase A-Z 150 | if o >= 97 and o <= 122: 151 | return _SEGMENTS[o-87] # lowercase a-z 152 | if o >= 48 and o <= 57: 153 | return _SEGMENTS[o-48] # 0-9 154 | raise ValueError("Character out of range: {:d} '{:s}'".format(o, chr(o))) 155 | 156 | def hex(self, val): 157 | """Display a hex value 0x0000 through 0xffff, right aligned.""" 158 | string = '{:04x}'.format(val & 0xffff) 159 | self.write(self.encode_string(string)) 160 | 161 | def number(self, num): 162 | """Display a numeric value -999 through 9999, right aligned.""" 163 | # limit to range -999 to 9999 164 | num = max(-999, min(num, 9999)) 165 | string = '{0: >4d}'.format(num) 166 | self.write(self.encode_string(string)) 167 | 168 | def numbers(self, num1, num2, colon=True): 169 | """Display two numeric values -9 through 99, with leading zeros 170 | and separated by a colon.""" 171 | num1 = max(-9, min(num1, 99)) 172 | num2 = max(-9, min(num2, 99)) 173 | segments = self.encode_string('{0:0>2d}{1:0>2d}'.format(num1, num2)) 174 | if colon: 175 | segments[1] |= 0x80 # colon on 176 | self.write(segments) 177 | 178 | def temperature(self, num): 179 | if num < -9: 180 | self.show('lo') # low 181 | elif num > 99: 182 | self.show('hi') # high 183 | else: 184 | string = '{0: >2d}'.format(num) 185 | self.write(self.encode_string(string)) 186 | self.write([_SEGMENTS[38], _SEGMENTS[12]], 2) # degrees C 187 | 188 | def show(self, string, colon=False): 189 | segments = self.encode_string(string) 190 | if len(segments) > 1 and colon: 191 | segments[1] |= 128 192 | self.write(segments[:4]) 193 | 194 | def scroll(self, string, delay=250): 195 | segments = string if isinstance(string, list) else self.encode_string(string) 196 | data = [0] * 8 197 | data[4:0] = list(segments) 198 | for i in range(len(segments) + 5): 199 | self.write(data[0+i:4+i]) 200 | sleep_ms(delay) 201 | 202 | 203 | class TM1637Decimal(TM1637): 204 | """Library for quad 7-segment LED modules based on the TM1637 LED driver. 205 | 206 | This class is meant to be used with decimal display modules (modules 207 | that have a decimal point after each 7-segment LED). 208 | """ 209 | 210 | def encode_string(self, string): 211 | """Convert a string to LED segments. 212 | 213 | Convert an up to 4 character length string containing 0-9, a-z, 214 | space, dash, star and '.' to an array of segments, matching the length of 215 | the source string.""" 216 | segments = bytearray(len(string.replace('.',''))) 217 | j = 0 218 | for i in range(len(string)): 219 | if string[i] == '.' and j > 0: 220 | segments[j-1] |= TM1637_MSB 221 | continue 222 | segments[j] = self.encode_char(string[i]) 223 | j += 1 224 | return segments 225 | -------------------------------------------------------------------------------- /wifi/LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /wifi/README.md: -------------------------------------------------------------------------------- 1 | # wifiat_pico 2 | PICO use WIFI ESP8266 AT V1.1 3 | 4 | 5 | ## 介绍 6 | 7 | 该项目参考于[Raspberry-Pi-Pico-Wifi](https://github.com/tkraspi/Raspberry-Pi-Pico-Wifi) 8 | ESP01 的版本号为1.1 版本较老,未测试其他版本的esp01,使用时请按照上面的连线测试 9 | 10 | 在HTTP请求不同域名的时候请先执行restart 11 | 12 | 13 | ## 连线 14 | |PICO|ESP826601| 15 | |-|-| 16 | |GPIO0|RX| 17 | |GPIO1|TX| 18 | |3V3|3v3| 19 | |3v3|EN| 20 | |GND|GND| 21 | 22 | ## 测试代码 23 | 24 | ``` 25 | import utime 26 | from machine import UART 27 | from wifiat import at 28 | uart=UART(0,115200) 29 | AT=at(uart) 30 | 31 | AT.info() 32 | 33 | AT.restore() 34 | #恢复出厂设置 35 | 36 | AT.restart() 37 | 38 | 39 | AT.netinfo() 40 | 41 | AT.connect("wifi_name","wifi_password") 42 | 43 | AT.netinfo() 44 | 45 | AT.ping("baidu.com") 46 | 47 | get=AT.http("http://httpbin.org/get") 48 | 49 | post=AT.http("http://httpbin.org/post",method="POST",data='{"name":'tom',"AGE":'12'}',timeout=4) 50 | 51 | print(get) 52 | 53 | print(post) 54 | 55 | AT.restart() 56 | #使用不同域名时请先restart 57 | 58 | utime.sleep(3) 59 | get=AT.http("http://www.baidu.com") 60 | 61 | 62 | #默认执行AT指令的方法 63 | AT.sendCMD_waitResp("AT\r\n") 64 | AT.sendCMD_waitRespLine("AT\r\n") 65 | ``` 66 | 67 | 所有方法指令方法都有返回值请自行判断 68 | 69 | -------------------------------------------------------------------------------- /wifi/wifiat.py: -------------------------------------------------------------------------------- 1 | import utime 2 | 3 | 4 | class at(object): 5 | def __init__(self,uart): 6 | self.uart=uart 7 | def sendCMD_waitResp(self,cmd, timeout=2000): 8 | print("CMD: " + cmd) 9 | self.uart.write(cmd.encode('utf-8')) 10 | return self.waitResp(timeout) 11 | def waitResp(self,timeout=2000): 12 | prvMills = utime.ticks_ms() 13 | resp = b"" 14 | while (utime.ticks_ms()-prvMills) save as and select MicroPython device. Give it the same name). Once it's there, you can import it into your code. 15 | 16 | You create an object with the parameters number of LEDs, state machine ID and GPIO number in that order. so, to create a strip of 10 leds on state machine 0 and GPIO 0, you use: 17 | 18 | ``` 19 | pixels = ws2812b.ws2812b(10,0,0) 20 | ``` 21 | 22 | This object has two methods, show() which sends the data to the strip, and set_pixel which sets the colour values for a particular LED. The parameters are LED number, red, green, blue with the colours taking values between 0 and 255. 23 | 24 | At the moment, this isn't working with the interpreter, so you have to run it from a file. Looks like it's running just too slow to keep up with the PIO buffer from the interpreter. The key methods are set_pixel(r,g,b), set_pixel_line(p1, p2, r, g, b) which sets a row of pixels from pixel p1 to pixel p2 (inclusive), and fill(r,g,b) which fills all the pixels with the colour r,g,b. 25 | 26 | ``` 27 | pixels.set_pixel(5,10,0,0) 28 | pixels.set_pixel_line(5,7,0,10,0) 29 | pixels.fill(20,5,0) 30 | ``` 31 | 32 | Pull requests are open if you'd like more features! 33 | -------------------------------------------------------------------------------- /ws2812/pico_python_ws2812b-main/examples/fireflies.py: -------------------------------------------------------------------------------- 1 | import time 2 | import ws2812b 3 | import random 4 | 5 | numpix = 50 # Number of NeoPixels 6 | # Pin where NeoPixels are connected 7 | strip = ws2812b.ws2812b(numpix, 0,0) 8 | 9 | colors = [ 10 | [232, 100, 255], # Purple 11 | [200, 200, 20], # Yellow 12 | [30, 200, 200], # Blue 13 | [150,50,10], 14 | [50,200,10], 15 | ] 16 | 17 | max_len=20 18 | min_len = 5 19 | #pixelnum, posn in flash, flash_len, direction 20 | flashing = [] 21 | 22 | num_flashes = 10 23 | 24 | for i in range(num_flashes): 25 | pix = random.randint(0, numpix - 1) 26 | col = random.randint(1, len(colors) - 1) 27 | flash_len = random.randint(min_len, max_len) 28 | flashing.append([pix, colors[col], flash_len, 0, 1]) 29 | 30 | strip.fill(0,0,0) 31 | 32 | while True: 33 | strip.show() 34 | for i in range(num_flashes): 35 | 36 | pix = flashing[i][0] 37 | brightness = (flashing[i][3]/flashing[i][2]) 38 | colr = (int(flashing[i][1][0]*brightness), 39 | int(flashing[i][1][1]*brightness), 40 | int(flashing[i][1][2]*brightness)) 41 | strip.set_pixel(pix, colr[0], colr[1], colr[2]) 42 | 43 | if flashing[i][2] == flashing[i][3]: 44 | flashing[i][4] = -1 45 | if flashing[i][3] == 0 and flashing[i][4] == -1: 46 | pix = random.randint(0, numpix - 1) 47 | col = random.randint(0, len(colors) - 1) 48 | flash_len = random.randint(min_len, max_len) 49 | flashing[i] = [pix, colors[col], flash_len, 0, 1] 50 | flashing[i][3] = flashing[i][3] + flashing[i][4] 51 | time.sleep(0.005) 52 | 53 | 54 | -------------------------------------------------------------------------------- /ws2812/pico_python_ws2812b-main/examples/flash.py: -------------------------------------------------------------------------------- 1 | import time 2 | from ws2812b import ws2812b 3 | 4 | num_leds = 30 5 | pixels = ws2812b(num_leds, 0,0, delay=0) 6 | 7 | pixels.fill(10,10,10) 8 | pixels.show() 9 | 10 | while True: 11 | for i in range(num_leds): 12 | for j in range(num_leds): 13 | pixels.set_pixel(j,abs(i+j)%10,abs(i-(j+3))%10,abs(i-(j+6))%10) 14 | pixels.show() 15 | time.sleep(0.05) 16 | 17 | 18 | -------------------------------------------------------------------------------- /ws2812/pico_python_ws2812b-main/examples/rainbow.py: -------------------------------------------------------------------------------- 1 | import time 2 | import ws2812b 3 | 4 | numpix = 30 5 | strip = ws2812b.ws2812b(numpix, 0,0) 6 | 7 | RED = (255, 0, 0) 8 | ORANGE = (255, 165, 0) 9 | YELLOW = (255, 150, 0) 10 | GREEN = (0, 255, 0) 11 | BLUE = (0, 0, 255) 12 | INDIGO = (75, 0, 130) 13 | VIOLET = (138, 43, 226) 14 | COLORS = (RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET) 15 | 16 | while True: 17 | for color in COLORS: 18 | for i in range(numpix): 19 | strip.set_pixel(i, color[0], color[1], color[2]) 20 | time.sleep(0.01) 21 | strip.show() 22 | -------------------------------------------------------------------------------- /ws2812/pico_python_ws2812b-main/examples/thermometer.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import time 3 | from ws2812b import ws2812b 4 | 5 | num_leds = 30 6 | 7 | pixels = ws2812b(num_leds, 0,0) 8 | 9 | sensor_temp = machine.ADC(4) 10 | conversion_factor = 3.3 / 65535 11 | 12 | min_temp = 0 13 | max_temp = 30 14 | 15 | while True: 16 | reading = sensor_temp.read_u16() * conversion_factor 17 | temperature = 27 - (reading - 0.706)/0.001721 18 | temp_int = int(temperature) 19 | for i in range(num_leds): 20 | if i < temp_int: 21 | pixels.set_pixel(i, 10,0,0) 22 | else: 23 | pixels.set_pixel(i,0,0,0) 24 | pixels.show() 25 | time.sleep(2) 26 | -------------------------------------------------------------------------------- /ws2812/pico_python_ws2812b-main/pico_ws2812b.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maysrp/pico_micropython/4d6ddf712a606d555dab287529a109ac71b26e75/ws2812/pico_python_ws2812b-main/pico_ws2812b.jpg -------------------------------------------------------------------------------- /ws2812/pico_python_ws2812b-main/ws2812b.py: -------------------------------------------------------------------------------- 1 | import array, time 2 | from machine import Pin 3 | import rp2 4 | 5 | @rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24) 6 | def ws2812(): 7 | T1 = 2 8 | T2 = 5 9 | T3 = 3 10 | wrap_target() 11 | label("bitloop") 12 | out(x, 1) .side(0) [T3 - 1] 13 | jmp(not_x, "do_zero") .side(1) [T1 - 1] 14 | jmp("bitloop") .side(1) [T2 - 1] 15 | label("do_zero") 16 | nop() .side(0) [T2 - 1] 17 | wrap() 18 | 19 | #delay here is the reset time. You need a pause to reset the LED strip back to the initial LED 20 | #however, if you have quite a bit of processing to do before the next time you update the strip 21 | #you could put in delay=0 (or a lower delay) 22 | class ws2812b: 23 | def __init__(self, num_leds, state_machine, pin, delay=0.001): 24 | self.pixels = array.array("I", [0 for _ in range(num_leds)]) 25 | self.sm = rp2.StateMachine(state_machine, ws2812, freq=8000000, sideset_base=Pin(pin)) 26 | self.sm.active(1) 27 | self.num_leds = num_leds 28 | self.delay = delay 29 | self.brightnessvalue = 255 30 | 31 | # Set the overal value to adjust brightness when updating leds 32 | def brightness(self, brightness = None): 33 | if brightness == None: 34 | return self.brightnessvalue 35 | else: 36 | if (brightness < 1): 37 | brightness = 1 38 | if (brightness > 255): 39 | brightness = 255 40 | self.brightnessvalue = brightness 41 | 42 | # Create a gradient with two RGB colors between "pixel1" and "pixel2" (inclusive) 43 | def set_pixel_line_gradient(self, pixel1, pixel2, left_red, left_green, left_blue, right_red, right_green, right_blue): 44 | if pixel2 - pixel1 == 0: return 45 | 46 | right_pixel = max(pixel1, pixel2) 47 | left_pixel = min(pixel1, pixel2) 48 | 49 | for i in range(right_pixel - left_pixel + 1): 50 | fraction = i / (right_pixel - left_pixel) 51 | red = round((right_red - left_red) * fraction + left_red) 52 | green = round((right_green - left_green) * fraction + left_green) 53 | blue = round((right_blue - left_blue) * fraction + left_blue) 54 | 55 | self.set_pixel(left_pixel + i, red, green, blue) 56 | 57 | # Set an array of pixels starting from "pixel1" to "pixel2" to the desired color. 58 | def set_pixel_line(self, pixel1, pixel2, red, green, blue): 59 | for i in range(pixel1, pixel2+1): 60 | self.set_pixel(i, red, green, blue) 61 | 62 | def set_pixel(self, pixel_num, red, green, blue): 63 | # Adjust color values with brightnesslevel 64 | blue = round(blue * (self.brightness() / 255)) 65 | red = round(red * (self.brightness() / 255)) 66 | green = round(green * (self.brightness() / 255)) 67 | 68 | self.pixels[pixel_num] = blue | red << 8 | green << 16 69 | 70 | # rotate x pixels to the left 71 | def rotate_left(self, num_of_pixels): 72 | if num_of_pixels == None: 73 | num_of_pixels = 1 74 | self.pixels = self.pixels[num_of_pixels:] + self.pixels[:num_of_pixels] 75 | 76 | # rotate x pixels to the right 77 | def rotate_right(self, num_of_pixels): 78 | if num_of_pixels == None: 79 | num_of_pixels = 1 80 | num_of_pixels = -1 * num_of_pixels 81 | self.pixels = self.pixels[num_of_pixels:] + self.pixels[:num_of_pixels] 82 | 83 | def show(self): 84 | for i in range(self.num_leds): 85 | self.sm.put(self.pixels[i],8) 86 | time.sleep(self.delay) 87 | 88 | def fill(self, red, green, blue): 89 | for i in range(self.num_leds): 90 | self.set_pixel(i, red, green, blue) 91 | time.sleep(self.delay) 92 | --------------------------------------------------------------------------------