Last active
April 2, 2022 05:09
-
-
Save ksasao/f0bf90e6d931e7a0a967223c768c1eea to your computer and use it in GitHub Desktop.
M5StickV で UART 文字列受信サンプル。uart.any() で受信バッファの状態を確認して readline() で読むのが簡単。https://twitter.com/ksasao/status/1239058196334923776
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lcd | |
import image | |
import time | |
import uos | |
lcd.init() | |
lcd.rotation(2) #Rotate the lcd 180deg | |
from Maix import I2S, GPIO | |
from Maix import GPIO | |
from fpioa_manager import * | |
from machine import UART | |
# serial port setup | |
# grove connector | |
fm.register(35, fm.fpioa.UART2_TX, force=True) | |
fm.register(34, fm.fpioa.UART2_RX, force=True) | |
uart = UART(UART.UART2, 115200,8,0,0, timeout=1000, read_buf_len= 4096) | |
# initialize camera | |
import sensor | |
while 1: | |
try: | |
sensor.reset() #Reset sensor may failed, let's try some times | |
break | |
except: | |
err_counter = err_counter + 1 | |
if err_counter == 20: | |
lcd.draw_string(lcd.width()//2-100,lcd.height()//2-4, "Error: Sensor Init Failed", lcd.WHITE, lcd.RED) | |
time.sleep(0.1) | |
continue | |
sensor.set_pixformat(sensor.RGB565) | |
sensor.set_framesize(sensor.QVGA) #QVGA=320x240 | |
sensor.run(1) | |
# main | |
text = "" | |
try: | |
while(True): | |
if uart.any(): | |
data = uart.readline() | |
text = data.decode('utf-8') | |
print(text) | |
img = sensor.snapshot() # Take an image from sensor | |
img.draw_string(40, 50, text, scale=3) | |
lcd.display(img) | |
except KeyboardInterrupt: | |
a = kpu.deinit(task) | |
sys.exit() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <M5StickC.h> | |
const int MAX_RX_BUFFER_SIZE = 1024; | |
char rx_buffer[MAX_RX_BUFFER_SIZE]; | |
void setup() { | |
M5.begin(); | |
M5.Lcd.setRotation(1); | |
Serial2.begin(115200, SERIAL_8N1, 32, 33); | |
} | |
void loop() { | |
M5.update(); | |
if ( M5.BtnA.wasPressed() ) { | |
println_ext("Button A"); | |
} | |
if ( M5.BtnB.wasPressed() ) { | |
println_ext("Button B"); | |
} | |
int axpButton = M5.Axp.GetBtnPress(); | |
if ( axpButton == 1 ) { | |
println_ext("Axp Button (long)"); | |
} | |
if ( axpButton == 2 ) { | |
println_ext("Axp Button (short)"); | |
} | |
} | |
void println_ext(char* str){ | |
Serial2.write(str); | |
M5.Lcd.fillScreen(TFT_BLACK); | |
M5.Lcd.setCursor(0, 5, 4); | |
M5.Lcd.println(str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment