基于加速计的M5StickC图片旋转显示
简单演示加速计控制图片旋转角度-45/-30/-15/0/15/30/45 ,一共7个旋转角度,提前处理7张(像素80x80)图片存入数组,根据加速计当前状态进行显示。#include <M5StickC.h>
#include "img.c"//提前处理的图片文件
float accX = 0.0F;
float accY = 0.0F;
float accZ = 0.0F;
int xpos = 40;
void setup() {
// put your setup code here, to run once:
M5.begin();
M5.IMU.Init();//
M5.Axp.ScreenBreath(9);
M5.Lcd.setRotation(3);
M5.Lcd.fillScreen(0x0000);
M5.Lcd.drawBitmap(xpos, 0, 80, 80, img);
}
void loop() {
// put your main code here, to run repeatedly:
M5.IMU.getAccelData(&accX,&accY,&accZ);
accY = accY-0.0;
if(accY<-0.2||accY>0.2){
int moveSpeed = -int(floor(accY*10/2));
if(accY>0.1){
xpos = max(0,xpos+moveSpeed);
M5.Lcd.fillRect(xpos+80,0,-moveSpeed,80,0x0000);
}
else if(accY<-0.1){
xpos = min(79,xpos+moveSpeed);
M5.Lcd.fillRect(xpos-moveSpeed,0,moveSpeed,80,0x0000);
}
if(xpos<7){
M5.Lcd.drawBitmap(xpos, 0, 80, 80, img);
}
else if(xpos<20){
M5.Lcd.drawBitmap(xpos, 0, 80, 80, img);
}
else if(xpos<33){
M5.Lcd.drawBitmap(xpos, 0, 80, 80, img);
}
else if(xpos<47){
M5.Lcd.drawBitmap(xpos, 0, 80, 80, img);
}
else if(xpos<60){
M5.Lcd.drawBitmap(xpos, 0, 80, 80, img);
}
else if(xpos<73){
M5.Lcd.drawBitmap(xpos, 0, 80, 80, img);
}
else{
M5.Lcd.drawBitmap(xpos, 0, 80, 80, img);
}
}
}
img.C可从网上找图片转数组工具进行处理组成数组或运行以下py脚本进行转换
from PIL import Image
import numpy as np
import glob
#print('フォルダ名')
#dirname = input('>> ')
dirname = 'rotateImage'
files = glob.glob(dirname+'\*')
img0 = Image.open(files)
width0, height0 = img0.size
path_w = 'img.c'
with open(path_w, mode='w') as f:
for file in files:
print(file)
f.write('//'+file+'\n')
f.write('const unsigned short img[{}][{}] = '.format(len(files),width0*height0))
f.write('{\n')
for i,file in enumerate(files):
print(file)
img = Image.open(file)
f.write('\t{\n')
for y in range(height0):
f.write('\t')
for x in range(width0):
img_pixels = img.getpixel((x,y))
R = img_pixels>>3
G = img_pixels>>2
B = img_pixels>>3
img_data = int(R*2048+G*32+B)
f.write(format(img_data,'#06x'))
if x == width0-1:
if y == height0-1:
f.write('\n\t}')
else:
f.write(',\n')
else:
f.write(',')
if i < len(files)-1:
f.write(',')
f.write('};')
参考img文件
这是非常使用的技巧,已收藏,谢谢分享
页:
[1]