|
M5Stick V(UnitV) + Adafruit Thermal Printer + M5Stack Gray = Instant Camera Printer
1.硬件
Adafruit热敏打印机
M5StickV(或者UnitV)
M5Stack 主机
Grove连接线
2.连接
将M5StickV或UnitV的Grove接口与M5Stack Core进行连接
将Adafruit热敏打印机的串口与M5StackCore的Pin2和Pin5连接,5V与GND接到相应引脚
对于M5StickV来说需要加入两行代码:
sensor.set_vflip(1)
sensor.set_hmirror(1)
按下A键进行拍照打印
3.代码
M5Stack端
[mw_shl_code=arduino,true]
#include <M5Stack.h>
#include "Adafruit_Thermal.h"
Adafruit_Thermal printer(&Serial2); // Pass addr to printer constructor
void setup()
{
M5.begin(true, true, true);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0);
M5.Lcd.println("GO");
// Connect M5Stack
// vert 2 jaune 5 Noir gnd
Serial2.begin(19200, SERIAL_8N1, 2, 5);
printer.begin();
Serial1.begin(115200,SERIAL_8N1 ,21,22);
printer.println(F("Start"));
}
void loop()
{
M5.update();
if (M5.BtnA.wasReleased())
{
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0);
M5.Lcd.print('A');
Serial1.print('/');
}
if(Serial1.available()> 0)
{
String str = Serial1.readString();
M5.Lcd.println(str);
uint8_t dataArray[400];
char string[400*4+1];
str.toCharArray(string, 400*4+1);
Serial.print("ch:");
Serial.println(string);
Serial.println("****************");
char* ptr = strtok(string, ",");
int i = 0;
while(ptr != NULL) {
dataArray = atol(ptr);
i= i +1;
// create next part
ptr = strtok(NULL, ",");
}
Serial.print("fin");
Serial.println(i);
printer.printBitmap(320, 10, dataArray);
}
}[/mw_shl_code]
M5StickV/UnitV端
[mw_shl_code=python,true]
import sensor
import image
import lcd
import time
import sys
import utime
from machine import I2C,UART
from fpioa_manager import fm
#uart initial
fm.register(35, fm.fpioa.UART2_TX, force=True)
fm.register(34, fm.fpioa.UART2_RX, force=True)
uart_Port = UART(UART.UART2, 115200,8,0,0, timeout=1000, read_buf_len= 4096)
clock = time.clock()
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.set_vflip(1)
sensor.set_hmirror(1)
sensor.run(1)
sensor.skip_frames(20)
KO = 1
while True:
clock.tick()
img = sensor.snapshot()
data = str(uart_Port.readline())
colone = 0
info = [0,0,0,0,0,0,0,0]
if data[2] == "/" :
i = -1
lg = 0
img2 = sensor.snapshot()
print(img2.width())
print(img2.height())
for y in range(0, 240):
for x in range(0, 320):
i = i + 1
col = img2.get_pixel(x,y)
if col > 127 :
info= 0
else:
info= 1
if i == 7 :
result = info[0] * 128 + info[1] * 64 + info[2] * 32 + info[3] * 16 + info[4] * 8+ info[5] * 4 + info[6] * 2 + info[7] * 1
uart_Port.write("{}".format(result))
if (x != 320 and y != 10) :
uart_Port.write(",")
i = -1
info = [0,0,0,0,0,0,0,0]
colone = colone +1
lg = lg + 1
if lg == 10 :
lg = 0
utime.sleep(2)
utime.sleep(10)
lcd.display(img)[/mw_shl_code]
|
|