Skip to content

Commit

Permalink
Merge pull request #8 from viordash/StatusBar_f
Browse files Browse the repository at this point in the history
Status bar f
  • Loading branch information
viordash committed Sep 13, 2024
2 parents 5eed8e7 + 5139662 commit f7fa9b9
Show file tree
Hide file tree
Showing 17 changed files with 460 additions and 83 deletions.
89 changes: 65 additions & 24 deletions PLC_esp8266/main/LogicProgram/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ bool Controller::runned = NULL;
EventGroupHandle_t Controller::gpio_events = NULL;
EventGroupHandle_t Controller::events = NULL;

uint8_t Controller::Var1 = LogicElement::MinValue;
uint8_t Controller::Var2 = LogicElement::MinValue;
uint8_t Controller::Var3 = LogicElement::MinValue;
uint8_t Controller::Var4 = LogicElement::MinValue;
uint8_t Controller::var1 = LogicElement::MinValue;
uint8_t Controller::var2 = LogicElement::MinValue;
uint8_t Controller::var3 = LogicElement::MinValue;
uint8_t Controller::var4 = LogicElement::MinValue;

Ladder *Controller::ladder = NULL;

std::recursive_mutex Controller::lock_io_values_mutex;
ControllerIOValues Controller::cached_io_values = {};

void Controller::Start(EventGroupHandle_t gpio_events) {
Controller::gpio_events = gpio_events;
Controller::events = xEventGroupCreate();
Expand All @@ -55,6 +58,7 @@ void Controller::Stop() {
tasks_stopping_timeout / portTICK_PERIOD_MS);

vEventGroupDelete(Controller::events);
Controller::cached_io_values = {};
delete ladder;
}

Expand All @@ -79,6 +83,7 @@ void Controller::ProcessTask(void *parm) {
read_adc_max_period_ms / portTICK_PERIOD_MS);

need_render |= (uxBits & (INPUT_1_IO_CLOSE | INPUT_1_IO_OPEN));
need_render |= SampleIOValues();
need_render |= ladder->DoAction();

