xref: /aosp_15_r20/external/pigweed/pw_digital_io/digital_io_mock.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #define PW_LOG_MODULE_NAME "IO"
16 #include "pw_digital_io/digital_io_mock.h"
17 
18 #include "pw_log/log.h"
19 
20 namespace pw::digital_io {
21 
DigitalInOutMockImpl(Clock & clock,pw::InlineDeque<Event> & events)22 DigitalInOutMockImpl::DigitalInOutMockImpl(Clock& clock,
23                                            pw::InlineDeque<Event>& events)
24     : clock_(clock), events_(events) {}
25 
DoGetState()26 pw::Result<DigitalInOutMockImpl::State> DigitalInOutMockImpl::DoGetState() {
27   return events_.back().state;
28 }
29 
DoSetState(State state)30 pw::Status DigitalInOutMockImpl::DoSetState(State state) {
31   if (events_.full()) {
32     events_.pop_front();
33   }
34   // Log the LED state instead of toggling a physical LED. On host, there is no
35   // LED to blink, so this is a portable alternative.
36   switch (state) {
37     case State::kInactive:
38       PW_LOG_INFO("[ ]");
39       break;
40     case State::kActive:
41       PW_LOG_INFO("[*]");
42       break;
43   }
44   events_.push_back({clock_.now(), state});
45   return pw::OkStatus();
46 }
47 
48 }  // namespace pw::digital_io