mirror of
https://github.com/rstrouse/ESPSomfy-RTS.git
synced 2026-10-31 22:02:17 +01:00
The shade behaviour module (SHADE_BEHAVIOUR_PLAN.md, PR B) reads its settings through read-only structs, so they get one home each: RemoteSettings (flipCommands, bitLength, repeats) under SomfyRemote, shared with groups, and ShadeSettings (types, travel times, step size, flipPosition, simMy, sunSensor) under SomfyShade. Both are base structs, so shade->upTime and group->repeats keep working. A shade's flags byte is split by owner: simMy and sunSensor are settings, light is a field, and the sun flag, sunny, windy, demo mode and lighted bits are stateFlags. getFlags()/setFlags() put the byte together and take it apart for the config file, JSON, MQTT, events and group flags, so nothing saved or sent changes. Groups keep their own flags byte. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
77 lines
3 KiB
C++
77 lines
3 KiB
C++
#include <unity.h>
|
|
#include "ShadeSettings.h"
|
|
|
|
static const uint8_t SUN_FLAG = static_cast<uint8_t>(somfy_flags_t::SunFlag);
|
|
static const uint8_t SUN_SENSOR = static_cast<uint8_t>(somfy_flags_t::SunSensor);
|
|
static const uint8_t DEMO_MODE = static_cast<uint8_t>(somfy_flags_t::DemoMode);
|
|
static const uint8_t LIGHT = static_cast<uint8_t>(somfy_flags_t::Light);
|
|
static const uint8_t WINDY = static_cast<uint8_t>(somfy_flags_t::Windy);
|
|
static const uint8_t SUNNY = static_cast<uint8_t>(somfy_flags_t::Sunny);
|
|
static const uint8_t LIGHTED = static_cast<uint8_t>(somfy_flags_t::Lighted);
|
|
static const uint8_t SIM_MY = static_cast<uint8_t>(somfy_flags_t::SimMy);
|
|
|
|
void setUp() {}
|
|
void tearDown() {}
|
|
|
|
static void test_every_byte_comes_back_unchanged() {
|
|
// The config file and JSON hold the whole byte, so splitting it and putting
|
|
// it back together must not lose or move a bit.
|
|
for(int flags = 0; flags <= 0xFF; flags++) {
|
|
ShadeSettings settings;
|
|
bool light = false;
|
|
uint8_t state = 0;
|
|
splitShadeFlags(static_cast<uint8_t>(flags), settings, light, state);
|
|
TEST_ASSERT_EQUAL_HEX8(flags, joinShadeFlags(settings, light, state));
|
|
}
|
|
}
|
|
static void test_setting_bits_go_to_the_settings() {
|
|
ShadeSettings settings;
|
|
bool light = false;
|
|
uint8_t state = 0xFF;
|
|
splitShadeFlags(SUN_SENSOR | LIGHT | SIM_MY, settings, light, state);
|
|
TEST_ASSERT_TRUE(settings.sunSensor);
|
|
TEST_ASSERT_TRUE(light);
|
|
TEST_ASSERT_TRUE(settings.simMy);
|
|
TEST_ASSERT_EQUAL_HEX8(0, state);
|
|
}
|
|
static void test_state_bits_stay_state() {
|
|
ShadeSettings settings;
|
|
settings.sunSensor = settings.simMy = true;
|
|
bool light = true;
|
|
uint8_t state = 0;
|
|
const uint8_t stateBits = SUN_FLAG | DEMO_MODE | WINDY | SUNNY | LIGHTED;
|
|
splitShadeFlags(stateBits, settings, light, state);
|
|
TEST_ASSERT_FALSE(settings.sunSensor);
|
|
TEST_ASSERT_FALSE(light);
|
|
TEST_ASSERT_FALSE(settings.simMy);
|
|
TEST_ASSERT_EQUAL_HEX8(stateBits, state);
|
|
}
|
|
static void test_join_ignores_setting_bits_in_the_state() {
|
|
// The settings decide those bits, whatever the state byte holds.
|
|
ShadeSettings settings;
|
|
TEST_ASSERT_EQUAL_HEX8(WINDY, joinShadeFlags(settings, false, WINDY | SUN_SENSOR | LIGHT | SIM_MY));
|
|
settings.simMy = true;
|
|
TEST_ASSERT_EQUAL_HEX8(SIM_MY | SUNNY, joinShadeFlags(settings, false, SUNNY));
|
|
}
|
|
static void test_defaults() {
|
|
ShadeSettings shade;
|
|
TEST_ASSERT_EQUAL_HEX8(0, joinShadeFlags(shade, false, 0));
|
|
TEST_ASSERT_EQUAL_UINT32(10000, shade.upTime);
|
|
TEST_ASSERT_EQUAL_UINT32(10000, shade.downTime);
|
|
TEST_ASSERT_EQUAL_UINT32(7000, shade.tiltTime);
|
|
TEST_ASSERT_EQUAL_UINT16(100, shade.stepSize);
|
|
RemoteSettings remote;
|
|
TEST_ASSERT_FALSE(remote.flipCommands);
|
|
TEST_ASSERT_EQUAL_UINT8(0, remote.bitLength);
|
|
TEST_ASSERT_EQUAL_UINT8(1, remote.repeats);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
UNITY_BEGIN();
|
|
RUN_TEST(test_every_byte_comes_back_unchanged);
|
|
RUN_TEST(test_setting_bits_go_to_the_settings);
|
|
RUN_TEST(test_state_bits_stay_state);
|
|
RUN_TEST(test_join_ignores_setting_bits_in_the_state);
|
|
RUN_TEST(test_defaults);
|
|
return UNITY_END();
|
|
}
|