Games usermod: MPU-6050 gyro/accelero working

This commit is contained in:
Ewowi
2022-09-21 18:09:13 +02:00
parent f0fdfc73b9
commit c94228ae3c

View File

@@ -84,7 +84,7 @@ typedef struct PongBall {
} pongBall;
//effect function
//effect functions
uint16_t mode_pongGame(void) {
uint16_t dataSize = 3 * sizeof(pongBall);
@@ -180,6 +180,28 @@ uint16_t mode_pongGame(void) {
static const char _data_FX_MODE_PONGGAME[] PROGMEM = "🎮 Pong@!;!;!;2d";
const uint16_t MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.
int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data
int16_t temperature; // variables for temperature data
uint16_t mode_gyro(void) {
SEGMENT.fill(BLACK);
uint8_t y = 0;
SEGMENT.setPixelColorXY(SEGMENT.virtualWidth() * (accelerometer_x+INT16_MAX)/(2*INT16_MAX), y+=2, BLUE);
SEGMENT.setPixelColorXY(SEGMENT.virtualWidth() * (accelerometer_y+INT16_MAX)/(2*INT16_MAX), y+=2, BLUE);
SEGMENT.setPixelColorXY(SEGMENT.virtualWidth() * (accelerometer_z+INT16_MAX)/(2*INT16_MAX), y+=2, BLUE);
SEGMENT.setPixelColorXY(SEGMENT.virtualWidth() * (gyro_x+INT16_MAX)/(2*INT16_MAX), y+=2, BLUE);
SEGMENT.setPixelColorXY(SEGMENT.virtualWidth() * (gyro_y+INT16_MAX)/(2*INT16_MAX), y+=2, BLUE);
SEGMENT.setPixelColorXY(SEGMENT.virtualWidth() * (gyro_z+INT16_MAX)/(2*INT16_MAX), y+=2, BLUE);
return FRAMETIME;
}
static const char _data_FX_MODE_GYRO[] PROGMEM = "🎮 Gyro@!;!;!;2d";
//class name. Use something descriptive and leave the ": public Usermod" part :)
class GamesUsermod : public Usermod {
@@ -194,7 +216,14 @@ class GamesUsermod : public Usermod {
* You can use it to initialize variables, sensors or similar.
*/
void setup() {
strip.addEffect(255, &mode_pongGame, _data_FX_MODE_PONGGAME);
Wire.begin();
Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
strip.addEffect(255, &mode_pongGame, _data_FX_MODE_PONGGAME);
strip.addEffect(255, &mode_gyro, _data_FX_MODE_GYRO);
}
/*
@@ -206,6 +235,19 @@ class GamesUsermod : public Usermod {
}
void loop() {
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
Wire.requestFrom(MPU_ADDR, (uint8_t)(7*2), true); // request a total of 7*2=14 registers
// "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable
accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)
}
void addToJsonState(JsonObject& root)
@@ -213,7 +255,6 @@ class GamesUsermod : public Usermod {
//root["user0"] = userVar0;
}
void readFromJsonState(JsonObject& root)
{
userVar0 = root["user0"] | userVar0; //if "user0" key exists in JSON, update, else keep old value
@@ -221,7 +262,7 @@ class GamesUsermod : public Usermod {
void addToConfig(JsonObject& root)
{
JsonObject top = root.createNestedObject("gamesUsermod");
// JsonObject top = root.createNestedObject("gamesUsermod");
}
bool readFromConfig(JsonObject& root)