Skip to content

Commit

Permalink
fix gpio initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
rednblkx committed Sep 6, 2024
1 parent 953e013 commit 521001a
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ SpanCharacteristic* lockCurrentState;
SpanCharacteristic* lockTargetState;
esp_mqtt_client_handle_t client = nullptr;

Pixel *pixel;
std::shared_ptr<Pixel> pixel;

bool save_to_nvs() {
std::vector<uint8_t> serialized = nlohmann::json::to_msgpack(readerData);
Expand Down Expand Up @@ -173,7 +173,7 @@ void gpio_task(void* arg) {
LOG(D, "Got something in queue %d", status);
if (status == 1) {
if (espConfig::miscConfig.gpioActionMomentaryEnabled) {
LOG(I, "Momentary Enabled");
LOG(D, "%d - %d - %d -%d", espConfig::miscConfig.gpioActionPin, espConfig::miscConfig.gpioActionMomentaryEnabled, espConfig::miscConfig.lockAlwaysUnlock, espConfig::miscConfig.lockAlwaysLock);
if (espConfig::miscConfig.lockAlwaysUnlock) {
lockTargetState->setVal(lockStates::UNLOCKED);
digitalWrite(espConfig::miscConfig.gpioActionPin, espConfig::miscConfig.gpioActionUnlockState);
Expand Down Expand Up @@ -207,7 +207,7 @@ void gpio_task(void* arg) {
}
}
}
vTaskDelay(50 / portTICK_PERIOD_MS);
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}

Expand All @@ -222,13 +222,15 @@ void neopixel_task(void* arg) {
switch (status) {
case 0:
if (espConfig::miscConfig.nfcNeopixelPin && espConfig::miscConfig.nfcNeopixelPin != 255) {
LOG(D, "SUCCESS PIXEL %d:%d,%d,%d", espConfig::miscConfig.nfcNeopixelPin, espConfig::miscConfig.neopixelFailureColor[espConfig::misc_config_t::colorMap::R], espConfig::miscConfig.neopixelFailureColor[espConfig::misc_config_t::colorMap::G], espConfig::miscConfig.neopixelFailureColor[espConfig::misc_config_t::colorMap::B]);
pixel->set(pixel->RGB(espConfig::miscConfig.neopixelFailureColor[espConfig::misc_config_t::colorMap::R], espConfig::miscConfig.neopixelFailureColor[espConfig::misc_config_t::colorMap::G], espConfig::miscConfig.neopixelFailureColor[espConfig::misc_config_t::colorMap::B]));
delay(espConfig::miscConfig.neopixelFailTime);
pixel->off();
}
break;
case 1:
if (espConfig::miscConfig.nfcNeopixelPin && espConfig::miscConfig.nfcNeopixelPin != 255) {
LOG(D, "FAIL PIXEL %d:%d,%d,%d", espConfig::miscConfig.nfcNeopixelPin, espConfig::miscConfig.neopixelSuccessColor[espConfig::misc_config_t::colorMap::R], espConfig::miscConfig.neopixelSuccessColor[espConfig::misc_config_t::colorMap::G], espConfig::miscConfig.neopixelSuccessColor[espConfig::misc_config_t::colorMap::B]);
pixel->set(pixel->RGB(espConfig::miscConfig.neopixelSuccessColor[espConfig::misc_config_t::colorMap::R], espConfig::miscConfig.neopixelSuccessColor[espConfig::misc_config_t::colorMap::G], espConfig::miscConfig.neopixelSuccessColor[espConfig::misc_config_t::colorMap::B]));
delay(espConfig::miscConfig.neopixelSuccessTime);
pixel->off();
Expand All @@ -240,39 +242,42 @@ void neopixel_task(void* arg) {
}
}
}
vTaskDelay(50 / portTICK_PERIOD_MS);
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}
void nfc_gpio_task(void* arg) {
bool status = false;
uint8_t status = 0;
while (1) {
if (gpio_led_handle != nullptr) {
status = false;
status = 0;
if (uxQueueMessagesWaiting(gpio_led_handle) > 0) {
xQueueReceive(gpio_led_handle, &status, 0);
LOG(D, "Got something in queue %d", status);
switch (status) {
case 0:
if (espConfig::miscConfig.nfcFailPin && espConfig::miscConfig.nfcFailPin != 255) {
LOG(D, "FAIL LED %d:%d", espConfig::miscConfig.nfcFailPin, espConfig::miscConfig.nfcFailHL);
digitalWrite(espConfig::miscConfig.nfcFailPin, espConfig::miscConfig.nfcFailHL);
delay(espConfig::miscConfig.nfcFailTime);
digitalWrite(espConfig::miscConfig.nfcFailPin, !espConfig::miscConfig.nfcFailHL);
}
break;
case 1:
if (espConfig::miscConfig.nfcSuccessPin && espConfig::miscConfig.nfcSuccessPin != 255) {
LOG(D, "SUCCESS LED %d:%d", espConfig::miscConfig.nfcSuccessPin, espConfig::miscConfig.nfcSuccessHL);
digitalWrite(espConfig::miscConfig.nfcSuccessPin, espConfig::miscConfig.nfcSuccessHL);
delay(espConfig::miscConfig.nfcSuccessTime);
digitalWrite(espConfig::miscConfig.nfcSuccessPin, !espConfig::miscConfig.nfcSuccessHL);
}
break;
default:
LOG(I, "STOP");
return;
break;
}
}
}
vTaskDelay(50 / portTICK_PERIOD_MS);
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}

Expand Down Expand Up @@ -1156,6 +1161,7 @@ void setupWeb() {
if (!strcmp(p->name().c_str(), "nfc-neopixel-pin")) {
if (espConfig::miscConfig.nfcNeopixelPin == 255 && p->value().toInt() != 255) {
xTaskCreate(neopixel_task, "neopixel_task", 4096, NULL, 2, neopixel_task_handle);
pixel = std::make_shared<Pixel>(p->value().toInt(), PixelType::GRB);
}
else if (espConfig::miscConfig.nfcNeopixelPin != 255 && p->value().toInt() == 255 && neopixel_task_handle != nullptr) {
uint8_t status = 2;
Expand Down Expand Up @@ -1189,7 +1195,8 @@ void setupWeb() {
espConfig::miscConfig.neopixelFailureColor[espConfig::misc_config_t::colorMap::B] = p->value().toInt();
}
else if (!strcmp(p->name().c_str(), "nfc-s-pin")) {
if (espConfig::miscConfig.nfcSuccessPin == 255 && p->value().toInt() != 255 && gpio_led_task_handle == nullptr) {
if (espConfig::miscConfig.nfcSuccessPin == 255 && p->value().toInt() != 255 && gpio_led_task_handle == nullptr) {
pinMode(p->value().toInt(), OUTPUT);
xTaskCreate(nfc_gpio_task, "nfc_gpio_task", 4096, NULL, 2, gpio_led_task_handle);
}
else if (espConfig::miscConfig.nfcSuccessPin != 255 && p->value().toInt() == 255 && gpio_led_task_handle != nullptr) {
Expand All @@ -1200,10 +1207,11 @@ void setupWeb() {
espConfig::miscConfig.nfcSuccessPin = p->value().toInt();
}
else if (!strcmp(p->name().c_str(), "nfc-f-pin")) {
if (espConfig::miscConfig.nfcSuccessPin == 255 && p->value().toInt() != 255 && gpio_led_task_handle == nullptr) {
if (espConfig::miscConfig.nfcFailPin == 255 && p->value().toInt() != 255 && gpio_led_task_handle == nullptr) {
pinMode(p->value().toInt(), OUTPUT);
xTaskCreate(nfc_gpio_task, "nfc_gpio_task", 4096, NULL, 2, gpio_led_task_handle);
}
else if (espConfig::miscConfig.nfcSuccessPin != 255 && p->value().toInt() == 255 && gpio_led_task_handle != nullptr) {
else if (espConfig::miscConfig.nfcFailPin != 255 && p->value().toInt() == 255 && gpio_led_task_handle != nullptr) {
uint8_t status = 2;
xQueueSend(gpio_led_handle, &status, 0);
vTaskDelete(gpio_led_task_handle);
Expand All @@ -1224,6 +1232,7 @@ void setupWeb() {
}
else if (!strcmp(p->name().c_str(), "gpio-a-pin")) {
if (espConfig::miscConfig.gpioActionPin == 255 && p->value().toInt() != 255) {
pinMode(p->value().toInt(), OUTPUT);
xTaskCreate(gpio_task, "gpio_task", 4096, NULL, 2, gpio_lock_task_handle);
}
else if (espConfig::miscConfig.gpioActionPin != 255 && p->value().toInt() == 255 && gpio_lock_task_handle != nullptr) {
Expand Down Expand Up @@ -1627,11 +1636,16 @@ void setup() {
new Characteristic::Version();
homeSpan.setControllerCallback(pairCallback);
homeSpan.setWifiCallback(wifiCallback);

pixel = new Pixel(espConfig::miscConfig.nfcNeopixelPin, PixelType::GRB);
xTaskCreate(neopixel_task, "neopixel_task", 4096, NULL, 2, neopixel_task_handle);
xTaskCreate(nfc_gpio_task, "nfc_gpio_task", 4096, NULL, 2, gpio_led_task_handle);
xTaskCreate(gpio_task, "gpio_task", 4096, NULL, 2, gpio_lock_task_handle);
if (espConfig::miscConfig.nfcNeopixelPin != 255) {
pixel = std::make_shared<Pixel>(espConfig::miscConfig.nfcNeopixelPin, PixelType::GRB);
xTaskCreate(neopixel_task, "neopixel_task", 4096, NULL, 2, neopixel_task_handle);
}
if (espConfig::miscConfig.nfcSuccessPin != 255 || espConfig::miscConfig.nfcFailPin != 255) {
xTaskCreate(nfc_gpio_task, "nfc_gpio_task", 4096, NULL, 2, gpio_led_task_handle);
}
if (espConfig::miscConfig.gpioActionPin != 255) {
xTaskCreate(gpio_task, "gpio_task", 4096, NULL, 2, gpio_lock_task_handle);
}
xTaskCreate(nfc_thread_entry, "nfc_task", 8192, NULL, 1, NULL);
}

Expand Down

0 comments on commit 521001a

Please sign in to comment.