Generalized-Core-Counter 3.20
Particle-based generalized core counter firmware
Loading...
Searching...
No Matches
State_Modes.cpp
Go to the documentation of this file.
2#include "Config.h"
3#include "Cloud.h"
4#include "LocalTimeRK.h"
5#include "MyPersistentData.h"
6#include "PublishQueuePosixRK.h"
7#include "SensorManager.h"
8#include "device_pinout.h"
9#include "SensorDefinitions.h"
10
11// NOTE:
12// This file was split from StateHandlers.cpp as a mechanical refactor.
13// No behavioral changes were made.
14
15// *************** Mode-Specific Handler Functions ***************
16
25 // Check if sensor has new data
27 // Increment counters
28 current.set_hourlyCount(current.get_hourlyCount() + 1);
29 current.set_dailyCount(current.get_dailyCount() + 1);
30 current.set_lastCountTime(Time.now());
31
32 // Log the new count once per event
33 Log.info("Count detected - Hourly: %d, Daily: %d",
34 current.get_hourlyCount(), current.get_dailyCount());
35
36 // Flash the on-module BLUE LED for ~1 second as a
37 // visual count indicator using a software timer so we
38 // don't block the main loop.
39 digitalWrite(BLUE_LED, HIGH);
40 if (countSignalTimer.isActive()) {
41 countSignalTimer.reset();
42 } else {
43 countSignalTimer.start();
44 }
45
46 // Stay in IDLE_STATE; hourly reporting will publish aggregated counts.
47 }
48}
49
59 // Check if sensor has new data
61 // Sensor detected presence
62 if (!current.get_occupied()) {
63 // Transition from unoccupied to occupied
64 current.set_occupied(true);
65 current.set_occupancyStartTime(Time.now());
66
67 Log.info("Space now OCCUPIED at %s", Time.timeStr().c_str());
68 digitalWrite(BLUE_LED, HIGH); // Visual indicator
69 }
70
71 // Update last event time (resets debounce timer)
72 current.set_lastOccupancyEvent(millis());
73
74 if (sysStatus.get_verboseMode()) {
75 uint32_t occupiedDuration = Time.now() - current.get_occupancyStartTime();
76 Log.info("Occupancy event - Duration: %lu seconds", occupiedDuration);
77 }
78 }
79
80 // Check if we need to update occupancy state (timeout check)
82}
83
92 if (!current.get_occupied()) {
93 return; // Nothing to do if not occupied
94 }
95
96 uint32_t debounceMs = sysStatus.get_occupancyDebounceMs();
97 uint32_t timeSinceLastEvent = millis() - current.get_lastOccupancyEvent();
98
99 // Check if debounce timeout has expired
100 if (timeSinceLastEvent > debounceMs) {
101 // Calculate this occupancy session duration
102 uint32_t sessionDuration = Time.now() - current.get_occupancyStartTime();
103
104 // Add to total occupied seconds for the day
105 uint32_t totalOccupied = current.get_totalOccupiedSeconds() + sessionDuration;
106 current.set_totalOccupiedSeconds(totalOccupied);
107
108 // Mark as unoccupied
109 current.set_occupied(false);
110 current.set_occupancyStartTime(0);
111
112 Log.info("Space now UNOCCUPIED - Session duration: %lu seconds, Total today: %lu seconds",
113 sessionDuration, totalOccupied);
114
115 digitalWrite(BLUE_LED, LOW); // Turn off visual indicator
116 }
117}
Cloud Configuration Management - Particle Ledger integration for device configuration.
Global compile-time configuration options and enums.
Persistent Data Storage Structures - EEPROM/Retained Memory Management.
#define sysStatus
#define current
Singleton wrapper around ISensor implementations.
void handleOccupancyMode()
Handle sensor events in OCCUPANCY mode.
void updateOccupancyState()
Update occupancy state based on debounce timeout.
void handleCountingMode()
Handle sensor events in COUNTING mode.
Timer countSignalTimer
static SensorManager & instance()
Get the SensorManager singleton instance.
const pin_t BLUE_LED
Pinout definitions for the carrier board and sensors.