GroupState.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. #include <GroupState.h>
  2. #include <Units.h>
  3. #include <MiLightRemoteConfig.h>
  4. #include <RGBConverter.h>
  5. static const char* BULB_MODE_NAMES[] = {
  6. "white",
  7. "color",
  8. "scene",
  9. "night"
  10. };
  11. const BulbId DEFAULT_BULB_ID;
  12. static const GroupStateField ALL_PHYSICAL_FIELDS[] = {
  13. GroupStateField::BRIGHTNESS,
  14. GroupStateField::BULB_MODE,
  15. GroupStateField::HUE,
  16. GroupStateField::KELVIN,
  17. GroupStateField::MODE,
  18. GroupStateField::SATURATION,
  19. GroupStateField::STATE
  20. };
  21. static const GroupStateField ALL_SCRATCH_FIELDS[] = {
  22. GroupStateField::BRIGHTNESS,
  23. GroupStateField::KELVIN
  24. };
  25. // Number of units each increment command counts for
  26. static const uint8_t INCREMENT_COMMAND_VALUE = 10;
  27. const GroupState& GroupState::defaultState(MiLightRemoteType remoteType) {
  28. static GroupState instances[MiLightRemoteConfig::NUM_REMOTES];
  29. GroupState& state = instances[remoteType];
  30. switch (remoteType) {
  31. case REMOTE_TYPE_RGB:
  32. state.setBulbMode(BULB_MODE_COLOR);
  33. break;
  34. case REMOTE_TYPE_CCT:
  35. state.setBulbMode(BULB_MODE_WHITE);
  36. break;
  37. default:
  38. // No modifications needed
  39. break;
  40. }
  41. return state;
  42. }
  43. BulbId::BulbId()
  44. : deviceId(0),
  45. groupId(0),
  46. deviceType(REMOTE_TYPE_UNKNOWN)
  47. { }
  48. BulbId::BulbId(const BulbId &other)
  49. : deviceId(other.deviceId),
  50. groupId(other.groupId),
  51. deviceType(other.deviceType)
  52. { }
  53. BulbId::BulbId(
  54. const uint16_t deviceId, const uint8_t groupId, const MiLightRemoteType deviceType
  55. )
  56. : deviceId(deviceId),
  57. groupId(groupId),
  58. deviceType(deviceType)
  59. { }
  60. void BulbId::operator=(const BulbId &other) {
  61. deviceId = other.deviceId;
  62. groupId = other.groupId;
  63. deviceType = other.deviceType;
  64. }
  65. // determine if now BulbId's are the same. This compared deviceID (the controller/remote ID) and
  66. // groupId (the group number on the controller, 1-4 or 1-8 depending), but ignores the deviceType
  67. // (type of controller/remote) as this doesn't directly affect the identity of the bulb
  68. bool BulbId::operator==(const BulbId &other) {
  69. return deviceId == other.deviceId
  70. && groupId == other.groupId
  71. && deviceType == other.deviceType;
  72. }
  73. void GroupState::initFields() {
  74. state.fields._state = 0;
  75. state.fields._brightness = 0;
  76. state.fields._brightnessColor = 0;
  77. state.fields._brightnessMode = 0;
  78. state.fields._hue = 0;
  79. state.fields._saturation = 0;
  80. state.fields._mode = 0;
  81. state.fields._bulbMode = 0;
  82. state.fields._kelvin = 0;
  83. state.fields._isSetState = 0;
  84. state.fields._isSetHue = 0;
  85. state.fields._isSetBrightness = 0;
  86. state.fields._isSetBrightnessColor = 0;
  87. state.fields._isSetBrightnessMode = 0;
  88. state.fields._isSetSaturation = 0;
  89. state.fields._isSetMode = 0;
  90. state.fields._isSetKelvin = 0;
  91. state.fields._isSetBulbMode = 0;
  92. state.fields._dirty = 1;
  93. state.fields._mqttDirty = 0;
  94. state.fields._isSetNightMode = 0;
  95. state.fields._isNightMode = 0;
  96. scratchpad.fields._isSetBrightnessScratch = 0;
  97. scratchpad.fields._brightnessScratch = 0;
  98. scratchpad.fields._isSetKelvinScratch = 0;
  99. scratchpad.fields._kelvinScratch = 0;
  100. }
  101. GroupState& GroupState::operator=(const GroupState& other) {
  102. memcpy(state.rawData, other.state.rawData, DATA_LONGS * sizeof(uint32_t));
  103. scratchpad.rawData = other.scratchpad.rawData;
  104. return *this;
  105. }
  106. GroupState::GroupState()
  107. : previousState(NULL)
  108. {
  109. initFields();
  110. }
  111. GroupState::GroupState(const GroupState& other)
  112. : previousState(NULL)
  113. {
  114. memcpy(state.rawData, other.state.rawData, DATA_LONGS * sizeof(uint32_t));
  115. scratchpad.rawData = other.scratchpad.rawData;
  116. }
  117. GroupState::GroupState(const GroupState* previousState, const JsonObject& jsonState)
  118. : previousState(previousState)
  119. {
  120. initFields();
  121. if (previousState != NULL) {
  122. this->scratchpad = previousState->scratchpad;
  123. }
  124. patch(jsonState);
  125. }
  126. bool GroupState::operator==(const GroupState& other) const {
  127. return memcmp(state.rawData, other.state.rawData, DATA_LONGS * sizeof(uint32_t)) == 0;
  128. }
  129. bool GroupState::isEqualIgnoreDirty(const GroupState& other) const {
  130. GroupState meCopy = *this;
  131. GroupState otherCopy = other;
  132. meCopy.clearDirty();
  133. meCopy.clearMqttDirty();
  134. otherCopy.clearDirty();
  135. otherCopy.clearMqttDirty();
  136. return meCopy == otherCopy;
  137. }
  138. void GroupState::print(Stream& stream) const {
  139. stream.printf("State: %08X %08X\n", state.rawData[0], state.rawData[1]);
  140. }
  141. bool GroupState::clearField(GroupStateField field) {
  142. bool clearedAny = false;
  143. switch (field) {
  144. // Always set and can't be cleared
  145. case GroupStateField::COMPUTED_COLOR:
  146. case GroupStateField::DEVICE_ID:
  147. case GroupStateField::GROUP_ID:
  148. case GroupStateField::DEVICE_TYPE:
  149. break;
  150. case GroupStateField::STATE:
  151. case GroupStateField::STATUS:
  152. clearedAny = isSetState();
  153. state.fields._isSetState = 0;
  154. break;
  155. case GroupStateField::BRIGHTNESS:
  156. case GroupStateField::LEVEL:
  157. clearedAny = clearBrightness();
  158. break;
  159. case GroupStateField::COLOR:
  160. case GroupStateField::HUE:
  161. case GroupStateField::OH_COLOR:
  162. clearedAny = isSetHue();
  163. state.fields._isSetHue = 0;
  164. break;
  165. case GroupStateField::SATURATION:
  166. clearedAny = isSetSaturation();
  167. state.fields._isSetSaturation = 0;
  168. break;
  169. case GroupStateField::MODE:
  170. case GroupStateField::EFFECT:
  171. clearedAny = isSetMode();
  172. state.fields._isSetMode = 0;
  173. break;
  174. case GroupStateField::KELVIN:
  175. case GroupStateField::COLOR_TEMP:
  176. clearedAny = isSetKelvin();
  177. state.fields._isSetKelvin = 0;
  178. break;
  179. case GroupStateField::BULB_MODE:
  180. clearedAny = isSetBulbMode();
  181. state.fields._isSetBulbMode = 0;
  182. // Clear brightness as well
  183. clearedAny = clearBrightness() || clearedAny;
  184. break;
  185. default:
  186. Serial.printf_P(PSTR("Attempted to clear unknown field: %d\n"), static_cast<uint8_t>(field));
  187. break;
  188. }
  189. return clearedAny;
  190. }
  191. bool GroupState::isSetField(GroupStateField field) const {
  192. switch (field) {
  193. case GroupStateField::COMPUTED_COLOR:
  194. // Always set -- either send RGB color or white
  195. return true;
  196. case GroupStateField::DEVICE_ID:
  197. case GroupStateField::GROUP_ID:
  198. case GroupStateField::DEVICE_TYPE:
  199. // These are always defined
  200. return true;
  201. case GroupStateField::STATE:
  202. case GroupStateField::STATUS:
  203. return isSetState();
  204. case GroupStateField::BRIGHTNESS:
  205. case GroupStateField::LEVEL:
  206. return isSetBrightness();
  207. case GroupStateField::COLOR:
  208. case GroupStateField::HUE:
  209. case GroupStateField::OH_COLOR:
  210. return isSetHue();
  211. case GroupStateField::SATURATION:
  212. return isSetSaturation();
  213. case GroupStateField::MODE:
  214. return isSetMode();
  215. case GroupStateField::EFFECT:
  216. return isSetEffect();
  217. case GroupStateField::KELVIN:
  218. case GroupStateField::COLOR_TEMP:
  219. return isSetKelvin();
  220. case GroupStateField::BULB_MODE:
  221. return isSetBulbMode();
  222. default:
  223. Serial.print(F("WARNING: tried to check if unknown field was set: "));
  224. Serial.println(static_cast<unsigned int>(field));
  225. break;
  226. }
  227. return false;
  228. }
  229. bool GroupState::isSetScratchField(GroupStateField field) const {
  230. switch (field) {
  231. case GroupStateField::BRIGHTNESS:
  232. return scratchpad.fields._isSetBrightnessScratch;
  233. case GroupStateField::KELVIN:
  234. return scratchpad.fields._isSetKelvinScratch;
  235. default:
  236. Serial.print(F("WARNING: tried to check if unknown scratch field was set: "));
  237. Serial.println(static_cast<unsigned int>(field));
  238. break;
  239. }
  240. return false;
  241. }
  242. uint16_t GroupState::getFieldValue(GroupStateField field) const {
  243. switch (field) {
  244. case GroupStateField::STATE:
  245. case GroupStateField::STATUS:
  246. return getState();
  247. case GroupStateField::BRIGHTNESS:
  248. return getBrightness();
  249. case GroupStateField::HUE:
  250. return getHue();
  251. case GroupStateField::SATURATION:
  252. return getSaturation();
  253. case GroupStateField::MODE:
  254. return getMode();
  255. case GroupStateField::KELVIN:
  256. return getKelvin();
  257. case GroupStateField::BULB_MODE:
  258. return getBulbMode();
  259. default:
  260. Serial.print(F("WARNING: tried to fetch value for unknown field: "));
  261. Serial.println(static_cast<unsigned int>(field));
  262. break;
  263. }
  264. return 0;
  265. }
  266. uint16_t GroupState::getScratchFieldValue(GroupStateField field) const {
  267. switch (field) {
  268. case GroupStateField::BRIGHTNESS:
  269. return scratchpad.fields._brightnessScratch;
  270. case GroupStateField::KELVIN:
  271. return scratchpad.fields._kelvinScratch;
  272. default:
  273. Serial.print(F("WARNING: tried to fetch value for unknown scratch field: "));
  274. Serial.println(static_cast<unsigned int>(field));
  275. break;
  276. }
  277. return 0;
  278. }
  279. void GroupState::setFieldValue(GroupStateField field, uint16_t value) {
  280. switch (field) {
  281. case GroupStateField::STATE:
  282. case GroupStateField::STATUS:
  283. setState(static_cast<MiLightStatus>(value));
  284. break;
  285. case GroupStateField::BRIGHTNESS:
  286. setBrightness(value);
  287. break;
  288. case GroupStateField::HUE:
  289. setHue(value);
  290. break;
  291. case GroupStateField::SATURATION:
  292. setSaturation(value);
  293. break;
  294. case GroupStateField::MODE:
  295. setMode(value);
  296. break;
  297. case GroupStateField::KELVIN:
  298. setKelvin(value);
  299. break;
  300. case GroupStateField::BULB_MODE:
  301. setBulbMode(static_cast<BulbMode>(value));
  302. break;
  303. default:
  304. Serial.print(F("WARNING: tried to set value for unknown field: "));
  305. Serial.println(static_cast<unsigned int>(field));
  306. break;
  307. }
  308. }
  309. void GroupState::setScratchFieldValue(GroupStateField field, uint16_t value) {
  310. switch (field) {
  311. case GroupStateField::BRIGHTNESS:
  312. scratchpad.fields._isSetBrightnessScratch = 1;
  313. scratchpad.fields._brightnessScratch = value;
  314. break;
  315. case GroupStateField::KELVIN:
  316. scratchpad.fields._isSetKelvinScratch = 1;
  317. scratchpad.fields._kelvinScratch = value;
  318. break;
  319. default:
  320. Serial.print(F("WARNING: tried to set value for unknown scratch field: "));
  321. Serial.println(static_cast<unsigned int>(field));
  322. break;
  323. }
  324. }
  325. bool GroupState::isSetState() const { return state.fields._isSetState; }
  326. MiLightStatus GroupState::getState() const { return state.fields._state ? ON : OFF; }
  327. bool GroupState::isOn() const {
  328. return !isNightMode() && (!isSetState() || getState() == MiLightStatus::ON);
  329. }
  330. bool GroupState::setState(const MiLightStatus status) {
  331. if (!isNightMode() && isSetState() && getState() == status) {
  332. return false;
  333. }
  334. setDirty();
  335. state.fields._isSetState = 1;
  336. state.fields._state = status == ON ? 1 : 0;
  337. // Changing status will clear night mode
  338. setNightMode(false);
  339. return true;
  340. }
  341. bool GroupState::isSetBrightness() const {
  342. // If we don't know what mode we're in, just assume white mode. Do this for a few
  343. // reasons:
  344. // * Some bulbs don't have multiple modes
  345. // * It's confusing to not have a default
  346. if (! isSetBulbMode()) {
  347. return state.fields._isSetBrightness;
  348. }
  349. switch (state.fields._bulbMode) {
  350. case BULB_MODE_WHITE:
  351. return state.fields._isSetBrightness;
  352. case BULB_MODE_COLOR:
  353. return state.fields._isSetBrightnessColor;
  354. case BULB_MODE_SCENE:
  355. return state.fields._isSetBrightnessMode;
  356. }
  357. return false;
  358. }
  359. bool GroupState::clearBrightness() {
  360. bool cleared = false;
  361. if (!state.fields._isSetBulbMode) {
  362. cleared = state.fields._isSetBrightness;
  363. state.fields._isSetBrightness = 0;
  364. } else {
  365. switch (state.fields._bulbMode) {
  366. case BULB_MODE_COLOR:
  367. cleared = state.fields._isSetBrightnessColor;
  368. state.fields._isSetBrightnessColor = 0;
  369. break;
  370. case BULB_MODE_SCENE:
  371. cleared = state.fields._isSetBrightnessMode;
  372. state.fields._isSetBrightnessMode = 0;
  373. break;
  374. case BULB_MODE_WHITE:
  375. cleared = state.fields._isSetBrightness;
  376. state.fields._isSetBrightness = 0;
  377. break;
  378. }
  379. }
  380. return cleared;
  381. }
  382. uint8_t GroupState::getBrightness() const {
  383. switch (state.fields._bulbMode) {
  384. case BULB_MODE_WHITE:
  385. return state.fields._brightness;
  386. case BULB_MODE_COLOR:
  387. return state.fields._brightnessColor;
  388. case BULB_MODE_SCENE:
  389. return state.fields._brightnessMode;
  390. }
  391. return 0;
  392. }
  393. bool GroupState::setBrightness(uint8_t brightness) {
  394. if (isSetBrightness() && getBrightness() == brightness) {
  395. return false;
  396. }
  397. setDirty();
  398. uint8_t bulbMode = state.fields._bulbMode;
  399. if (! state.fields._isSetBulbMode) {
  400. bulbMode = BULB_MODE_WHITE;
  401. }
  402. switch (bulbMode) {
  403. case BULB_MODE_WHITE:
  404. state.fields._isSetBrightness = 1;
  405. state.fields._brightness = brightness;
  406. break;
  407. case BULB_MODE_COLOR:
  408. state.fields._isSetBrightnessColor = 1;
  409. state.fields._brightnessColor = brightness;
  410. break;
  411. case BULB_MODE_SCENE:
  412. state.fields._isSetBrightnessMode = 1;
  413. state.fields._brightnessMode = brightness;
  414. default:
  415. return false;
  416. }
  417. return true;
  418. }
  419. bool GroupState::isSetHue() const { return state.fields._isSetHue; }
  420. uint16_t GroupState::getHue() const {
  421. return Units::rescale<uint16_t, uint16_t>(state.fields._hue, 360, 255);
  422. }
  423. bool GroupState::setHue(uint16_t hue) {
  424. if (isSetHue() && getHue() == hue) {
  425. return false;
  426. }
  427. setDirty();
  428. state.fields._isSetHue = 1;
  429. state.fields._hue = Units::rescale<uint16_t, uint16_t>(hue, 255, 360);
  430. return true;
  431. }
  432. bool GroupState::isSetSaturation() const { return state.fields._isSetSaturation; }
  433. uint8_t GroupState::getSaturation() const { return state.fields._saturation; }
  434. bool GroupState::setSaturation(uint8_t saturation) {
  435. if (isSetSaturation() && getSaturation() == saturation) {
  436. return false;
  437. }
  438. setDirty();
  439. state.fields._isSetSaturation = 1;
  440. state.fields._saturation = saturation;
  441. return true;
  442. }
  443. bool GroupState::isSetMode() const { return state.fields._isSetMode; }
  444. bool GroupState::isSetEffect() const {
  445. // only BULB_MODE_COLOR does not have an effect.
  446. return isSetBulbMode() && getBulbMode() != BULB_MODE_COLOR;
  447. }
  448. uint8_t GroupState::getMode() const { return state.fields._mode; }
  449. bool GroupState::setMode(uint8_t mode) {
  450. if (isSetMode() && getMode() == mode) {
  451. return false;
  452. }
  453. setDirty();
  454. state.fields._isSetMode = 1;
  455. state.fields._mode = mode;
  456. return true;
  457. }
  458. bool GroupState::isSetKelvin() const { return state.fields._isSetKelvin; }
  459. uint8_t GroupState::getKelvin() const { return state.fields._kelvin; }
  460. uint16_t GroupState::getMireds() const {
  461. return Units::whiteValToMireds(getKelvin(), 100);
  462. }
  463. bool GroupState::setKelvin(uint8_t kelvin) {
  464. if (isSetKelvin() && getKelvin() == kelvin) {
  465. return false;
  466. }
  467. setDirty();
  468. state.fields._isSetKelvin = 1;
  469. state.fields._kelvin = kelvin;
  470. return true;
  471. }
  472. bool GroupState::setMireds(uint16_t mireds) {
  473. return setKelvin(Units::miredsToWhiteVal(mireds, 100));
  474. }
  475. bool GroupState::isSetBulbMode() const {
  476. return (isSetNightMode() && isNightMode()) || state.fields._isSetBulbMode;
  477. }
  478. BulbMode GroupState::getBulbMode() const {
  479. // Night mode is a transient state. When power is toggled, the bulb returns
  480. // to the state it was last in. To handle this case, night mode state is
  481. // stored separately.
  482. if (isSetNightMode() && isNightMode()) {
  483. return BULB_MODE_NIGHT;
  484. } else {
  485. return static_cast<BulbMode>(state.fields._bulbMode);
  486. }
  487. }
  488. bool GroupState::setBulbMode(BulbMode bulbMode) {
  489. if (isSetBulbMode() && getBulbMode() == bulbMode) {
  490. return false;
  491. }
  492. setDirty();
  493. // As mentioned in isSetBulbMode, NIGHT_MODE is stored separately.
  494. if (bulbMode == BULB_MODE_NIGHT) {
  495. setNightMode(true);
  496. } else {
  497. state.fields._isSetBulbMode = 1;
  498. state.fields._bulbMode = bulbMode;
  499. }
  500. return true;
  501. }
  502. bool GroupState::isSetNightMode() const { return state.fields._isSetNightMode; }
  503. bool GroupState::isNightMode() const { return state.fields._isNightMode; }
  504. bool GroupState::setNightMode(bool nightMode) {
  505. if (isSetNightMode() && isNightMode() == nightMode) {
  506. return false;
  507. }
  508. setDirty();
  509. state.fields._isSetNightMode = 1;
  510. state.fields._isNightMode = nightMode;
  511. return true;
  512. }
  513. bool GroupState::isDirty() const { return state.fields._dirty; }
  514. inline bool GroupState::setDirty() {
  515. state.fields._dirty = 1;
  516. state.fields._mqttDirty = 1;
  517. return true;
  518. }
  519. bool GroupState::clearDirty() {
  520. state.fields._dirty = 0;
  521. return true;
  522. }
  523. bool GroupState::isMqttDirty() const { return state.fields._mqttDirty; }
  524. bool GroupState::clearMqttDirty() {
  525. state.fields._mqttDirty = 0;
  526. return true;
  527. }
  528. void GroupState::load(Stream& stream) {
  529. for (size_t i = 0; i < DATA_LONGS; i++) {
  530. stream.readBytes(reinterpret_cast<uint8_t*>(&state.rawData[i]), 4);
  531. }
  532. clearDirty();
  533. }
  534. void GroupState::dump(Stream& stream) const {
  535. for (size_t i = 0; i < DATA_LONGS; i++) {
  536. stream.write(reinterpret_cast<const uint8_t*>(&state.rawData[i]), 4);
  537. }
  538. }
  539. bool GroupState::applyIncrementCommand(GroupStateField field, IncrementDirection dir) {
  540. if (field != GroupStateField::KELVIN && field != GroupStateField::BRIGHTNESS) {
  541. Serial.print(F("WARNING: tried to apply increment for unsupported field: "));
  542. Serial.println(static_cast<uint8_t>(field));
  543. return false;
  544. }
  545. int8_t dirValue = static_cast<int8_t>(dir);
  546. // If there's already a known value, update it
  547. if (previousState != NULL && previousState->isSetField(field)) {
  548. int8_t currentValue = static_cast<int8_t>(previousState->getFieldValue(field));
  549. int8_t newValue = currentValue + (dirValue * INCREMENT_COMMAND_VALUE);
  550. #ifdef STATE_DEBUG
  551. previousState->debugState("Updating field from increment command");
  552. #endif
  553. // For now, assume range for both brightness and kelvin is [0, 100]
  554. setFieldValue(field, constrain(newValue, 0, 100));
  555. return true;
  556. // Otherwise start or update scratch state
  557. } else {
  558. if (isSetScratchField(field)) {
  559. int8_t newValue = static_cast<int8_t>(getScratchFieldValue(field)) + dirValue;
  560. if (newValue == 0 || newValue == 10) {
  561. setFieldValue(field, newValue * INCREMENT_COMMAND_VALUE);
  562. return true;
  563. } else {
  564. setScratchFieldValue(field, newValue);
  565. }
  566. } else if (dir == IncrementDirection::DECREASE) {
  567. setScratchFieldValue(field, 9);
  568. } else {
  569. setScratchFieldValue(field, 1);
  570. }
  571. #ifdef STATE_DEBUG
  572. Serial.print(F("Updated scratch field: "));
  573. Serial.print(static_cast<int8_t>(field));
  574. Serial.print(F(" to: "));
  575. Serial.println(getScratchFieldValue(field));
  576. #endif
  577. }
  578. return false;
  579. }
  580. bool GroupState::clearNonMatchingFields(const GroupState& other) {
  581. #ifdef STATE_DEBUG
  582. this->debugState("Clearing fields. Current state");
  583. other.debugState("Other state");
  584. #endif
  585. bool clearedAny = false;
  586. for (size_t i = 0; i < size(ALL_PHYSICAL_FIELDS); ++i) {
  587. GroupStateField field = ALL_PHYSICAL_FIELDS[i];
  588. if (other.isSetField(field) && isSetField(field) && getFieldValue(field) != other.getFieldValue(field)) {
  589. if (clearField(field)) {
  590. clearedAny = true;
  591. }
  592. }
  593. }
  594. #ifdef STATE_DEBUG
  595. this->debugState("Result");
  596. #endif
  597. return clearedAny;
  598. }
  599. void GroupState::patch(const GroupState& other) {
  600. #ifdef STATE_DEBUG
  601. other.debugState("Patching existing state with: ");
  602. Serial.println();
  603. #endif
  604. for (size_t i = 0; i < size(ALL_PHYSICAL_FIELDS); ++i) {
  605. GroupStateField field = ALL_PHYSICAL_FIELDS[i];
  606. if (other.isSetField(field)) {
  607. setFieldValue(field, other.getFieldValue(field));
  608. }
  609. }
  610. for (size_t i = 0; i < size(ALL_SCRATCH_FIELDS); ++i) {
  611. GroupStateField field = ALL_SCRATCH_FIELDS[i];
  612. if (other.isSetScratchField(field)) {
  613. setScratchFieldValue(field, other.getScratchFieldValue(field));
  614. }
  615. }
  616. }
  617. /*
  618. Update group state to reflect a packet state
  619. Called both when a packet is sent locally, and when an intercepted packet is read
  620. (see main.cpp onPacketSentHandler)
  621. Returns true if the packet changes affects a state change
  622. */
  623. bool GroupState::patch(const JsonObject& state) {
  624. bool changes = false;
  625. #ifdef STATE_DEBUG
  626. Serial.print(F("Patching existing state with: "));
  627. state.printTo(Serial);
  628. Serial.println();
  629. #endif
  630. if (state.containsKey("state")) {
  631. bool stateChange = setState(state["state"] == "ON" ? ON : OFF);
  632. changes |= stateChange;
  633. }
  634. // Devices do not support changing their state while off, so don't apply state
  635. // changes to devices we know are off.
  636. if (isOn() && state.containsKey("brightness")) {
  637. bool stateChange = setBrightness(Units::rescale(state.get<uint8_t>("brightness"), 100, 255));
  638. changes |= stateChange;
  639. }
  640. if (isOn() && state.containsKey("hue")) {
  641. changes |= setHue(state["hue"]);
  642. changes |= setBulbMode(BULB_MODE_COLOR);
  643. }
  644. if (isOn() && state.containsKey("saturation")) {
  645. changes |= setSaturation(state["saturation"]);
  646. }
  647. if (isOn() && state.containsKey("mode")) {
  648. changes |= setMode(state["mode"]);
  649. changes |= setBulbMode(BULB_MODE_SCENE);
  650. }
  651. if (isOn() && state.containsKey("color_temp")) {
  652. changes |= setMireds(state["color_temp"]);
  653. changes |= setBulbMode(BULB_MODE_WHITE);
  654. }
  655. if (state.containsKey("command")) {
  656. const String& command = state["command"];
  657. if (isOn() && command == "set_white") {
  658. changes |= setBulbMode(BULB_MODE_WHITE);
  659. } else if (command == "night_mode") {
  660. changes |= setBulbMode(BULB_MODE_NIGHT);
  661. } else if (isOn() && command == "brightness_up") {
  662. changes |= applyIncrementCommand(GroupStateField::BRIGHTNESS, IncrementDirection::INCREASE);
  663. } else if (isOn() && command == "brightness_down") {
  664. changes |= applyIncrementCommand(GroupStateField::BRIGHTNESS, IncrementDirection::DECREASE);
  665. } else if (isOn() && command == "temperature_up") {
  666. changes |= applyIncrementCommand(GroupStateField::KELVIN, IncrementDirection::INCREASE);
  667. changes |= setBulbMode(BULB_MODE_WHITE);
  668. } else if (isOn() && command == "temperature_down") {
  669. changes |= applyIncrementCommand(GroupStateField::KELVIN, IncrementDirection::DECREASE);
  670. changes |= setBulbMode(BULB_MODE_WHITE);
  671. }
  672. }
  673. if (changes) {
  674. debugState("GroupState::patch: State changed");
  675. }
  676. else {
  677. debugState("GroupState::patch: State not changed");
  678. }
  679. return changes;
  680. }
  681. void GroupState::applyColor(ArduinoJson::JsonObject& state) const {
  682. uint8_t rgb[3];
  683. RGBConverter converter;
  684. converter.hsvToRgb(
  685. getHue()/360.0,
  686. // Default to fully saturated
  687. (isSetSaturation() ? getSaturation() : 100)/100.0,
  688. 1,
  689. rgb
  690. );
  691. applyColor(state, rgb[0], rgb[1], rgb[2]);
  692. }
  693. void GroupState::applyColor(ArduinoJson::JsonObject& state, uint8_t r, uint8_t g, uint8_t b) const {
  694. JsonObject& color = state.createNestedObject("color");
  695. color["r"] = r;
  696. color["g"] = g;
  697. color["b"] = b;
  698. }
  699. void GroupState::applyOhColor(ArduinoJson::JsonObject& state) const {
  700. uint8_t rgb[3];
  701. RGBConverter converter;
  702. converter.hsvToRgb(
  703. getHue()/360.0,
  704. // Default to fully saturated
  705. (isSetSaturation() ? getSaturation() : 100)/100.0,
  706. 1,
  707. rgb
  708. );
  709. char ohColorStr[13];
  710. sprintf(ohColorStr, "%d,%d,%d", rgb[0], rgb[1], rgb[2]);
  711. state["color"] = ohColorStr;
  712. }
  713. // gather partial state for a single field; see GroupState::applyState to gather many fields
  714. void GroupState::applyField(JsonObject& partialState, const BulbId& bulbId, GroupStateField field) const {
  715. if (isSetField(field)) {
  716. switch (field) {
  717. case GroupStateField::STATE:
  718. case GroupStateField::STATUS:
  719. partialState[GroupStateFieldHelpers::getFieldName(field)] = getState() == ON ? "ON" : "OFF";
  720. break;
  721. case GroupStateField::BRIGHTNESS:
  722. partialState["brightness"] = Units::rescale(getBrightness(), 255, 100);
  723. break;
  724. case GroupStateField::LEVEL:
  725. partialState["level"] = getBrightness();
  726. break;
  727. case GroupStateField::BULB_MODE:
  728. partialState["bulb_mode"] = BULB_MODE_NAMES[getBulbMode()];
  729. break;
  730. case GroupStateField::COLOR:
  731. if (getBulbMode() == BULB_MODE_COLOR) {
  732. applyColor(partialState);
  733. }
  734. break;
  735. case GroupStateField::OH_COLOR:
  736. if (getBulbMode() == BULB_MODE_COLOR) {
  737. applyOhColor(partialState);
  738. }
  739. break;
  740. case GroupStateField::COMPUTED_COLOR:
  741. if (getBulbMode() == BULB_MODE_COLOR) {
  742. applyColor(partialState);
  743. } else {
  744. applyColor(partialState, 255, 255, 255);
  745. }
  746. break;
  747. case GroupStateField::HUE:
  748. if (getBulbMode() == BULB_MODE_COLOR) {
  749. partialState["hue"] = getHue();
  750. }
  751. break;
  752. case GroupStateField::SATURATION:
  753. if (getBulbMode() == BULB_MODE_COLOR) {
  754. partialState["saturation"] = getSaturation();
  755. }
  756. break;
  757. case GroupStateField::MODE:
  758. if (getBulbMode() == BULB_MODE_SCENE) {
  759. partialState["mode"] = getMode();
  760. }
  761. break;
  762. case GroupStateField::EFFECT:
  763. if (getBulbMode() == BULB_MODE_SCENE) {
  764. partialState["effect"] = String(getMode());
  765. } else if (isSetBulbMode() && getBulbMode() == BULB_MODE_WHITE) {
  766. partialState["effect"] = "white_mode";
  767. } else if (getBulbMode() == BULB_MODE_NIGHT) {
  768. partialState["effect"] = "night_mode";
  769. }
  770. break;
  771. case GroupStateField::COLOR_TEMP:
  772. if (isSetBulbMode() && getBulbMode() == BULB_MODE_WHITE) {
  773. partialState["color_temp"] = getMireds();
  774. }
  775. break;
  776. case GroupStateField::KELVIN:
  777. if (isSetBulbMode() && getBulbMode() == BULB_MODE_WHITE) {
  778. partialState["kelvin"] = getKelvin();
  779. }
  780. break;
  781. case GroupStateField::DEVICE_ID:
  782. partialState["device_id"] = bulbId.deviceId;
  783. break;
  784. case GroupStateField::GROUP_ID:
  785. partialState["group_id"] = bulbId.groupId;
  786. break;
  787. case GroupStateField::DEVICE_TYPE:
  788. {
  789. const MiLightRemoteConfig* remoteConfig = MiLightRemoteConfig::fromType(bulbId.deviceType);
  790. if (remoteConfig) {
  791. partialState["device_type"] = remoteConfig->name;
  792. }
  793. }
  794. break;
  795. default:
  796. Serial.printf_P(PSTR("Tried to apply unknown field: %d\n"), static_cast<uint8_t>(field));
  797. break;
  798. }
  799. }
  800. }
  801. // helper function to debug the current state (in JSON) to the serial port
  802. void GroupState::debugState(char const *debugMessage) const {
  803. #ifdef STATE_DEBUG
  804. // using static to keep large buffers off the call stack
  805. static StaticJsonBuffer<500> jsonBuffer;
  806. // define fields to show (if count changes, make sure to update count to applyState below)
  807. GroupStateField fields[] {
  808. GroupStateField::BRIGHTNESS,
  809. GroupStateField::BULB_MODE,
  810. GroupStateField::COLOR_TEMP,
  811. GroupStateField::EFFECT,
  812. GroupStateField::HUE,
  813. GroupStateField::KELVIN,
  814. GroupStateField::MODE,
  815. GroupStateField::SATURATION,
  816. GroupStateField::STATE
  817. };
  818. // since our buffer is reused, make sure to clear it every time
  819. jsonBuffer.clear();
  820. JsonObject& jsonState = jsonBuffer.createObject();
  821. // Fake id
  822. BulbId id;
  823. // use applyState to build JSON of all fields (from above)
  824. applyState(jsonState, id, fields, size(fields));
  825. // convert to string and print
  826. Serial.printf("%s: ", debugMessage);
  827. jsonState.printTo(Serial);
  828. Serial.println("");
  829. #endif
  830. }
  831. // build up a partial state representation based on the specified GrouipStateField array. Used
  832. // to gather a subset of states (configurable in the UI) for sending to MQTT and web responses.
  833. void GroupState::applyState(JsonObject& partialState, const BulbId& bulbId, GroupStateField* fields, size_t numFields) const {
  834. for (size_t i = 0; i < numFields; i++) {
  835. applyField(partialState, bulbId, fields[i]);
  836. }
  837. }