Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_SwitchAggregator.h
Go to the documentation of this file.
8#pragma once
9
12#include <common/cs_Component.h>
16#include <test/cs_TestAccess.h>
17#include <optional>
18
25 friend class TestAccess<SwitchAggregator>;
26public:
28
29 void init(const boards_config_t& board);
30
36 virtual void handleEvent(event_t& evt) override;
37
42
43protected:
44 std::vector<Component*> getChildren() override;
45
46private:
50
52
53 // the latest states requested by other parts of the system.
54 std::optional<uint8_t> _overrideState = {};
55 std::optional<uint8_t> _behaviourState = {};
56 std::optional<uint8_t> _twilightState = {};
57
58 // the last state that was aggregated and passed on towards the SoftwareSwitch.
59 std::optional<uint8_t> _aggregatedState = {};
60
61 // Cache of previous time update.
62 uint32_t _lastTimestamp = 0;
63
66
70
79
80 // Max number of switch commands to store in history.
81 const static uint8_t _maxSwitchHistoryItems = 10;
82
88
89 // ================================== State updaters ==================================
90
105 cs_ret_code_t updateState(bool allowOverrideReset, const cmd_source_with_counter_t& source);
106
114
120 void update();
121
122 // ================================== Event handling ==================================
123
129
136
146 bool registerSwitchcraftEvent(uint8_t currentValue);
147
156 uint8_t getStateIntentionSwitchcraft(uint8_t currentValue, bool doubleTap);
157
166
174
180
190
191 void handleSwitchStateChange(uint8_t newIntensity);
192
193 // ================================== Misc ==================================
194
204
211 uint8_t resolveOverrideState(uint8_t overrideState);
212
219
221
224
225 void printStates(uint32_t lineNumber);
226
228};
Definition: cs_BehaviourHandler.h:18
Keeps track of the behaviours that are active on this crownstone.
Definition: cs_BehaviourStore.h:25
Circular Buffer implementation.
Definition: cs_CircularBuffer.h:27
Helper class to manage decoupling of components.
Definition: cs_Component.h:35
Event listener.
Definition: cs_EventListener.h:17
Class that:
Definition: cs_SmartSwitch.h:20
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Sep 26,...
Definition: cs_SwitchAggregator.h:24
bool handlePresenceEvents(event_t &evt)
EVT_PRESENCE_MUTATION.
bool handleBehaviourEvents(event_t &evt)
Handles the following events:
uint32_t _lastTimestamp
Definition: cs_SwitchAggregator.h:62
void executeStateIntentionUpdate(uint8_t value, cmd_source_with_counter_t &source)
Tries to update [overrideState] to [value] and then calls updateState(false).
void update()
Updates internal state and adjusts the switch values based on the new state.
bool registerSwitchcraftEvent(uint8_t currentValue)
Registers a switchcraft event and checks if it's a double tap action.
TwilightHandler _twilightHandler
Definition: cs_SwitchAggregator.h:47
cmd_source_with_counter_t _source
Which source claimed the switch.
Definition: cs_SwitchAggregator.h:77
std::optional< uint8_t > _overrideState
Definition: cs_SwitchAggregator.h:54
std::optional< uint8_t > _aggregatedState
Definition: cs_SwitchAggregator.h:59
void printStates(uint32_t lineNumber)
bool handleTimingEvents(event_t &evt)
EVT_TICK, STATE_TIME and EVT_TIME_SET events possibly trigger a new aggregated state.
CircularBuffer< cs_switch_history_item_t > _switchHistory
Keep up a history of switch commands.
Definition: cs_SwitchAggregator.h:87
static const uint8_t _maxSwitchHistoryItems
Definition: cs_SwitchAggregator.h:81
std::optional< uint8_t > _behaviourState
Definition: cs_SwitchAggregator.h:55
uint8_t resolveOverrideState(uint8_t overrideState)
Returns the switch state that should be set according to the override state.
bool checkAndSetOwner(const cmd_source_with_counter_t &source)
Tries to set source as owner of the switch.
bool updateBehaviourHandlers()
Calls update on the behaviour handlers and returns true if after the update is allowed to reset the o...
SmartSwitch _smartSwitch
Definition: cs_SwitchAggregator.h:51
virtual void handleEvent(event_t &evt) override
When swSwitch is locked, only CMD_SWITCH_LOCKED events will be handled.
BehaviourStore * _behaviourStore
Definition: cs_SwitchAggregator.h:49
uint16_t _switchcraftDoubleTapCountdown
Set on switchcraft event, then decremented each tick event until 0.
Definition: cs_SwitchAggregator.h:65
uint32_t _ownerTimeoutCountdown
Definition: cs_SwitchAggregator.h:78
void init(const boards_config_t &board)
std::vector< Component * > getChildren() override
Components with children can override this method to return them.
void switchPowered()
To be called when there is enough power to use the switch.
uint8_t aggregatedBehaviourIntensity()
Aggregates the value of twilightHandler and behaviourHandler into a single intensity value.
uint8_t getStateIntentionSwitchcraft(uint8_t currentValue, bool doubleTap)
Get the state intention from a switchcraft event.
std::optional< uint8_t > _twilightState
Definition: cs_SwitchAggregator.h:56
uint8_t _lastSwitchcraftOnValue
Keeps up the switch value (1-100 from smart switch) of the last time it was on, before being turned o...
Definition: cs_SwitchAggregator.h:69
void printSwitchHistory()
bool handleSwitchAggregatorCommand(event_t &evt)
Clearing private variables, setting them to specific values, etc.
void handleGetBehaviourDebug(event_t &evt)
BehaviourHandler _behaviourHandler
Definition: cs_SwitchAggregator.h:48
void addToSwitchHistory(const cs_switch_history_item_t &cmd)
cs_ret_code_t updateState(bool allowOverrideReset, const cmd_source_with_counter_t &source)
Checks the behaviourState and overrideState, to set the swSwitch to the desired value:
void pushTestDataToHost()
void handleSwitchStateChange(uint8_t newIntensity)
bool handleStateIntentionEvents(event_t &evt)
Triggers an updateState() call on all handled events and adjusts at least one of behaviourState or ov...
Definition: cs_TestAccess.h:11
Definition: cs_TwilightHandler.h:17
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Oct 9,...
Definition: cs_Event.h:26
@ CS_CMD_SOURCE_NONE
Definition: cs_CmdSource.h:23
uint16_t cs_ret_code_t
Definition: cs_Typedefs.h:21
Board configuration.
Definition: cs_Boards.h:169
Struct that tells where a command originated from.
Definition: cs_CmdSource.h:54
Definition: cs_Packets.h:486