Skip to content

Commit

Permalink
Merge pull request #31 from toniebox-reverse-engineering/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
SciLor authored May 15, 2022
2 parents d904137 + 1c6cabd commit f240194
Show file tree
Hide file tree
Showing 19 changed files with 308 additions and 87 deletions.
4 changes: 3 additions & 1 deletion AudioOutputCC3200I2S.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ bool AudioOutputCC3200I2S::ConsumeSample(int16_t sample[2])
ms[1] = sample[1];
MakeSampleStereo16( ms );

if (this->mono) {
if (this->mono && this->channels == 2) {
// Average the two samples and overwrite
int32_t ttl = ms[LEFTCHANNEL] + ms[RIGHTCHANNEL];
ms[LEFTCHANNEL] = ms[RIGHTCHANNEL] = (ttl>>1) & 0xffff;
} else if (this->channels == 1) {
ms[LEFTCHANNEL] = ms[RIGHTCHANNEL];
}

writeBuffer->buffer[writeBuffer->position++] = ms[LEFTCHANNEL];
Expand Down
111 changes: 87 additions & 24 deletions BoxAccelerometer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ void BoxAccelerometer::begin() {
_accel.setupPL();


_accel.writeRegister(CTRL_REG1, 0x02); //Original 0x02
_accel.writeRegister(XYZ_DATA_CFG, 0x02); //Original 0x02
_accel.writeRegister(CTRL_REG2, 0x00); //Original 0x00
_accel.writeRegister(CTRL_REG1, 0x02); //Original 0x02 //F_READ
_accel.writeRegister(XYZ_DATA_CFG, 0x02); //Original 0x02 //FS1
_accel.writeRegister(CTRL_REG2, 0x00); //Original 0x00 //Standby
_accel.writeRegister(F_SETUP, 0x00); //Original 0x00
_accel.writeRegister(TRIG_CFG, 0x08); //Original 0x08
_accel.writeRegister(PULSE_CFG, 0x54); //Original 0x54
_accel.writeRegister(TRIG_CFG, 0x08); //Original 0x08 //Trig_PULSE/ZSPEFE/ELE
_accel.writeRegister(PULSE_CFG, 0x54); //Original 0x54 //YSPEFE
_accel.setupTap(0x1B, 0x3F, 0x3F); //Original 0x1B, 0x3F, 0x3F
_accel.writeRegister(PULSE_TMLT, 0x28); //Original 0x28
_accel.writeRegister(PULSE_LTCY, 0x7F); //Original 0x7F
_accel.writeRegister(HP_FILTER_CUTOFF, 0x10); //Original 0x10
_accel.writeRegister(PULSE_TMLT, 0x28); //Original 0x28 //TMLT3/TMLT5
_accel.writeRegister(PULSE_LTCY, 0x7F); //Original 0x7F //LTCY6/LTCY5/LTCY4/LTCY3/LTCY2/LTCY1/LTCY0
_accel.writeRegister(HP_FILTER_CUTOFF, 0x10); //Original 0x10 //Pulse_LPF_EN

_accel.writeRegister(CTRL_REG3, 0x12); //Original 0x12
_accel.writeRegister(CTRL_REG4, 0x40); //Original 0x40
_accel.writeRegister(CTRL_REG5, 0x40); //Original 0x40
_accel.writeRegister(CTRL_REG1, 0x03); //Original 0x03
_accel.writeRegister(CTRL_REG3, 0x12); //Original 0x12 //WAKE_PULSE/IPOL
_accel.writeRegister(CTRL_REG4, 0x40); //Original 0x40 //INT_EN_FIFO
_accel.writeRegister(CTRL_REG5, 0x40); //Original 0x40 //INT_CFG_FIFO INT1
_accel.writeRegister(CTRL_REG1, 0x03); //Original 0x03 //F_READ/ACTIVE

Log.info("...done");
}
Expand All @@ -51,13 +51,13 @@ void BoxAccelerometer::loop() {

uint8_t tap = _accel.readTap();
if (tap) {
bool AxZ = tap&0b1000000; //event on axis
bool AxY = tap&0b0100000;
bool AxX = tap&0b0010000;
//bool DPE = tap&0b0001000; //double
bool PolZ = tap&0b0000100; //0=positive 1=negative
bool PolY = tap&0b0000010;
bool PolX = tap&0b0000001;
bool AxZ = (tap&0b1000000)>0; //event on axis
bool AxY = (tap&0b0100000)>0;
bool AxX = (tap&0b0010000)>0;
//bool DPE = (tap&0b0001000)>0; //double
bool PolZ = !((tap&0b0000100)>0); //0=positive 1=negative
bool PolY = !((tap&0b0000010)>0);
bool PolX = !((tap&0b0000001)>0);

//X+ = box bottom
//X- = box top
Expand All @@ -80,22 +80,85 @@ void BoxAccelerometer::loop() {
if (AxY && AxZ) {
if (PolY && PolZ) {
tapOn = TapOn::BACK;
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Blue, 2);
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Blue, 3);
} else if (!PolY && !PolZ) {
tapOn = TapOn::FRONT;
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Violet, 2);
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Violet, 3);
} else if (PolY && !PolZ) {
tapOn = TapOn::LEFT;
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Green, 2);
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Green, 3);
} else if (!PolY && PolZ) {
tapOn = TapOn::RIGHT;
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::GreenYellow, 2);
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::GreenYellow, 3);
}
} else if (AxY) {
if (PolY) {
tapOn = TapOn::LEFT_BACK;
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Blue, 3);
} else {
tapOn = TapOn::LEFT_FRONT;
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Violet, 3);
}
} else if (AxZ) {
if (PolZ) {
tapOn = TapOn::RIGHT_BACK;
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Green, 3);
} else {
tapOn = TapOn::RIGHT_FRONT;
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::GreenYellow, 3);
}
}