if (need_render) {
Expand Down Expand Up @@ -115,40 +120,76 @@ void Controller::RenderTask(void *parm) {
vTaskDelete(NULL);
}

bool Controller::SampleIOValues() {
bool val_1bit;
uint16_t val_10bit;
uint8_t percent04;
ControllerIOValues io_values;

val_10bit = get_analog_value();
percent04 = val_10bit / 4;
io_values.AI = percent04;

val_1bit = get_digital_input_value();
percent04 = val_1bit ? LogicElement::MaxValue : LogicElement::MinValue;
io_values.DI = percent04;

val_1bit = get_digital_value(gpio_output::OUTPUT_0);
percent04 = val_1bit ? LogicElement::MaxValue : LogicElement::MinValue;
io_values.O1 = percent04;

val_1bit = get_digital_value(gpio_output::OUTPUT_1);
percent04 = val_1bit ? LogicElement::MaxValue : LogicElement::MinValue;
io_values.O2 = percent04;

io_values.V1 = Controller::var1;

io_values.V2 = Controller::var2;

io_values.V3 = Controller::var3;

io_values.V4 = Controller::var4;
{
std::lock_guard<std::recursive_mutex> lock(Controller::lock_io_values_mutex);
bool has_changes =
memcmp(&io_values, &Controller::cached_io_values, sizeof(io_values)) != 0;
Controller::cached_io_values = io_values;
return has_changes;
}
}

ControllerIOValues &Controller::GetIOValues() {
std::lock_guard<std::recursive_mutex> lock(Controller::lock_io_values_mutex);
return Controller::cached_io_values;
}

uint8_t Controller::GetAIRelativeValue() {
uint16_t val_10bit = get_analog_value();
uint8_t percent04 = val_10bit / 4;
ESP_LOGD(TAG_Controller, "adc, val_10bit:%u, percent04:%u", val_10bit, percent04);
uint8_t percent04 = Controller::cached_io_values.AI;
ESP_LOGD(TAG_Controller, "adc percent04:%u", percent04);
return percent04;
}

uint8_t Controller::GetDIRelativeValue() {
bool val_1bit = get_digital_input_value();
uint8_t percent04 = val_1bit ? LogicElement::MaxValue : LogicElement::MinValue;
return percent04;
return Controller::cached_io_values.DI;
}

uint8_t Controller::GetO1RelativeValue() {
uint8_t percent04 =
get_digital_value(gpio_output::OUTPUT_0) ? LogicElement::MaxValue : LogicElement::MinValue;
return percent04;
return Controller::cached_io_values.O1;
}
uint8_t Controller::GetO2RelativeValue() {
uint8_t percent04 =
get_digital_value(gpio_output::OUTPUT_1) ? LogicElement::MaxValue : LogicElement::MinValue;
return percent04;
return Controller::cached_io_values.O2;
}
uint8_t Controller::GetV1RelativeValue() {
return Controller::Var1;
return Controller::cached_io_values.V1;
}
uint8_t Controller::GetV2RelativeValue() {
return Controller::Var2;
return Controller::cached_io_values.V2;
}
uint8_t Controller::GetV3RelativeValue() {
return Controller::Var3;
return Controller::cached_io_values.V3;
}
uint8_t Controller::GetV4RelativeValue() {
return Controller::Var4;
return Controller::cached_io_values.V4;
}

void Controller::SetO1RelativeValue(uint8_t value) {
Expand All @@ -158,14 +199,14 @@ void Controller::SetO2RelativeValue(uint8_t value) {
set_digital_value(gpio_output::OUTPUT_1, value != LogicElement::MinValue);
}
void Controller::SetV1RelativeValue(uint8_t value) {
Controller::Var1 = value;
Controller::var1 = value;
}
void Controller::SetV2RelativeValue(uint8_t value) {
Controller::Var2 = value;
Controller::var2 = value;
}
void Controller::SetV3RelativeValue(uint8_t value) {
Controller::Var3 = value;
Controller::var3 = value;
}
void Controller::SetV4RelativeValue(uint8_t value) {
Controller::Var4 = value;
Controller::var4 = value;
}
26 changes: 21 additions & 5 deletions PLC_esp8266/main/LogicProgram/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,36 @@ extern "C" {
#include <stdlib.h>
#include <string.h>

typedef struct {
uint8_t AI;
uint8_t DI;
uint8_t O1;
uint8_t O2;
uint8_t V1;
uint8_t V2;
uint8_t V3;
uint8_t V4;
} ControllerIOValues;

class Controller {
private:
protected:
static bool runned;
static EventGroupHandle_t gpio_events;
static EventGroupHandle_t events;
static uint8_t Var1;
static uint8_t Var2;
static uint8_t Var3;
static uint8_t Var4;
static uint8_t var1;
static uint8_t var2;
static uint8_t var3;
static uint8_t var4;
static Ladder *ladder;

static std::recursive_mutex lock_io_values_mutex;
static ControllerIOValues cached_io_values;

public:
static void Start(EventGroupHandle_t gpio_events);
static void Stop();
static bool SampleIOValues();
static ControllerIOValues &GetIOValues();

static void ProcessTask(void *parm);
static void RenderTask(void *parm);
Expand Down
18 changes: 10 additions & 8 deletions PLC_esp8266/main/LogicProgram/StatusBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,31 @@ IRAM_ATTR bool StatusBar::Render(uint8_t *fb) {
uint8_t separator_width = 1;
Point point = { 2, y };

res &= indicator_AI->Render(fb, &point, Controller::GetAIRelativeValue());
ControllerIOValues io_values = Controller::GetIOValues();

res &= indicator_AI->Render(fb, &point, io_values.AI);
point.x += separator_width;

res &= indicator_DI->Render(fb, &point, Controller::GetDIRelativeValue());
res &= indicator_DI->Render(fb, &point, io_values.DI);
point.x += separator_width;

res &= indicator_O1->Render(fb, &point, Controller::GetO1RelativeValue());
res &= indicator_O1->Render(fb, &point, io_values.O1);
point.x += separator_width;

res &= indicator_O2->Render(fb, &point, Controller::GetO2RelativeValue());
res &= indicator_O2->Render(fb, &point, io_values.O2);
point.x += separator_width;

res &= indicator_V1->Render(fb, &point, Controller::GetV1RelativeValue());
res &= indicator_V1->Render(fb, &point, io_values.V1);
point.x += separator_width;

res &= indicator_V2->Render(fb, &point, Controller::GetV2RelativeValue());
res &= indicator_V2->Render(fb, &point, io_values.V2);
point.x += separator_width;

res &= indicator_V3->Render(fb, &point, Controller::GetV3RelativeValue());
res &= indicator_V3->Render(fb, &point, io_values.V3);
point.x += separator_width;

separator_width = 0;
res &= indicator_V4->Render(fb, &point, Controller::GetV4RelativeValue());
res &= indicator_V4->Render(fb, &point, io_values.V4);
point.x += separator_width;

res &= draw_horz_line(fb, 0, y + MapIOIndicator::GetHeight(), DISPLAY_WIDTH);
Expand Down
4 changes: 3 additions & 1 deletion Tests_esp8266/src/AllTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ IMPORT_TEST_GROUP(LogicDecOutputTestsGroup);
IMPORT_TEST_GROUP(LogicDirectOutputTestsGroup);
IMPORT_TEST_GROUP(LogicSetOutputTestsGroup);
IMPORT_TEST_GROUP(LogicResetOutputTestsGroup);
IMPORT_TEST_GROUP(LogicIncOutputTestsGroup);
IMPORT_TEST_GROUP(LogicIncOutputTestsGroup);

IMPORT_TEST_GROUP(LogicControllerTestsGroup);
16 changes: 15 additions & 1 deletion Tests_esp8266/src/logic_ComparatorEq_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ static uint8_t frame_buffer[DISPLAY_WIDTH * DISPLAY_HEIGHT / 8] = {};
TEST_GROUP(LogicComparatorEqTestsGroup){
//
TEST_SETUP(){ memset(frame_buffer, 0, sizeof(frame_buffer));

mock().expectOneCall("xEventGroupWaitBits").ignoreOtherParameters();
mock().expectOneCall("vEventGroupDelete").ignoreOtherParameters();
Controller::Stop();
}

TEST_TEARDOWN() {
Expand Down Expand Up @@ -71,39 +75,49 @@ TEST(LogicComparatorEqTestsGroup, DoAction_skip_when_incoming_passive) {

TEST(LogicComparatorEqTestsGroup, DoAction_change_state_to_active) {
volatile uint16_t adc = 49 / 0.1;
mock("0").expectNCalls(3, "gpio_get_level").ignoreOtherParameters();
mock("2").expectNCalls(3, "gpio_get_level").ignoreOtherParameters();
mock("15").expectNCalls(3, "gpio_get_level").ignoreOtherParameters();
mock()
.expectNCalls(3, "adc_read")
.withOutputParameterReturning("adc", (const void *)&adc, sizeof(adc));

TestableComparatorEq testable;
testable.SetReference(50 / 0.4);
testable.SetIoAdr(MapIO::AI);

CHECK_TRUE(Controller::SampleIOValues());
CHECK_FALSE(testable.DoAction(false, LogicItemState::lisActive));
CHECK_EQUAL(LogicItemState::lisPassive, *testable.PublicMorozov_Get_state());

adc = 51 / 0.1;
CHECK_TRUE(Controller::SampleIOValues());
CHECK_FALSE(testable.DoAction(false, LogicItemState::lisActive));
CHECK_EQUAL(LogicItemState::lisPassive, *testable.PublicMorozov_Get_state());

adc = 50 / 0.1;
CHECK_TRUE(Controller::SampleIOValues());
CHECK_TRUE(testable.DoAction(false, LogicItemState::lisActive));
CHECK_EQUAL(LogicItemState::lisActive, *testable.PublicMorozov_Get_state());
}

TEST(LogicComparatorEqTestsGroup, DoAction_change_state_to_passive) {
volatile uint16_t adc = 50 / 0.1;
mock("0").expectNCalls(2, "gpio_get_level").ignoreOtherParameters();
mock("2").expectNCalls(2, "gpio_get_level").ignoreOtherParameters();
mock("15").expectNCalls(2, "gpio_get_level").ignoreOtherParameters();
mock()
.expectNCalls(2, "adc_read")
.withOutputParameterReturning("adc", (const void *)&adc, sizeof(adc));

TestableComparatorEq testable;
testable.SetReference(50 / 0.4);
testable.SetIoAdr(MapIO::AI);
CHECK_TRUE(Controller::SampleIOValues());
CHECK_TRUE(testable.DoAction(false, LogicItemState::lisActive));
CHECK_EQUAL(LogicItemState::lisActive, *testable.PublicMorozov_Get_state());

adc = 49 / 0.1;
CHECK_TRUE(Controller::SampleIOValues());
CHECK_TRUE(testable.DoAction(false, LogicItemState::lisActive));
CHECK_EQUAL(LogicItemState::lisPassive, *testable.PublicMorozov_Get_state());
}
Expand Down
15 changes: 14 additions & 1 deletion Tests_esp8266/src/logic_ComparatorGE_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ static uint8_t frame_buffer[DISPLAY_WIDTH * DISPLAY_HEIGHT / 8] = {};
TEST_GROUP(LogicComparatorGETestsGroup){
//
TEST_SETUP(){ memset(frame_buffer, 0, sizeof(frame_buffer));
mock().expectOneCall("xEventGroupWaitBits").ignoreOtherParameters();
mock().expectOneCall("vEventGroupDelete").ignoreOtherParameters();
Controller::Stop();
}

TEST_TEARDOWN() {
Expand Down Expand Up @@ -72,39 +75,49 @@ TEST(LogicComparatorGETestsGroup, DoAction_skip_when_incoming_passive) {

TEST(LogicComparatorGETestsGroup, DoAction_change_state_to_active) {
volatile uint16_t adc = 49 / 0.1;
mock("0").expectNCalls(3, "gpio_get_level").ignoreOtherParameters();
mock("2").expectNCalls(3, "gpio_get_level").ignoreOtherParameters();
mock("15").expectNCalls(3, "gpio_get_level").ignoreOtherParameters();
mock()
.expectNCalls(3, "adc_read")
.withOutputParameterReturning("adc", (const void *)&adc, sizeof(adc));

TestableComparatorGE testable;
testable.SetReference(50 / 0.4);
testable.SetIoAdr(MapIO::AI);

CHECK_TRUE(Controller::SampleIOValues());
CHECK_FALSE(testable.DoAction(false, LogicItemState::lisActive));
CHECK_EQUAL(LogicItemState::lisPassive, *testable.PublicMorozov_Get_state());

adc = 50 / 0.1;
CHECK_TRUE(Controller::SampleIOValues());
CHECK_TRUE(testable.DoAction(false, LogicItemState::lisActive));
CHECK_EQUAL(LogicItemState::lisActive, *testable.PublicMorozov_Get_state());

adc = 51 / 0.1;
CHECK_TRUE(Controller::SampleIOValues());
CHECK_FALSE(testable.DoAction(false, LogicItemState::lisActive));
CHECK_EQUAL(LogicItemState::lisActive, *testable.PublicMorozov_Get_state());
}

TEST(LogicComparatorGETestsGroup, DoAction_change_state_to_passive) {
volatile uint16_t adc = 50 / 0.1;
mock("0").expectNCalls(2, "gpio_get_level").ignoreOtherParameters();
mock("2").expectNCalls(2, "gpio_get_level").ignoreOtherParameters();
mock("15").expectNCalls(2, "gpio_get_level").ignoreOtherParameters();
mock()
.expectNCalls(2, "adc_read")
.withOutputParameterReturning("adc", (const void *)&adc, sizeof(adc));

TestableComparatorGE testable;
testable.SetReference(50 / 0.4);
testable.SetIoAdr(MapIO::AI);
CHECK_TRUE(Controller::SampleIOValues());
CHECK_TRUE(testable.DoAction(false, LogicItemState::lisActive));
CHECK_EQUAL(LogicItemState::lisActive, *testable.PublicMorozov_Get_state());

adc = 49 / 0.1;
CHECK_TRUE(Controller::SampleIOValues());
CHECK_TRUE(testable.DoAction(false, LogicItemState::lisActive));
CHECK_EQUAL(LogicItemState::lisPassive, *testable.PublicMorozov_Get_state());
}
Expand Down
Loading

0 comments on commit f7fa9b9

Please sign in to comment.