mirror of
https://github.com/jhbruhn/butns.git
synced 2025-07-02 01:18:50 +00:00
Implement Tilt Sensor support
This commit is contained in:
parent
7144b6580c
commit
2b1e2626cb
3 changed files with 34 additions and 40 deletions
|
@ -74,7 +74,7 @@ public:
|
|||
void refreshArc();
|
||||
|
||||
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;
|
||||
|
|
|
@ -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]) {
|
||||
lastTiltX[sensor] = x;
|
||||
lastTiltY[sensor] = y;
|
||||
lastTiltZ[sensor] = z;
|
||||
|
||||
Serial.write((uint8_t)0x81); // tiltイベントのプレフィックス
|
||||
Serial.write(sensor);
|
||||
Serial.write((uint8_t)(x >> 8));
|
||||
Serial.write((uint8_t)(x & 0xFF));
|
||||
Serial.write((uint8_t)(y >> 8));
|
||||
Serial.write((uint8_t)(y & 0xFF));
|
||||
Serial.write((uint8_t)(z >> 8));
|
||||
Serial.write((uint8_t)(z & 0xFF));
|
||||
Serial.write((int8_t)xh);
|
||||
Serial.write((int8_t)xl);
|
||||
Serial.write((int8_t)yh);
|
||||
Serial.write((int8_t)yl);
|
||||
Serial.write((int8_t)zh);
|
||||
Serial.write((int8_t)zl);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -637,15 +634,13 @@ void MonomeSerialDevice::processSerial() {
|
|||
break;
|
||||
|
||||
case 0x81: // tilt data request
|
||||
{
|
||||
uint8_t sensor = Serial.read();
|
||||
if (sensor < 4 && tiltActive[sensor]) {
|
||||
// recent data send
|
||||
sendTiltEvent(sensor, lastTiltX[sensor], lastTiltY[sensor], lastTiltZ[sensor]);
|
||||
}
|
||||
}
|
||||
readX = Serial.read();
|
||||
setTiltActive(readX, true);
|
||||
break;
|
||||
case 0x82: // tilt data request
|
||||
readX = Serial.read();
|
||||
setTiltActive(readX, false);
|
||||
break;
|
||||
|
||||
// 0x90 variable 64 LED ring
|
||||
case 0x90:
|
||||
//pattern: /prefix/ring/set n x a
|
||||
|
|
|
@ -42,6 +42,8 @@ int gridRotation = DEFAULT_ROTATION;
|
|||
bool flipHorizontal = DEFAULT_FLIP_HORIZONTAL;
|
||||
bool flipVertical = DEFAULT_FLIP_VERTICAL;
|
||||
|
||||
bool hasTiltSensor = false;
|
||||
|
||||
void sendTiltData();
|
||||
void updateLEDMatrix();
|
||||
void scanButtonMatrix();
|
||||
|
@ -141,23 +143,17 @@ void setup() {
|
|||
pixels.begin();
|
||||
|
||||
// Initialize MPU-6050
|
||||
/*Wire.setSCL(21);
|
||||
Wire.setSCL(21);
|
||||
Wire.setSDA(20);
|
||||
Wire.begin();*/
|
||||
Wire.begin();
|
||||
|
||||
/*if (!mpu.begin()) {
|
||||
Serial.println("Failed to find MPU6050 chip");
|
||||
while (1) {
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
if (mpu.begin()) {
|
||||
hasTiltSensor = true;
|
||||
Serial.println("MPU6050 Found!");
|
||||
|
||||
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
|
||||
mpu.setAccelerometerRange(MPU6050_RANGE_16_G);
|
||||
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
|
||||
|
||||
// Set tilt sensor 0 as active
|
||||
mdp.setTiltActive(0, true);*/
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
@ -174,9 +170,9 @@ void loop() {
|
|||
}
|
||||
|
||||
// Send tilt data every 100ms
|
||||
if (currentMillis - lastTiltCheck >= 100) {
|
||||
if (hasTiltSensor && currentMillis - lastTiltCheck >= 100) {
|
||||
lastTiltCheck = currentMillis;
|
||||
//sendTiltData();
|
||||
sendTiltData();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -214,10 +210,13 @@ void sendTiltData() {
|
|||
sensors_event_t a, g, temp;
|
||||
mpu.getEvent(&a, &g, &temp);
|
||||
|
||||
// Scale gyro data to 16-bit integer range
|
||||
int16_t x = (int16_t)(g.gyro.x * 1000);
|
||||
int16_t y = (int16_t)(g.gyro.y * 1000);
|
||||
int16_t z = (int16_t)(g.gyro.z * 1000);
|
||||
// TODO: Include Rotation here?
|
||||
int16_t axis[3];
|
||||
axis[0] = (int16_t)(a.acceleration.x * 4) + 128;
|
||||
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]);
|
||||
}
|
Loading…
Reference in a new issue