Implement Tilt Sensor support

This commit is contained in:
Jan-Henrik 2025-02-14 22:06:03 +01:00
parent 7144b6580c
commit 2b1e2626cb
3 changed files with 34 additions and 40 deletions

View file

@ -74,7 +74,7 @@ public:
void refreshArc(); void refreshArc();
void setTiltActive(uint8_t sensor, bool active); void setTiltActive(uint8_t sensor, bool active);
void sendTiltEvent(uint8_t sensor, int16_t x, int16_t y, int16_t z); void sendTiltEvent(uint8_t sensor, int8_t xh,int8_t xl,int8_t yh,int8_t yl,int8_t zh,int8_t zl);
bool active; bool active;

View file

@ -43,20 +43,17 @@ void MonomeSerialDevice::setTiltActive(uint8_t sensor, bool active) {
} }
} }
void MonomeSerialDevice::sendTiltEvent(uint8_t sensor, int16_t x, int16_t y, int16_t z) { void MonomeSerialDevice::sendTiltEvent(uint8_t sensor, int8_t xh,int8_t xl,int8_t yh,int8_t yl,int8_t zh,int8_t zl) {
if (sensor < 4 && tiltActive[sensor]) { if (sensor < 4 && tiltActive[sensor]) {
lastTiltX[sensor] = x;
lastTiltY[sensor] = y;
lastTiltZ[sensor] = z;
Serial.write((uint8_t)0x81); // tiltイベントのプレフィックス Serial.write((uint8_t)0x81); // tiltイベントのプレフィックス
Serial.write(sensor); Serial.write(sensor);
Serial.write((uint8_t)(x >> 8)); Serial.write((int8_t)xh);
Serial.write((uint8_t)(x & 0xFF)); Serial.write((int8_t)xl);
Serial.write((uint8_t)(y >> 8)); Serial.write((int8_t)yh);
Serial.write((uint8_t)(y & 0xFF)); Serial.write((int8_t)yl);
Serial.write((uint8_t)(z >> 8)); Serial.write((int8_t)zh);
Serial.write((uint8_t)(z & 0xFF)); Serial.write((int8_t)zl);
} }
} }
@ -637,15 +634,13 @@ void MonomeSerialDevice::processSerial() {
break; break;
case 0x81: // tilt data request case 0x81: // tilt data request
{ readX = Serial.read();
uint8_t sensor = Serial.read(); setTiltActive(readX, true);
if (sensor < 4 && tiltActive[sensor]) { break;
// recent data send case 0x82: // tilt data request
sendTiltEvent(sensor, lastTiltX[sensor], lastTiltY[sensor], lastTiltZ[sensor]); readX = Serial.read();
} setTiltActive(readX, false);
}
break; break;
// 0x90 variable 64 LED ring // 0x90 variable 64 LED ring
case 0x90: case 0x90:
//pattern: /prefix/ring/set n x a //pattern: /prefix/ring/set n x a

View file

@ -42,6 +42,8 @@ int gridRotation = DEFAULT_ROTATION;
bool flipHorizontal = DEFAULT_FLIP_HORIZONTAL; bool flipHorizontal = DEFAULT_FLIP_HORIZONTAL;
bool flipVertical = DEFAULT_FLIP_VERTICAL; bool flipVertical = DEFAULT_FLIP_VERTICAL;
bool hasTiltSensor = false;
void sendTiltData(); void sendTiltData();
void updateLEDMatrix(); void updateLEDMatrix();
void scanButtonMatrix(); void scanButtonMatrix();
@ -141,23 +143,17 @@ void setup() {
pixels.begin(); pixels.begin();
// Initialize MPU-6050 // Initialize MPU-6050
/*Wire.setSCL(21); Wire.setSCL(21);
Wire.setSDA(20); Wire.setSDA(20);
Wire.begin();*/ Wire.begin();
/*if (!mpu.begin()) { if (mpu.begin()) {
Serial.println("Failed to find MPU6050 chip"); hasTiltSensor = true;
while (1) { Serial.println("MPU6050 Found!");
delay(10);
} mpu.setAccelerometerRange(MPU6050_RANGE_16_G);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
} }
Serial.println("MPU6050 Found!");
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
// Set tilt sensor 0 as active
mdp.setTiltActive(0, true);*/
} }
void loop() { void loop() {
@ -174,9 +170,9 @@ void loop() {
} }
// Send tilt data every 100ms // Send tilt data every 100ms
if (currentMillis - lastTiltCheck >= 100) { if (hasTiltSensor && currentMillis - lastTiltCheck >= 100) {
lastTiltCheck = currentMillis; lastTiltCheck = currentMillis;
//sendTiltData(); sendTiltData();
} }
} }
@ -214,10 +210,13 @@ void sendTiltData() {
sensors_event_t a, g, temp; sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp); mpu.getEvent(&a, &g, &temp);
// Scale gyro data to 16-bit integer range // TODO: Include Rotation here?
int16_t x = (int16_t)(g.gyro.x * 1000); int16_t axis[3];
int16_t y = (int16_t)(g.gyro.y * 1000); axis[0] = (int16_t)(a.acceleration.x * 4) + 128;
int16_t z = (int16_t)(g.gyro.z * 1000); axis[1] = (int16_t)(-a.acceleration.y * 4) + 128;
axis[2] = (int16_t)(a.acceleration.z * 4) + 128;
mdp.sendTiltEvent(0, x, y, z); int8_t *axisbytes = (int8_t *)axis;
mdp.sendTiltEvent(0,axisbytes[0],axisbytes[1],axisbytes[2],axisbytes[3],axisbytes[4],axisbytes[5]);
} }