Log.verbose("Tap recieved %B, direction %X", tap, tapOn);
Log.disableNewline(true);
Log.verbose("Tap recieved %B, direction=%X, ", tap, tapOn);
Log.disableNewline(false);
switch (tapOn)
{
case TapOn::LEFT:
Log.printfln("LEFT");
break;

case TapOn::RIGHT:
Log.printfln("RIGHT");
break;

case TapOn::FRONT:
Log.printfln("FRONT");
break;

case TapOn::BACK:
Log.printfln("BACK");
break;

case TapOn::TOP:
Log.printfln("TOP");
break;

case TapOn::BOTTOM:
Log.printfln("BOTTOM");
break;

case TapOn::LEFT_FRONT:
Log.printfln("LEFT_FRONT");
break;

case TapOn::RIGHT_FRONT:
Log.printfln("RIGHT_FRONT");
break;

case TapOn::LEFT_BACK:
Log.printfln("LEFT_BACK");
break;

case TapOn::RIGHT_BACK:
Log.printfln("RIGHT_BACK");
break;

default:
break;
Log.printfln("OTHER");
}

}

}
Expand Down
19 changes: 12 additions & 7 deletions BoxAccelerometer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ class BoxAccelerometer : public EnhancedThread {
EARS_DOWN2 = 0x4
};
enum class TapOn {
NONE = 0x0,
LEFT = 0x1,
RIGHT = 0x2, //17,34,68 - 34
FRONT = 0x3, //16, 17
BACK = 0x4,
TOP = 0x5, //16,32,64,68,24 - 16
BOTTOM = 0x6 //17
NONE = 0x00,
LEFT = 0x01,
RIGHT = 0x02, //17,34,68 - 34
FRONT = 0x04, //16, 17
BACK = 0x08,
TOP = 0x10, //16,32,64,68,24 - 16
BOTTOM = 0x20, //17

LEFT_FRONT = LEFT + FRONT,
RIGHT_FRONT = RIGHT + FRONT,
LEFT_BACK = LEFT + BACK,
RIGHT_BACK = RIGHT + BACK
};

