Skip to content

Commit f240194

Browse files
authored
Merge pull request #31 from toniebox-reverse-engineering/develop
Develop
2 parents d904137 + 1c6cabd commit f240194

19 files changed

+308
-87
lines changed

AudioOutputCC3200I2S.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,12 @@ bool AudioOutputCC3200I2S::ConsumeSample(int16_t sample[2])
9393
ms[1] = sample[1];
9494
MakeSampleStereo16( ms );
9595

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

102104
writeBuffer->buffer[writeBuffer->position++] = ms[LEFTCHANNEL];

BoxAccelerometer.cpp

+87-24
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ void BoxAccelerometer::begin() {
1414
_accel.setupPL();
1515

1616

17-
_accel.writeRegister(CTRL_REG1, 0x02); //Original 0x02
18-
_accel.writeRegister(XYZ_DATA_CFG, 0x02); //Original 0x02
19-
_accel.writeRegister(CTRL_REG2, 0x00); //Original 0x00
17+
_accel.writeRegister(CTRL_REG1, 0x02); //Original 0x02 //F_READ
18+
_accel.writeRegister(XYZ_DATA_CFG, 0x02); //Original 0x02 //FS1
19+
_accel.writeRegister(CTRL_REG2, 0x00); //Original 0x00 //Standby
2020
_accel.writeRegister(F_SETUP, 0x00); //Original 0x00
21-
_accel.writeRegister(TRIG_CFG, 0x08); //Original 0x08
22-
_accel.writeRegister(PULSE_CFG, 0x54); //Original 0x54
21+
_accel.writeRegister(TRIG_CFG, 0x08); //Original 0x08 //Trig_PULSE/ZSPEFE/ELE
22+
_accel.writeRegister(PULSE_CFG, 0x54); //Original 0x54 //YSPEFE
2323
_accel.setupTap(0x1B, 0x3F, 0x3F); //Original 0x1B, 0x3F, 0x3F
24-
_accel.writeRegister(PULSE_TMLT, 0x28); //Original 0x28
25-
_accel.writeRegister(PULSE_LTCY, 0x7F); //Original 0x7F
26-
_accel.writeRegister(HP_FILTER_CUTOFF, 0x10); //Original 0x10
24+
_accel.writeRegister(PULSE_TMLT, 0x28); //Original 0x28 //TMLT3/TMLT5
25+
_accel.writeRegister(PULSE_LTCY, 0x7F); //Original 0x7F //LTCY6/LTCY5/LTCY4/LTCY3/LTCY2/LTCY1/LTCY0
26+
_accel.writeRegister(HP_FILTER_CUTOFF, 0x10); //Original 0x10 //Pulse_LPF_EN
2727

28-
_accel.writeRegister(CTRL_REG3, 0x12); //Original 0x12
29-
_accel.writeRegister(CTRL_REG4, 0x40); //Original 0x40
30-
_accel.writeRegister(CTRL_REG5, 0x40); //Original 0x40
31-
_accel.writeRegister(CTRL_REG1, 0x03); //Original 0x03
28+
_accel.writeRegister(CTRL_REG3, 0x12); //Original 0x12 //WAKE_PULSE/IPOL
29+
_accel.writeRegister(CTRL_REG4, 0x40); //Original 0x40 //INT_EN_FIFO
30+
_accel.writeRegister(CTRL_REG5, 0x40); //Original 0x40 //INT_CFG_FIFO INT1
31+
_accel.writeRegister(CTRL_REG1, 0x03); //Original 0x03 //F_READ/ACTIVE
3232

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

5252
uint8_t tap = _accel.readTap();
5353
if (tap) {
54-
bool AxZ = tap&0b1000000; //event on axis
55-
bool AxY = tap&0b0100000;
56-
bool AxX = tap&0b0010000;
57-
//bool DPE = tap&0b0001000; //double
58-
bool PolZ = tap&0b0000100; //0=positive 1=negative
59-
bool PolY = tap&0b0000010;
60-
bool PolX = tap&0b0000001;
54+
bool AxZ = (tap&0b1000000)>0; //event on axis
55+
bool AxY = (tap&0b0100000)>0;
56+
bool AxX = (tap&0b0010000)>0;
57+
//bool DPE = (tap&0b0001000)>0; //double
58+
bool PolZ = !((tap&0b0000100)>0); //0=positive 1=negative
59+
bool PolY = !((tap&0b0000010)>0);
60+
bool PolX = !((tap&0b0000001)>0);
6161

6262
//X+ = box bottom
6363
//X- = box top
@@ -80,22 +80,85 @@ void BoxAccelerometer::loop() {
8080
if (AxY && AxZ) {
8181
if (PolY && PolZ) {
8282
tapOn = TapOn::BACK;
83-
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Blue, 2);
83+
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Blue, 3);
8484
} else if (!PolY && !PolZ) {
8585
tapOn = TapOn::FRONT;
86-
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Violet, 2);
86+
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Violet, 3);
8787
} else if (PolY && !PolZ) {
8888
tapOn = TapOn::LEFT;
89-
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Green, 2);
89+
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Green, 3);
9090
} else if (!PolY && PolZ) {
9191
tapOn = TapOn::RIGHT;
92-
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::GreenYellow, 2);
92+
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::GreenYellow, 3);
93+
}
94+
} else if (AxY) {
95+
if (PolY) {
96+
tapOn = TapOn::LEFT_BACK;
97+
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Blue, 3);
98+
} else {
99+
tapOn = TapOn::LEFT_FRONT;
100+
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Violet, 3);
101+
}
102+
} else if (AxZ) {
103+
if (PolZ) {
104+
tapOn = TapOn::RIGHT_BACK;
105+
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::Green, 3);
106+
} else {
107+
tapOn = TapOn::RIGHT_FRONT;
108+
Box.boxLEDs.setActiveAnimationByIteration(BoxLEDs::ANIMATION_TYPE::BLINK, BoxLEDs::CRGB::GreenYellow, 3);
93109
}
94110
}
95111

