SenseoWifi icon indicating copy to clipboard operation
SenseoWifi copied to clipboard

Refactor of the Fsm to be more modular (component based) + various improvments

Open Pierre-33 opened this issue 1 year ago • 7 comments

Hey! So this is the result of my last weeks of work. It's kind of a huge refactor, so let me know if you accept that as a PR or if you'd rather have me stay in my fork. I'll try to describe here the most noticeable change.

SenseoWifi.cpp has been slim down and now mostly handle setup and Homie things like Mqtt handlers. Among the most noticeable things :

  • I noticed that you read the configuration variable before Homie' setup which results in invalid value
  • HomeAssistant sensors are now automatically created thanks to the publication of HomeAssistant AutoDiscovery Config
  • All the Fsm logic is now on separate files (one per states). And states are easily overidable. I thought I would need to override some states behavior with my button hack, but I turns out I didn't. However it should make things easier if some future Senseo machine behave differently.
  • You'll find code here to handle my CustomizableButtonAddon, which is totally optional.
  • Mqtt handlers now go through a Command pattern to communicate with the Fsm. So does the button when the Addon is installed.

The folder SenseoFsm contain all the states logic which is basically the same as before. You'll find in that folder a component folder. Each of them represents a different functionality of the Senseo, like the Buzzer or the CupDetector. You should recognize your code inside most of them. The nice thing is that most of them are totally optional. If you don't have a Buzzer, just don't add the BuzzerComponent when you create the Fsm :)

Regarding the LedObserver :

  • I fix an issue when the boot time was longer than 3 * pulseContThreshold, the led could stay stuck in LED_unknown (it might have been introduced by slightly longer boot time with all my modifications, not sure about that)
  • The LedObserver should now be thread safe, in your implementation it could happen every now and then that the interrupt kick in right in the middle of SenseoLed::updateState execution. It could result in inconsistency between ledChangeMillis and prevLedChangeMillis, or worst, totally random value into your float. (Note that I didn't encounter that issue myself, I just infer it from reading the code)
  • My first thread safe implementation was using the Hardware Timer, unfortunately I learned the hard way that the tone() function uses it too... The code is still present in HwTimerLedObserver and as a result it's now quite easy to try different implementations of the LedObserver through a common interface.

I change all the formatting to something more VScode compliant. At first I tried to comply with the previous formatting but then I realize that it was most likely because your project started with the Arduino Ide and that it didn't make much sense with a modern IDE. Hope it wasn't a mistake...

In terms of pure features, here is what you can expect (line in bold require the CustomizableButtonAddon) :

  • Home Assistant auto discovery
  • Implementation of EzBuzzer with some nice tunes (I could use some help with the "sound design")
  • Full brewing cycle (on -> heating -> ready -> brewing -> off) is now handled by the Senseo. No need for an HA automation anymore.
  • Pressing one cup or two cups buttons on the Senseo trigger a full cycle
  • If the cup is removed during brewing, the brewing is canceled
  • If brewing is requested through Mqtt without a cup, the brewing cycle starts until the Senseo is ready and then wait for the cup before brewing
  • Same things if the brewing is requested from the Senseo Buttons
  • Decicated led blinking pattern for Unknown State and Brewing State
  • Holding all the three buttons altogether will beep 3 times and then reset the Senseo
  • Holding one cup or two cup buttons will raise a boolean in Mqtt (a switch on HA) that you can then use in an automation to program your coffee on the next morning. While this switch is raised, the led will blink once per minute
  • Holding the power button will reset that switch

All that is quite fresh and would probably require a few weeks of testing to be sure everyting is smooth

Pierre-33 avatar Mar 09 '23 18:03 Pierre-33