void
Expand Down
20 changes: 10 additions & 10 deletions BoxBattery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ void BoxBattery::begin() {

_wasLow = false;
_wasCritical = false;
_batteryAdcRaw = analogRead(60);
_batteryAdcLowRaw = 9999;
_readBatteryAdc();

batteryTestThread.setInterval(10*60*1000);
batteryTestThread.enabled = false;

loop();
logBatteryStatus();

setInterval(500);
setInterval(100);
}
void BoxBattery::loop() {
_batteryAdcRaw = analogRead(60);
_readBatteryAdc();
_charger.read();

if (_batteryAdcRaw < _batteryAdcLowRaw || isChargerConnected())
Expand All @@ -46,6 +45,11 @@ void BoxBattery::loop() {
}
}

void BoxBattery::_readBatteryAdc() {
uint16_t adcValue = analogReadAvg(BATTERY_VOLTAGE_PIN, 1);
_batteryAdcRaw = adcValue;
}

bool BoxBattery::isChargerConnected() {
if (_charger.isPressed())
return true;
Expand All @@ -55,10 +59,7 @@ uint16_t BoxBattery::getBatteryAdcRaw() {
return _batteryAdcRaw;
}
uint16_t BoxBattery::getBatteryVoltage() {
if (isChargerConnected()) {
return 10000 * getBatteryAdcRaw() / _batteryVoltageChargerFactor;
}
return 10000 * getBatteryAdcRaw() / _batteryVoltageFactor;
return 1000 * getBatteryAdcRaw() / _batteryVoltageFactor;
}
bool BoxBattery::isBatteryLow() {
if (getBatteryAdcRaw() < _batteryLowAdc)
Expand Down Expand Up @@ -88,7 +89,6 @@ void BoxBattery::reloadConfig() {
ConfigStruct* config = Config.get();

_batteryVoltageFactor = config->battery.voltageFactor;
_batteryVoltageChargerFactor = config->battery.voltageChargerFactor;
_batteryLowAdc = config->battery.lowAdc;
_batteryCriticalAdc = config->battery.criticalAdc;
}
Expand Down Expand Up @@ -144,7 +144,7 @@ void BoxBattery::startBatteryTest() {
file.writeString("Comments");
file.writeString("\r\n");
file.writeString("0;;;;;;");
sprintf(output, "vFactor=%lu, vChargerFactor=%lu;v2-wav", _batteryVoltageFactor, _batteryVoltageChargerFactor);
sprintf(output, "vFactor=%lu;v3-wav", _batteryVoltageFactor);
file.writeString(output);
file.writeString("\r\n");
file.close();
Expand Down
4 changes: 3 additions & 1 deletion BoxBattery.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class BoxBattery : public EnhancedThread {

private:
uint32_t _batteryVoltageFactor;
uint32_t _batteryVoltageChargerFactor;
uint16_t _batteryLowAdc;
uint16_t _batteryCriticalAdc;

Expand All @@ -64,6 +63,9 @@ class BoxBattery : public EnhancedThread {

const char* _batteryTestFilename = "/revvox/batteryTest.csv";
uint64_t _batteryTestStartMillis;

const static uint8_t BATTERY_VOLTAGE_PIN = 60;
void _readBatteryAdc();
};

#endif
24 changes: 18 additions & 6 deletions BoxConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ String BoxConfig::getAsJson() {
JsonObject batteryDoc = doc.createNestedObject("battery");
ConfigBattery* batteryCfg = &_config.battery;
batteryDoc["voltageFactor"] = batteryCfg->voltageFactor;
batteryDoc["voltageChargerFactor"] = batteryCfg->voltageChargerFactor;
batteryDoc["lowAdc"] = batteryCfg->lowAdc;
batteryDoc["criticalAdc"] = batteryCfg->criticalAdc;
batteryDoc["sleepMinutes"] = batteryCfg->sleepMinutes;
Expand All @@ -76,6 +75,7 @@ String BoxConfig::getAsJson() {
ConfigMisc* miscCfg = &_config.misc;
miscDoc["autodump"] = miscCfg->autodump;
miscDoc["swd"] = miscCfg->swd;
miscDoc["watchdogSeconds"] = miscCfg->watchdogSeconds;

_json = "";
serializeJson(doc, _json);
Expand All @@ -95,7 +95,6 @@ bool BoxConfig::setFromJson(String json) {
JsonObject batteryDoc = doc["battery"];
ConfigBattery* batteryCfg = &_config.battery;
batteryCfg->voltageFactor = batteryDoc["voltageFactor"].as<uint32_t>();
batteryCfg->voltageChargerFactor = batteryDoc["voltageChargerFactor"].as<uint32_t>();
batteryCfg->lowAdc = batteryDoc["lowAdc"].as<uint16_t>();
batteryCfg->criticalAdc = batteryDoc["criticalAdc"].as<uint16_t>();
batteryCfg->sleepMinutes = batteryDoc["sleepMinutes"].as<uint8_t>();
Expand All @@ -118,6 +117,7 @@ bool BoxConfig::setFromJson(String json) {
ConfigMisc* miscCfg = &_config.misc;
miscCfg->autodump = miscDoc["autodump"].as<bool>();
miscCfg->swd = miscDoc["swd"].as<bool>();
miscCfg->watchdogSeconds = miscDoc["watchdogSeconds"].as<uint8_t>();

// Convert old config version to latest one.
if (_config.version != CONFIG_ACTIVE_VERSION) {
Expand All @@ -132,6 +132,16 @@ bool BoxConfig::setFromJson(String json) {
case 4:
miscCfg->swd = false;
_config.version = 5;
case 5:
batteryCfg->lowAdc = 9658;
batteryCfg->criticalAdc = 8869;
batteryCfg->voltageFactor = 27850;
_config.version = 6;
case 6:
miscCfg->watchdogSeconds = 10;
if (batteryCfg->voltageFactor == 67690) //Fix for wrong value in previous CFW
batteryCfg->voltageFactor = 27850;
_config.version = 7;
write();
break;
default:
Expand All @@ -147,11 +157,12 @@ bool BoxConfig::setFromJson(String json) {
void BoxConfig::_initializeConfig() {
_config.version = CONFIG_ACTIVE_VERSION;

//(4936,0258+0x59)/(10000/0x663d) = adc
//(10000/0x663d)×13152−0x59 = v-OFW
ConfigBattery* battery = &_config.battery;
battery->voltageFactor = 67690;
battery->voltageChargerFactor = 71907;
battery->lowAdc = 2500;
battery->criticalAdc = 2400;
battery->voltageFactor = 27850;
battery->lowAdc = 9658; //OFW 0xE11 (9657,837)
battery->criticalAdc = 8869; //OFW 0xCE3/0xCE4 (8867,4124/8870,0297)
battery->sleepMinutes = 15;

ConfigButtonEars* buttons = &_config.buttonEars;
Expand All @@ -173,4 +184,5 @@ void BoxConfig::_initializeConfig() {
ConfigMisc* misc = &_config.misc;
misc->autodump = false;
misc->swd = false;
misc->watchdogSeconds = 10;
}
4 changes: 2 additions & 2 deletions BoxConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "BoxSD.h"


#define BOXCONFIG_JSON_SIZE 528
//{"version":255,"battery":{"voltageFactor":4294967295,"voltageChargerFactor":4294967295,"lowAdc":65535,"criticalAdc":65535,"sleepMinutes":255},"buttonEars":{"longPressMs":65535,"veryLongPressMs":65535},"wifi":{"ssid":"12345678901234567890123456789012","password":"1234567890123456789012345678901234567890123456789012345678901234"},"log":{"sdLog":false},"misc":{"autodump":false,"swd":false}}
#define BOXCONFIG_JSON_SIZE 560
//{"version":255,"battery":{"voltageFactor":4294967295,"voltageChargerFactor":4294967295,"lowAdc":65535,"criticalAdc":65535,"sleepMinutes":255},"buttonEars":{"longPressMs":65535,"veryLongPressMs":65535},"wifi":{"ssid":"12345678901234567890123456789012","password":"1234567890123456789012345678901234567890123456789012345678901234"},"log":{"sdLog":false},"misc":{"autodump":false,"swd":false,"watchdogSeconds":255}}
//Size from https://arduinojson.org/v6/assistant/

class BoxConfig {
Expand Down
Loading

0 comments on commit f240194

Please sign in to comment.