96-
Log.verbose("Tap recieved %B, direction %X", tap, tapOn);
112+
Log.disableNewline(true);
113+
Log.verbose("Tap recieved %B, direction=%X, ", tap, tapOn);
114+
Log.disableNewline(false);
115+
switch (tapOn)
116+
{
117+
case TapOn::LEFT:
118+
Log.printfln("LEFT");
119+
break;
120+
121+
case TapOn::RIGHT:
122+
Log.printfln("RIGHT");
123+
break;
124+
125+
case TapOn::FRONT:
126+
Log.printfln("FRONT");
127+
break;
128+
129+
case TapOn::BACK:
130+
Log.printfln("BACK");
131+
break;
132+
133+
case TapOn::TOP:
134+
Log.printfln("TOP");
135+
break;
136+
137+
case TapOn::BOTTOM:
138+
Log.printfln("BOTTOM");
139+
break;
97140

141+
case TapOn::LEFT_FRONT:
142+
Log.printfln("LEFT_FRONT");
143+
break;
144+
145+
case TapOn::RIGHT_FRONT:
146+
Log.printfln("RIGHT_FRONT");
147+
break;
148+
149+
case TapOn::LEFT_BACK:
150+
Log.printfln("LEFT_BACK");
151+
break;
98152

153+
case TapOn::RIGHT_BACK:
154+
Log.printfln("RIGHT_BACK");
155+
break;
156+
157+
default:
158+
break;
159+
Log.printfln("OTHER");
160+
}
161+
99162
}
100163

101164
}

BoxAccelerometer.h

