|
上周五,M5Stack又上架了一款M5StickC的专属配件,Joystick HAT,每周五新款上架已经成为了一个惯例,本周就一起来看一下最新的这个配件。
从外观上看真的很迷你很小巧,光秃秃的摇杆,没有任何多余的修饰,可以轻松的放入口袋中,为了更加方便进行数据读取,依然采用了I2C协议进行通信,自带STM32F030系列芯片,所有采集的数据处理都在内部进行处理,减轻了StickC的压力。个人体验与之前有键帽的摇杆最主要的区别是手感,自动回位很迅速,不会存在死区导致摇杆被卡住。为了减小封装的尺寸,摇杆采用了对角线的安装形式,因此如果你读取的数据是转换后的值-127~127, 当然你也可以读取原始值来应用到你的程序里。
为了演示摇杆的灵敏度写了一个小程序来进行测试,参考的是涂鸦弹跳,这个游戏原来是用重力感应来控制,现在被改为摇杆控制
[mw_shl_code=arduino,true]#include <M5StickC.h>
#include "Wire.h"
#define JOY_ADDR 0x38
// initial position of the cursor
int xPos = 40;
int yPos = 0;
int prevxPos = 40;
int prevyPos = 0;
char data[100];
int pressstate=0;
float upvelocity=0;
int platwidth=20;
// We store platform x and ys here (so there are 5 platforms here)
int plats[14]={0,0,25,0,50,0,75,0,100,0,125,0,150,0};
bool dead = true;
int deadstate=0;
int speedy=0;
int step=0;
int8_t x_data,y_data,button_data;
void setup() {
// initialize the display
M5.begin();
Wire.begin(0, 26, 100000);
// clear the background
M5.Lcd.setRotation(2);
M5.Lcd.fillRect(0, 0, 80, 160, BLACK);
for(int i=0; i<14; i+=2)
{
plats[i+1]=(random(159)) % 159;
}
}
void loop() {
Wire.beginTransmission(JOY_ADDR);
Wire.write(0x02);
Wire.endTransmission();
Wire.requestFrom(JOY_ADDR, 3);
if (Wire.available()) {
x_data = Wire.read();
y_data = Wire.read();
button_data = Wire.read();
}
if(!dead)
{
for(int i=0; i<14; i+=2)
{
// EsploraTFT.stroke(0, 255, 0);
M5.Lcd.drawLine(plats[i+1],plats,plats[i+1]+platwidth,plats,GREEN);
// EsploraTFT.stroke(150, 89, 43);
M5.Lcd.drawLine(plats[i+1],plats-1,plats[i+1]+platwidth,plats-1, BLACK);
plats=plats+1;
if(plats > 160)
{
//sets the platform to the top of screen
plats=0;
//sets random x position
plats[i+1]= (random(59)) % 59;
}
}
step++;
int tilt = x_data;
// the values are 100 when tilted to the left
// and -100 when tilted to the right
// map these values to the start and end points
speedy = map(tilt, -110, 110, 3, -3);
xPos+=speedy;
// don't let the point go past the screen edges
if (xPos > 59) {
(xPos = 0);
}-
if (xPos < 0) {
(xPos = 59);
}
if (yPos > 160) {
(dead=true);
}
if (yPos < 0) {
(yPos = 0);
}
// Erase previous position of player
// EsploraTFT.stroke(150, 89, 43);
M5.Lcd.drawPixel(prevxPos, prevyPos, BLACK);
M5.Lcd.drawPixel(prevxPos+1, prevyPos, BLACK);
M5.Lcd.drawPixel(prevxPos+1, prevyPos+1, BLACK);
M5.Lcd.drawPixel(prevxPos, prevyPos+1, BLACK);
M5.Lcd.drawPixel(prevxPos+1, prevyPos+2, BLACK);
M5.Lcd.drawPixel(prevxPos, prevyPos+2, BLACK);
// Create new position of player
// EsploraTFT.stroke(0, 0, 255);
M5.Lcd.drawPixel(xPos, yPos, 0xc48c40);
M5.Lcd.drawPixel(xPos+1, yPos, 0xc48c40);
M5.Lcd.drawPixel(xPos+1, yPos+1, 0xc48c40);
M5.Lcd.drawPixel(xPos, yPos+1, 0xc48c40);
M5.Lcd.drawPixel(xPos+1, yPos+2, 0xc48c40);
M5.Lcd.drawPixel(xPos, yPos+2, 0xc48c40);
prevxPos=xPos;
prevyPos=yPos;
upvelocity-=0.3f;
yPos-=upvelocity;
// Check for collisions with platforms
for(int i=0; i<20; i+=2)
{
if(yPos >= plats-3 && prevyPos < plats && xPos < plats[i+1]+platwidth && xPos > plats[i+1])
{
yPos=plats-3;
upvelocity=4.5f;
}
}
}
// If dead
else{
deadstate++;
if(deadstate==1) {
M5.Lcd.fillRect(0, 0, 80, 160, BLACK);
xPos = 40 / 2;
yPos = 0;
M5.Lcd.setCursor(10, 60);
M5.Lcd.printf("Score: %d", step);
M5.Lcd.println();
M5.Lcd.print("Press Joystick to start");
}
// Press button to restart
if(button_data == 0){
M5.Lcd.fillRect(0, 0, 80, 160, BLACK);
pressstate=0;
upvelocity=0;
step=0;
dead=false;
deadstate=0;
}
}
delay(33);
}[/mw_shl_code]
|
|