@@ -16,14 +16,21 @@ using namespace chip::DeviceLayer;
1616
1717static constexpr uint32_t kMoveIntervalMs = 100 ;
1818
19- GarageDoorImpl::GarageDoorImpl (const pwm_dt_spec *spec) : mSpec(spec) {}
19+ GarageDoorImpl::GarageDoorImpl (const pwm_dt_spec *spec) : mSpec(spec), mCurrentPosition( " cp " ) {}
2020
2121CHIP_ERROR GarageDoorImpl::Init ()
2222{
23+ ReturnErrorOnFailure (mCurrentPosition .Load ());
24+ mObserver ->OnSetup (mCurrentPosition .Get ());
25+
2326 if (mPhysicalIndicator .Init (mSpec , 0 , 255 ) != 0 ) {
2427 LOG_ERR (" Cannot initialize the physical indicator" );
2528 return CHIP_ERROR_INCORRECT_STATE;
2629 }
30+ uint8_t brightness =
31+ static_cast <uint8_t >((static_cast <uint32_t >(mCurrentPosition .Get ()) * 255U + 5000U ) / 10000U );
32+ mPhysicalIndicator .InitiateAction (Nrf::PWMDevice::LEVEL_ACTION, 0 , &brightness);
33+
2734 return CHIP_NO_ERROR;
2835}
2936
@@ -43,7 +50,7 @@ CHIP_ERROR GarageDoorImpl::Stop()
4350{
4451 SystemLayer ().CancelTimer (TimerTimeoutCallback, this );
4552 LOG_DBG (" Movement stopped" );
46- mObserver ->OnMovementStopped (mCurrentPosition );
53+ mObserver ->OnMovementStopped (mCurrentPosition . Get () );
4754 return CHIP_NO_ERROR;
4855}
4956
@@ -60,25 +67,26 @@ void GarageDoorImpl::HandleTimer()
6067 uint32_t movePerTick32 = (static_cast <uint32_t >(mSpeed ) * kMoveIntervalMs ) / 1000U ;
6168 uint16_t movePerTick = static_cast <uint16_t >(std::min<uint32_t >(movePerTick32, UINT16_MAX));
6269 uint16_t distanceLeft = 0 ;
63- if (mCurrentPosition <= mTargetPosition ) {
64- distanceLeft = mTargetPosition - mCurrentPosition ;
70+
71+ if (mCurrentPosition .Get () <= mTargetPosition ) {
72+ distanceLeft = mTargetPosition - mCurrentPosition .Get ();
6573 if (movePerTick >= distanceLeft) {
6674 finished = true ;
67- mCurrentPosition = mTargetPosition ;
75+ mCurrentPosition . Set ( mTargetPosition , true ) ;
6876 } else {
69- mCurrentPosition += movePerTick;
77+ mCurrentPosition . Set ( mCurrentPosition . Get () + movePerTick) ;
7078 }
7179 } else {
72- distanceLeft = mCurrentPosition - mTargetPosition ;
80+ distanceLeft = mCurrentPosition . Get () - mTargetPosition ;
7381 if (movePerTick >= distanceLeft) {
7482 finished = true ;
75- mCurrentPosition = mTargetPosition ;
83+ mCurrentPosition . Set ( mTargetPosition , true ) ;
7684 } else {
77- mCurrentPosition -= movePerTick;
85+ mCurrentPosition . Set ( mCurrentPosition . Get () - movePerTick) ;
7886 }
7987 }
80-
81- uint8_t brightness = static_cast <uint8_t >((static_cast <uint32_t >(mCurrentPosition ) * 255U + 5000U ) / 10000U );
88+ uint8_t brightness =
89+ static_cast <uint8_t >((static_cast <uint32_t >(mCurrentPosition . Get () ) * 255U + 5000U ) / 10000U );
8290
8391 mPhysicalIndicator .InitiateAction (Nrf::PWMDevice::LEVEL_ACTION, 0 , &brightness);
8492
@@ -87,7 +95,7 @@ void GarageDoorImpl::HandleTimer()
8795 } else {
8896 uint32_t timeLeftMs = (static_cast <uint32_t >(distanceLeft) * kMoveIntervalMs ) / movePerTick;
8997 uint16_t timeLeftS = static_cast <uint16_t >((timeLeftMs + 999 ) / 1000 ); /* ceil*/
90- mObserver ->OnMovementUpdate (mCurrentPosition , timeLeftS, mJustStarted );
98+ mObserver ->OnMovementUpdate (mCurrentPosition . Get () , timeLeftS, mJustStarted );
9199 mJustStarted = false ;
92100 auto err = SystemLayer ().StartTimer (System::Clock::Milliseconds32 (kMoveIntervalMs ),
93101 TimerTimeoutCallback, this );
0 commit comments