+12-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ class BoxAccelerometer : public EnhancedThread {
2121
EARS_DOWN2 = 0x4
2222
};
2323
enum class TapOn {
24-
NONE = 0x0,
25-
LEFT = 0x1,
26-
RIGHT = 0x2, //17,34,68 - 34
27-
FRONT = 0x3, //16, 17
28-
BACK = 0x4,
29-
TOP = 0x5, //16,32,64,68,24 - 16
30-
BOTTOM = 0x6 //17
24+
NONE = 0x00,
25+
LEFT = 0x01,
26+
RIGHT = 0x02, //17,34,68 - 34
27+
FRONT = 0x04, //16, 17
28+
BACK = 0x08,
29+
TOP = 0x10, //16,32,64,68,24 - 16
30+
BOTTOM = 0x20, //17
31+
32+
LEFT_FRONT = LEFT + FRONT,
33+
RIGHT_FRONT = RIGHT + FRONT,
34+
LEFT_BACK = LEFT + BACK,
35+
RIGHT_BACK = RIGHT + BACK
3136
};
3237

3338
void

BoxBattery.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@ void BoxBattery::begin() {
88

99
_wasLow = false;
1010
_wasCritical = false;
11-
_batteryAdcRaw = analogRead(60);
12-
_batteryAdcLowRaw = 9999;
11+
_readBatteryAdc();
1312

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

1716
loop();
1817
logBatteryStatus();
1918

20-
setInterval(500);
19+
setInterval(100);
2120
}
2221
void BoxBattery::loop() {
23-
_batteryAdcRaw = analogRead(60);
22+
_readBatteryAdc();
2423
_charger.read();
2524

2625
if (_batteryAdcRaw < _batteryAdcLowRaw || isChargerConnected())
@@ -46,6 +45,11 @@ void BoxBattery::loop() {
4645
}
4746
}
4847

