Skip to content

Commit

Permalink
Initial implement start/stop control of KingSmith R2 when press start…
Browse files Browse the repository at this point in the history
…/pause/stop buttons (cagnulein#813)
  • Loading branch information
d3m3vilurr authored Oct 12, 2024
1 parent 88ba956 commit cd918f3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/devices/kingsmithr2treadmill/kingsmithr2treadmill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ kingsmithr2treadmill::kingsmithr2treadmill(uint32_t pollDeviceTime, bool noConso
if (forceInitInclination > 0) {
lastInclination = forceInitInclination;
}
if (lastControlMode != UNKNOWN_CONTROL_MODE) {
lastControlMode = UNKNOWN_CONTROL_MODE;
}
if (lastRunState != UNKNOWN_RUN_STATE) {
lastRunState = UNKNOWN_RUN_STATE;
}

refresh = new QTimer(this);
initDone = false;
Expand Down Expand Up @@ -205,16 +211,25 @@ void kingsmithr2treadmill::update() {
}
if (requestStart != -1) {
emit debug(QStringLiteral("starting..."));
if (lastControlMode != MANUAL) {
writeCharacteristic(QStringLiteral("props ControlMode 1"), QStringLiteral("turn on treadmill to manual mode"),
false, true);
}
if (lastRunState != START) {
writeCharacteristic(QStringLiteral("props runState 1"), QStringLiteral("starting"), false, true);
}
if (lastSpeed == 0.0) {

lastSpeed = 0.5;
}
// btinit(true);
requestStart = -1;
emit tapeStarted();
}
if (requestStop != -1) {
emit debug(QStringLiteral("stopping..."));
if (lastRunState != STOP) {
writeCharacteristic(QStringLiteral("props runState 0"), QStringLiteral("stopping"), false, true);
}
// don't go to standby mode automatically
requestStop = -1;
}
if (requestFanSpeed != -1) {
Expand Down Expand Up @@ -297,6 +312,11 @@ void kingsmithr2treadmill::characteristicChanged(const QLowEnergyCharacteristic
QStringList _props = data.split(QStringLiteral(" "), QString::SkipEmptyParts);
for (int i = 1; i < _props.size(); i += 2) {
QString key = _props.at(i);
// Error key only can have error code
// props Error "ErrorCode" -5000
if (!key.compare(QStringLiteral("Error"))) {
break;
}
// skip string params
if (!key.compare(QStringLiteral("mcu_version")) || !key.compare(QStringLiteral("goal"))) {
continue;
Expand All @@ -312,6 +332,8 @@ void kingsmithr2treadmill::characteristicChanged(const QLowEnergyCharacteristic

double speed = props.value("CurrentSpeed", 0);
Cadence = props.value("spm", 0);
KINGSMITH_R2_CONTROL_MODE controlMode = (KINGSMITH_R2_CONTROL_MODE)(int)props.value("ControlMode", (double)UNKNOWN_CONTROL_MODE);
KINGSMITH_R2_RUN_STATE runState = (KINGSMITH_R2_RUN_STATE)(int)props.value("runState", (double)UNKNOWN_RUN_STATE);

// TODO:
// - RunningDistance (int; meter) : update each 10miters / 0.01 mile
Expand All @@ -320,6 +342,9 @@ void kingsmithr2treadmill::characteristicChanged(const QLowEnergyCharacteristic
// - RunningTotalTime (int; sec)
// - spm (int) : steps per minute

// TODO: check 'ControlMode' and 'runState' of treadmill side
// then update current running status of application.

#ifdef Q_OS_ANDROID
if (settings.value(QZSettings::ant_heart, QZSettings::default_ant_heart).toBool())
Heart = (uint8_t)KeepAwakeHelper::heart();
Expand Down Expand Up @@ -376,6 +401,16 @@ void kingsmithr2treadmill::characteristicChanged(const QLowEnergyCharacteristic
// lastInclination = incline;
}

if (lastControlMode != controlMode) {
lastControlMode = controlMode;
if (controlMode != UNKNOWN_CONTROL_MODE) {
emit debug(QStringLiteral("kingsmith r2 is ready"));
initDone = true;
}
}
if (lastRunState != runState) {
lastRunState = runState;
}
firstCharacteristicChanged = false;
}

Expand Down Expand Up @@ -403,7 +438,6 @@ void kingsmithr2treadmill::btinit(bool startTape) {
// QStringLiteral("servers getProp 1 3 7 8 9 16 17 18 19 21 22 23 24 31"), QStringLiteral("init"), false, true);
writeCharacteristic(QStringLiteral("servers getProp 1 2 7 12 23 24 31"), QStringLiteral("init"), false, true);

// TODO need reset BurnCalories & RunningDistance
initDone = true;
}

Expand Down
5 changes: 5 additions & 0 deletions src/devices/kingsmithr2treadmill/kingsmithr2treadmill.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ class kingsmithr2treadmill : public treadmill {
QDateTime lastTimeCharacteristicChanged;
bool firstCharacteristicChanged = true;

enum KINGSMITH_R2_CONTROL_MODE { AUTOMODE = 0, MANUAL, STANDBY, UNKNOWN_CONTROL_MODE };
enum KINGSMITH_R2_RUN_STATE { STOP = 0, START, UNKNOWN_RUN_STATE };
KINGSMITH_R2_CONTROL_MODE lastControlMode = UNKNOWN_CONTROL_MODE;
KINGSMITH_R2_RUN_STATE lastRunState = UNKNOWN_RUN_STATE;

QTimer *refresh;

QLowEnergyService *gattCommunicationChannelService = nullptr;
Expand Down

0 comments on commit cd918f3

Please sign in to comment.