★ wanayoo — archive 1999 https://github.com/m5stack/UIFlow-Code/wiki/AdvancedNouvelle recherche | Portail wanayoo
Skip to content

Advanced

vany5921 edited this page Aug 27, 2019 · 5 revisions

Advanced

Easy IO

from easyIO import *
# pin: only >= 32, esp32 GPIO number
analogRead(pin)
# write 38KHz Pwm
# pin: esp32 GPIO number
# duty: 0.0 ~ 100.0
analogWrite(pin, duty)
# pin mode: INOUT, Pull up
# pin: esp32 GPIO number
digitalWrite(pin, value)
# pin mode: INOUT, Pull up
# pin: esp32 GPIO number
# return: 0 or 1
digitalRead(pin)
# pin: esp32 GPIO number, toggle pin digital value
toggleIO(pin)
# Map value from a range to another Scale up or down value
map_value(value, from_low, from_high, from_low, from_high)

Pin

import machine
# pin: esp32 GPIO number
# mode: machine.Pin.(IN, OUT, OUT_OD, INOUT, OPEN_DRAIN)
# pull: machine.Pin.(PULL_UP, PULL_DOWN, PULL_HOLD, PULL_FLOAT)
# value: 0, 1
pin0 = machine.Pin(pin [, mode, pull, value])
# set pin high level
pin0.on()
# set pin low level
pin0.off()
# get pin state
pin0.value()
# state: 0, 1
# set pin level to state
pin0.value(state)

Pwm

import machine
# pin: esp32 GPIO number
# freq: 1 - 40000000Hz
# duty: 0 ~ 100.0 (%)
# timer: 0, 3(servo unit use 3), (lcd, ir unit use 1) (speak use 2)
pwm0 = machine.PWM(pin, freq, duty, timer)
# freq: 1 ~ 40000000Hz
pwm0.freq(freq)
# duty: 0 ~ 100.0 (%)
pwm0.duty(duty)
# pause pwm out
pwm0.pause()
# resume pwm out
pwm0.resume()

I2C

import i2c_bus
# port i2c_bus.PORTA, i2c_bus.PORTB or (sda, scl)
i2c1 = i2c_bus.easyI2C(port, addr)
i2c1.write_u8(reg, data)
# byteorder: 'big' or 'little'
i2c1.write_u16(reg, data, byteorder='big')
#
data = i2c1.read_u8(reg):
# byteorder: 'big' or 'little'
data = i2c1.read_u16(reg, byteorder='big')
# data is bytes
data = i2c1.read(num)
# state: True or False
state = i2c1.available()
# addrList is a available addr list
addrList = i2c1.scan()

ADC

import machine
# pin number must > 36  
adc=machine.ADC(pin)
# value: ATTN_0DB (range 0 ~ 1.1V)
#        ATTN_2_5DB (range 0 ~ 1.5V)
#        ATTN_6DB (range 0 ~ 2.5V)
#        ATTN_11DB (range 0 ~ 3.9V)
adc.atten(value)
# value: WIDTH_9BIT - capture width is 9Bit
#        WIDTH_10BIT - capture width is 10Bit
#        WIDTH_11BIT - capture width is 11Bit
#        WIDTH_12BIT - capture width is 12Bit
adc.width(value)
# Read the ADC value as voltage (in mV)
data = adc.read()

DAC

import machine
dac = machine.DAC(25)
# set the  dac value: 0 ~ 255
dac.write(value)
# freq: 16-32000 Hz
# duration: the duration of the beep in ms
# scale: range: 0-3; scale the output voltage by 2^scale
dac.beep(freq, duration [, scale=0])
# freq: 16-32000 Hz for sine wave
#       500-32000 Hz for noise
#       170 - 3600 Hz for triangle
#       170 - 7200 Hz for ramp and sawtooth
# type: SINE, TRIANGLE, RAMP, SAWTOOTH and NOISE
# scale: 0~ 3, scale the output voltage by 2^scale
# offset: optional, only valid for sine wave; range: 0-255; ofset the output voltage by offset value
# invert: optional, only valid for sine wave; range: 0-3; invert the half-cycle of the sine wave
dac.waveform(freq, type [,duration=0] [,scale=0] [,offset=0] [,invert=2])
# Stops the background process started by dac.waveform()
dac.stopwave()
# Changes the frequency of the background waveform process started by dac.waveform() function.
dac.freq(freq)

MQTT

Mqtt lib support auto reconnect Wi-Fi, MQTT server and restart subscribe

Mqtt lib will auto ping broker every 10000ms

Mqtt lib will auto connect Wi-Fi

from m5mqtt import M5mqtt
m5mqtt = M5mqtt(id, server, port, user, password, keepalive)

def callback(topic_data):
  # global params
  pass
m5mqtt.subscribe(topic, callback)

# Must be called after subscription
# after start can`t subscribe again
m5mqtt.start()

m5mqtt.publish(topic, msg)

ESP-NOW

import espnow
# init espnow
# mac ff:ff:ff:ff:ff:ff add peer in channel 1, IF_WIFI_STA
espnow.init()

# deinit espnow
espnow.deinit()

# return mac addr,type: bytes
espnow.get_mac_addr()

# peer: must be bytes, len: 6, slave mac addr
# channel: 1 ~ 13, default 1
# ifidx: espnow.IF_WIFI_STA or espnow.IF_WIFI_AP or espnow.IF_MAX, default: espnow.IF_WIFI_STA
# encrypt: True or False, default: False
# lmk: len must be 16, type string or bytes, if encrypt is False, ignore, default: None
# id: save peer, 0 ~ 9, default: -1
espnow.add_peer(peer, [channel, ifidx, encrypt, lmk, id])

# pmk: type should be bytes or str, len must 16
espnow.set_pmk(pmk)

# recv msg will callback
# if msg is broadcast, broadcast = True
def recv_cb(broadcast):
  print(broadcast)
  mac_addr, cmd_id, data = espnow.recv_data()
  
espnow.recv_cb(recv_cb)

# turn tuple (mac_addr, cmd id, data)
# if no data: mac_addr = b'', cmd_id = 0, msg = b''
mac_addr, cmd_id, msg = espnow.recv_data()

def send_cb(send_state):
  print(send_state)

espnow.send_cb(send_cb)

# cmd id: 0~65535, default 0
# data: bytes or str, default: None
espnow.broadcast([cmd_id, data])

# id: 0 ~ 9, (by espnow.add_peer set)
# mac_addr: bytes, len 6 (if mac_addr was give, ignore id)
# cmd_id: 0~65535
# data: bytes or string
espnow.send([id, mac_addr, cmd_id, data])

UART

import machine
# Set uart port and tx rx
uart = machine.UART(channel, tx_pin, rx_pin)

# Set part baud and data bits, Parity bits, stop bits
uart.init(baud, bits, parity, stop)

# Read all data from uart
uart.read()

# Read 10 characters from uart
uart.read(10)

# Read a line data from uart
uart.readline()

# Read the rest of the cache
uart.any()

# Write characters  to uart 
uart.write(ch)

WiFi

import wifiCfg
# Display wifi option "reconnect" and "set wifi"  on bottom
wifiCfg.screenShow()

# auto connect wifi
wifiCfg.autoConnect(lcdShow=True)

# Return true when wifi is connected
wifiCfg.wlan_sta.isconnected()

# Connect with ssid and pswd
wifiCfg.doConnect(ssid, pswd)

P2P

# Initialize P2P settings
remoteInit()

# Connect remotely APIkey and send data
sendP2PData(APIkey, data)

# Return the data of remotely host
getP2PData()
Clone this wiki locally
You can’t perform that action at this time.