48+
void BoxBattery::_readBatteryAdc() {
49+
uint16_t adcValue = analogReadAvg(BATTERY_VOLTAGE_PIN, 1);
50+
_batteryAdcRaw = adcValue;
51+
}
52+
4953
bool BoxBattery::isChargerConnected() {
5054
if (_charger.isPressed())
5155
return true;
@@ -55,10 +59,7 @@ uint16_t BoxBattery::getBatteryAdcRaw() {
5559
return _batteryAdcRaw;
5660
}
5761
uint16_t BoxBattery::getBatteryVoltage() {
58-
if (isChargerConnected()) {
59-
return 10000 * getBatteryAdcRaw() / _batteryVoltageChargerFactor;
60-
}
61-
return 10000 * getBatteryAdcRaw() / _batteryVoltageFactor;
62+
return 1000 * getBatteryAdcRaw() / _batteryVoltageFactor;
6263
}
6364
bool BoxBattery::isBatteryLow() {
6465
if (getBatteryAdcRaw() < _batteryLowAdc)
@@ -88,7 +89,6 @@ void BoxBattery::reloadConfig() {
8889
ConfigStruct* config = Config.get();
8990

9091
_batteryVoltageFactor = config->battery.voltageFactor;
91-
_batteryVoltageChargerFactor = config->battery.voltageChargerFactor;
9292
_batteryLowAdc = config->battery.lowAdc;
9393
_batteryCriticalAdc = config->battery.criticalAdc;
9494
}
@@ -144,7 +144,7 @@ void BoxBattery::startBatteryTest() {
144144
file.writeString("Comments");
145145
file.writeString("\r\n");
146146
file.writeString("0;;;;;;");
147-
sprintf(output, "vFactor=%lu, vChargerFactor=%lu;v2-wav", _batteryVoltageFactor, _batteryVoltageChargerFactor);
147+
sprintf(output, "vFactor=%lu;v3-wav", _batteryVoltageFactor);
148148
file.writeString(output);
149149
file.writeString("\r\n");
150150
file.close();

BoxBattery.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class BoxBattery : public EnhancedThread {
5252

5353
private:
5454
uint32_t _batteryVoltageFactor;
55-
uint32_t _batteryVoltageChargerFactor;
5655
uint16_t _batteryLowAdc;
5756
uint16_t _batteryCriticalAdc;
5857

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

6564
const char* _batteryTestFilename = "/revvox/batteryTest.csv";
6665
uint64_t _batteryTestStartMillis;
66+
67+
const static uint8_t BATTERY_VOLTAGE_PIN = 60;
68+
void _readBatteryAdc();
6769
};
6870

6971
#endif

BoxConfig.cpp

+18-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ String BoxConfig::getAsJson() {
5353
JsonObject batteryDoc = doc.createNestedObject("battery");
5454
ConfigBattery* batteryCfg = &_config.battery;
5555
batteryDoc["voltageFactor"] = batteryCfg->voltageFactor;
56-
batteryDoc["voltageChargerFactor"] = batteryCfg->voltageChargerFactor;
5756
batteryDoc["lowAdc"] = batteryCfg->lowAdc;
5857
batteryDoc["criticalAdc"] = batteryCfg->criticalAdc;
5958
batteryDoc["sleepMinutes"] = batteryCfg->sleepMinutes;
@@ -76,6 +75,7 @@ String BoxConfig::getAsJson() {
7675
ConfigMisc* miscCfg = &_config.misc;
7776
miscDoc["autodump"] = miscCfg->autodump;
7877
miscDoc["swd"] = miscCfg->swd;
78+
miscDoc["watchdogSeconds"] = miscCfg->watchdogSeconds;
7979

8080
_json = "";
8181
serializeJson(doc, _json);
@@ -95,7 +95,6 @@ bool BoxConfig::setFromJson(String json) {
9595
JsonObject batteryDoc = doc["battery"];
9696
ConfigBattery* batteryCfg = &_config.battery;
9797
batteryCfg->voltageFactor = batteryDoc["voltageFactor"].as<uint32_t>();
98-
batteryCfg->voltageChargerFactor = batteryDoc["voltageChargerFactor"].as<uint32_t>();
9998
batteryCfg->lowAdc = batteryDoc["lowAdc"].as<uint16_t>();
10099
batteryCfg->criticalAdc = batteryDoc["criticalAdc"].as<uint16_t>();
101100
batteryCfg->sleepMinutes = batteryDoc["sleepMinutes"].as<uint8_t>();
@@ -118,6 +117,7 @@ bool BoxConfig::setFromJson(String json) {
118117
ConfigMisc* miscCfg = &_config.misc;
119118
miscCfg->autodump = miscDoc["autodump"].as<bool>();
120119
miscCfg->swd = miscDoc["swd"].as<bool>();
120+
miscCfg->watchdogSeconds = miscDoc["watchdogSeconds"].as<uint8_t>();
121121

122122
// Convert old config version to latest one.
123123
if (_config.version != CONFIG_ACTIVE_VERSION) {
@@ -132,6 +132,16 @@ bool BoxConfig::setFromJson(String json) {
132132
case 4:
133133
miscCfg->swd = false;
134134
_config.version = 5;
135+
case 5:
136+
batteryCfg->lowAdc = 9658;
137+
batteryCfg->criticalAdc = 8869;
138+
batteryCfg->voltageFactor = 27850;
139+
_config.version = 6;
140+
case 6:
141+
miscCfg->watchdogSeconds = 10;
142+
if (batteryCfg->voltageFactor == 67690) //Fix for wrong value in previous CFW
143+
batteryCfg->voltageFactor = 27850;
144+
_config.version = 7;
135145
write();
136146
break;
137147
default:
@@ -147,11 +157,12 @@ bool BoxConfig::setFromJson(String json) {
147157
void BoxConfig::_initializeConfig() {
148158
_config.version = CONFIG_ACTIVE_VERSION;
149159

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

157168
ConfigButtonEars* buttons = &_config.buttonEars;
@@ -173,4 +184,5 @@ void BoxConfig::_initializeConfig() {
173184
ConfigMisc* misc = &_config.misc;
174185
misc->autodump = false;
175186
misc->swd = false;
187+
misc->watchdogSeconds = 10;
176188
}

BoxConfig.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#include "BoxSD.h"
77

88

9-
#define BOXCONFIG_JSON_SIZE 528
10-
//{"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}}
9+
#define BOXCONFIG_JSON_SIZE 560
10+
//{"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}}
1111
//Size from https://arduinojson.org/v6/assistant/
1212

1313
class BoxConfig {

0 commit comments

Comments
 (0)