server_GPIO.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. #include <string>
  2. #include <string.h>
  3. #include <functional>
  4. #include <freertos/FreeRTOS.h>
  5. #include <freertos/task.h>
  6. #include <esp_system.h>
  7. #include <esp_log.h>
  8. #include <sys/stat.h>
  9. #include <vector>
  10. #include "defines.h"
  11. #include "Helper.h"
  12. #include "server_GPIO.h"
  13. #include "ClassFlow.h"
  14. #include "ClassLogFile.h"
  15. #include "interface_mqtt.h"
  16. #include "server_mqtt.h"
  17. #include "basic_auth.h"
  18. static const char *TAG = "GPIO";
  19. QueueHandle_t gpio_queue_handle = NULL;
  20. GpioPin::GpioPin(gpio_num_t gpio, const char *name, gpio_pin_mode_t mode, gpio_int_type_t interruptType, uint8_t dutyResolution, std::string mqttTopic, bool httpEnable)
  21. {
  22. _gpio = gpio;
  23. _name = name;
  24. _mode = mode;
  25. _interruptType = interruptType;
  26. _mqttTopic = mqttTopic;
  27. }
  28. GpioPin::~GpioPin()
  29. {
  30. ESP_LOGD(TAG, "reset GPIO pin %d", _gpio);
  31. if (_interruptType != GPIO_INTR_DISABLE)
  32. {
  33. // hook isr handler for specific gpio pin
  34. gpio_isr_handler_remove(_gpio);
  35. }
  36. gpio_reset_pin(_gpio);
  37. }
  38. static void IRAM_ATTR gpio_isr_handler(void *arg)
  39. {
  40. GpioResult gpioResult;
  41. gpioResult.gpio = *(gpio_num_t *)arg;
  42. gpioResult.value = gpio_get_level(gpioResult.gpio);
  43. BaseType_t ContextSwitchRequest = pdFALSE;
  44. xQueueSendToBackFromISR(gpio_queue_handle, (void *)&gpioResult, &ContextSwitchRequest);
  45. if (ContextSwitchRequest)
  46. {
  47. taskYIELD();
  48. }
  49. }
  50. static void gpioHandlerTask(void *arg)
  51. {
  52. ESP_LOGD(TAG, "start interrupt task");
  53. while (1)
  54. {
  55. if (uxQueueMessagesWaiting(gpio_queue_handle))
  56. {
  57. while (uxQueueMessagesWaiting(gpio_queue_handle))
  58. {
  59. GpioResult gpioResult;
  60. xQueueReceive(gpio_queue_handle, (void *)&gpioResult, 10);
  61. ESP_LOGD(TAG, "gpio: %d state: %d", gpioResult.gpio, gpioResult.value);
  62. ((GpioHandler *)arg)->gpioInterrupt(&gpioResult);
  63. }
  64. }
  65. ((GpioHandler *)arg)->taskHandler();
  66. vTaskDelay(pdMS_TO_TICKS(1000));
  67. }
  68. }
  69. void GpioPin::gpioInterrupt(int value)
  70. {
  71. if (_mqttTopic.compare("") != 0)
  72. {
  73. ESP_LOGD(TAG, "gpioInterrupt %s %d", _mqttTopic.c_str(), value);
  74. MQTTPublish(_mqttTopic, value ? "true" : "false", 1);
  75. }
  76. currentState = value;
  77. }
  78. void GpioPin::init()
  79. {
  80. gpio_config_t io_conf;
  81. // set interrupt
  82. io_conf.intr_type = _interruptType;
  83. // set as output mode
  84. io_conf.mode = (_mode == GPIO_PIN_MODE_OUTPUT) || (_mode == GPIO_PIN_MODE_BUILTIN_FLASH) ? gpio_mode_t::GPIO_MODE_OUTPUT : gpio_mode_t::GPIO_MODE_INPUT;
  85. // bit mask of the pins that you want to set,e.g.GPIO18/19
  86. io_conf.pin_bit_mask = (1ULL << _gpio);
  87. // set pull-down mode
  88. io_conf.pull_down_en = _mode == GPIO_PIN_MODE_INPUT_PULLDOWN ? gpio_pulldown_t::GPIO_PULLDOWN_ENABLE : gpio_pulldown_t::GPIO_PULLDOWN_DISABLE;
  89. // set pull-up mode
  90. io_conf.pull_up_en = _mode == GPIO_PIN_MODE_INPUT_PULLDOWN ? gpio_pullup_t::GPIO_PULLUP_ENABLE : gpio_pullup_t::GPIO_PULLUP_DISABLE;
  91. // configure GPIO with the given settings
  92. gpio_config(&io_conf);
  93. if ((_interruptType != GPIO_INTR_DISABLE) && (_mode != GPIO_PIN_MODE_OUTPUT_WS281X))
  94. {
  95. // hook isr handler for specific gpio pin
  96. ESP_LOGD(TAG, "GpioPin::init add isr handler for GPIO %d", _gpio);
  97. gpio_isr_handler_add(_gpio, gpio_isr_handler, (void *)&_gpio);
  98. }
  99. if ((_mqttTopic.compare("") != 0) && ((_mode == GPIO_PIN_MODE_OUTPUT) || (_mode == GPIO_PIN_MODE_BUILTIN_FLASH_PWM) || (_mode == GPIO_PIN_MODE_BUILTIN_FLASH)))
  100. {
  101. std::function<bool(std::string, char *, int)> f = std::bind(&GpioPin::handleMQTT, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
  102. MQTTregisterSubscribeFunction(_mqttTopic, f);
  103. }
  104. }
  105. bool GpioPin::getValue(std::string *errorText)
  106. {
  107. if ((_mode != GPIO_PIN_MODE_INPUT) && (_mode != GPIO_PIN_MODE_INPUT_PULLUP) && (_mode != GPIO_PIN_MODE_INPUT_PULLDOWN))
  108. {
  109. (*errorText) = "GPIO is not in input mode";
  110. }
  111. return gpio_get_level(_gpio) == 1;
  112. }
  113. void GpioPin::setValue(bool value, gpio_set_source setSource, std::string *errorText)
  114. {
  115. ESP_LOGD(TAG, "GpioPin::setValue %d", value);
  116. if ((_mode != GPIO_PIN_MODE_OUTPUT) && (_mode != GPIO_PIN_MODE_BUILTIN_FLASH_PWM) && (_mode != GPIO_PIN_MODE_BUILTIN_FLASH))
  117. {
  118. (*errorText) = "GPIO is not in output mode";
  119. }
  120. else
  121. {
  122. gpio_set_level(_gpio, value);
  123. if ((_mqttTopic.compare("") != 0) && (setSource != GPIO_SET_SOURCE_MQTT))
  124. {
  125. MQTTPublish(_mqttTopic, value ? "true" : "false", 1);
  126. }
  127. }
  128. }
  129. void GpioPin::publishState()
  130. {
  131. int newState = gpio_get_level(_gpio);
  132. if (newState != currentState)
  133. {
  134. ESP_LOGD(TAG, "publish state of GPIO %d new state %d", _gpio, newState);
  135. if (_mqttTopic.compare("") != 0)
  136. {
  137. MQTTPublish(_mqttTopic, newState ? "true" : "false", 1);
  138. }
  139. currentState = newState;
  140. }
  141. }
  142. bool GpioPin::handleMQTT(std::string, char *data, int data_len)
  143. {
  144. ESP_LOGD(TAG, "GpioPin::handleMQTT data %.*s", data_len, data);
  145. std::string dataStr(data, data_len);
  146. dataStr = to_lower(dataStr);
  147. std::string errorText = "";
  148. if ((dataStr == "true") || (dataStr == "1"))
  149. {
  150. setValue(true, GPIO_SET_SOURCE_MQTT, &errorText);
  151. }
  152. else if ((dataStr == "false") || (dataStr == "0"))
  153. {
  154. setValue(false, GPIO_SET_SOURCE_MQTT, &errorText);
  155. }
  156. else
  157. {
  158. errorText = "wrong value ";
  159. errorText.append(data, data_len);
  160. }
  161. if (errorText != "")
  162. {
  163. ESP_LOGE(TAG, "%s", errorText.c_str());
  164. }
  165. return (errorText == "");
  166. }
  167. esp_err_t callHandleHttpRequest(httpd_req_t *req)
  168. {
  169. ESP_LOGD(TAG, "callHandleHttpRequest");
  170. GpioHandler *gpioHandler = (GpioHandler *)req->user_ctx;
  171. return gpioHandler->handleHttpRequest(req);
  172. }
  173. void taskGpioHandler(void *pvParameter)
  174. {
  175. ESP_LOGD(TAG, "taskGpioHandler");
  176. ((GpioHandler *)pvParameter)->init();
  177. }
  178. GpioHandler::GpioHandler(std::string configFile, httpd_handle_t httpServer)
  179. {
  180. ESP_LOGI(TAG, "start GpioHandler");
  181. _httpServer = httpServer;
  182. ESP_LOGI(TAG, "register GPIO Uri");
  183. registerGpioUri();
  184. }
  185. GpioHandler::~GpioHandler()
  186. {
  187. if (gpioMap != NULL)
  188. {
  189. clear();
  190. delete gpioMap;
  191. }
  192. }
  193. void GpioHandler::init()
  194. {
  195. ESP_LOGD(TAG, "*************** Start GPIOHandler_Init *****************");
  196. if (gpioMap == NULL)
  197. {
  198. gpioMap = new std::map<gpio_num_t, GpioPin *>();
  199. }
  200. else
  201. {
  202. clear();
  203. }
  204. ESP_LOGI(TAG, "read GPIO config and init GPIO");
  205. if (!readConfig())
  206. {
  207. clear();
  208. delete gpioMap;
  209. gpioMap = NULL;
  210. ESP_LOGI(TAG, "GPIO init completed, handler is disabled");
  211. return;
  212. }
  213. for (std::map<gpio_num_t, GpioPin *>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it)
  214. {
  215. it->second->init();
  216. }
  217. std::function<void()> f = std::bind(&GpioHandler::handleMQTTconnect, this);
  218. MQTTregisterConnectFunction("gpio-handler", f);
  219. if (xHandleTaskGpio == NULL)
  220. {
  221. gpio_queue_handle = xQueueCreate(10, sizeof(GpioResult));
  222. BaseType_t xReturned = xTaskCreate(&gpioHandlerTask, "gpio_int", 3 * 1024, (void *)this, tskIDLE_PRIORITY + 4, &xHandleTaskGpio);
  223. if (xReturned == pdPASS)
  224. {
  225. ESP_LOGD(TAG, "xHandletaskGpioHandler started");
  226. }
  227. else
  228. {
  229. ESP_LOGD(TAG, "xHandletaskGpioHandler not started %d ", (int)xHandleTaskGpio);
  230. }
  231. }
  232. ESP_LOGI(TAG, "GPIO init completed, is enabled");
  233. }
  234. void GpioHandler::taskHandler()
  235. {
  236. if (gpioMap != NULL)
  237. {
  238. for (std::map<gpio_num_t, GpioPin *>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it)
  239. {
  240. if ((it->second->getInterruptType() == GPIO_INTR_DISABLE))
  241. it->second->publishState();
  242. }
  243. }
  244. }
  245. void GpioHandler::handleMQTTconnect()
  246. {
  247. if (gpioMap != NULL)
  248. {
  249. for (std::map<gpio_num_t, GpioPin *>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it)
  250. {
  251. if ((it->second->getMode() == GPIO_PIN_MODE_INPUT) || (it->second->getMode() == GPIO_PIN_MODE_INPUT_PULLDOWN) || (it->second->getMode() == GPIO_PIN_MODE_INPUT_PULLUP))
  252. it->second->publishState();
  253. }
  254. }
  255. }
  256. void GpioHandler::deinit()
  257. {
  258. MQTTunregisterConnectFunction("gpio-handler");
  259. clear();
  260. if (xHandleTaskGpio != NULL)
  261. {
  262. vTaskDelete(xHandleTaskGpio);
  263. xHandleTaskGpio = NULL;
  264. }
  265. }
  266. void GpioHandler::gpioInterrupt(GpioResult *gpioResult)
  267. {
  268. if ((gpioMap != NULL) && (gpioMap->find(gpioResult->gpio) != gpioMap->end()))
  269. {
  270. (*gpioMap)[gpioResult->gpio]->gpioInterrupt(gpioResult->value);
  271. }
  272. }
  273. bool GpioHandler::readConfig()
  274. {
  275. if (!gpioMap->empty())
  276. {
  277. clear();
  278. }
  279. FILE *pFile = fopen(CONFIG_FILE, "r");
  280. if (pFile == NULL)
  281. {
  282. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "No ConfigFile defined - exit GpioHandler::readConfig()!");
  283. return false;
  284. }
  285. ClassFlow classFlow;
  286. std::string aktparamgraph = "";
  287. while (classFlow.GetNextParagraph(pFile, aktparamgraph))
  288. {
  289. if ((to_upper(aktparamgraph).compare("[GPIO]") == 0) || (to_upper(aktparamgraph).compare(";[GPIO]") == 0))
  290. {
  291. break;
  292. }
  293. }
  294. if ((to_upper(aktparamgraph).compare("[GPIO]") != 0) || (to_upper(aktparamgraph).compare(";[GPIO]") == 0))
  295. {
  296. _isEnabled = false;
  297. fclose(pFile);
  298. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "GpioHandler disabled.");
  299. return false;
  300. }
  301. else
  302. {
  303. _isEnabled = true;
  304. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "GpioHandler enabled.");
  305. }
  306. std::string mainTopicMQTT = mqttServer_getMainTopic();
  307. if (mainTopicMQTT.length() > 0)
  308. {
  309. mainTopicMQTT = mainTopicMQTT + "/GPIO";
  310. ESP_LOGD(TAG, "MAINTOPICMQTT found");
  311. }
  312. bool registerISR = false;
  313. gpio_num_t gpioExtLED = GPIO_NUM_NC;
  314. std::vector<std::string> splitted;
  315. while (classFlow.getNextLine(pFile, &aktparamgraph) && !classFlow.isNewParagraph(aktparamgraph))
  316. {
  317. splitted = split_line(aktparamgraph);
  318. if (splitted.size() > 1)
  319. {
  320. std::string _param = to_upper(splitted[0]);
  321. if (_param == "MAINTOPICMQTT")
  322. {
  323. }
  324. else if ((_param.rfind("IO", 0) == 0) && (splitted.size() >= 6))
  325. {
  326. ESP_LOGI(TAG, "Enable GP%s in %s mode", splitted[0].c_str(), splitted[1].c_str());
  327. std::string gpioStr = splitted[0].substr(2, 2);
  328. gpio_num_t gpioNr = (gpio_num_t)atoi(gpioStr.c_str());
  329. gpio_pin_mode_t pinMode = resolvePinMode(to_lower(splitted[1]));
  330. gpio_int_type_t intType = resolveIntType(to_lower(splitted[2]));
  331. uint16_t dutyResolution = (uint8_t)atoi(splitted[3].c_str());
  332. bool mqttEnabled = (to_lower(splitted[4]) == "true");
  333. bool httpEnabled = (to_lower(splitted[5]) == "true");
  334. char gpioName[100];
  335. if (splitted.size() >= 7)
  336. {
  337. strcpy(gpioName, trim_string_left_right(splitted[6]).c_str());
  338. }
  339. else
  340. {
  341. sprintf(gpioName, "GPIO%d", gpioNr);
  342. }
  343. std::string mqttTopic = mqttEnabled ? (mainTopicMQTT + "/" + gpioName) : "";
  344. GpioPin *gpioPin = new GpioPin(gpioNr, gpioName, pinMode, intType, dutyResolution, mqttTopic, httpEnabled);
  345. (*gpioMap)[gpioNr] = gpioPin;
  346. if (pinMode == GPIO_PIN_MODE_OUTPUT_WS281X)
  347. {
  348. ESP_LOGD(TAG, "Set WS2812 to GPIO %d", gpioNr);
  349. gpioExtLED = gpioNr;
  350. }
  351. if (intType != GPIO_INTR_DISABLE)
  352. {
  353. registerISR = true;
  354. }
  355. }
  356. else if (_param == "LEDNUMBERS")
  357. {
  358. LEDNumbers = stoi(splitted[1]);
  359. }
  360. else if (_param == "LEDCOLOR")
  361. {
  362. uint8_t _r, _g, _b;
  363. _r = stoi(splitted[1]);
  364. _g = stoi(splitted[2]);
  365. _b = stoi(splitted[3]);
  366. LEDColor = Rgb{_r, _g, _b};
  367. }
  368. else if (_param == "LEDTYPE")
  369. {
  370. if (splitted[1] == "WS2812")
  371. {
  372. LEDType = LED_WS2812;
  373. }
  374. else if (splitted[1] == "WS2812B")
  375. {
  376. LEDType = LED_WS2812B;
  377. }
  378. else if (splitted[1] == "SK6812")
  379. {
  380. LEDType = LED_SK6812;
  381. }
  382. else if (splitted[1] == "WS2813")
  383. {
  384. LEDType = LED_WS2813;
  385. }
  386. }
  387. }
  388. }
  389. fclose(pFile);
  390. if (registerISR)
  391. {
  392. // install gpio isr service
  393. gpio_install_isr_service(ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM);
  394. }
  395. if (gpioExtLED > 0)
  396. {
  397. }
  398. return true;
  399. }
  400. void GpioHandler::clear()
  401. {
  402. ESP_LOGD(TAG, "GpioHandler::clear");
  403. if (gpioMap != NULL)
  404. {
  405. for (std::map<gpio_num_t, GpioPin *>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it)
  406. {
  407. delete it->second;
  408. }
  409. gpioMap->clear();
  410. }
  411. }
  412. void GpioHandler::registerGpioUri()
  413. {
  414. ESP_LOGI(TAG, "server_GPIO - Registering URI handlers");
  415. httpd_uri_t camuri = {};
  416. camuri.method = HTTP_GET;
  417. camuri.uri = "/GPIO";
  418. camuri.handler = APPLY_BASIC_AUTH_FILTER(callHandleHttpRequest);
  419. camuri.user_ctx = (void *)this;
  420. httpd_register_uri_handler(_httpServer, &camuri);
  421. }
  422. esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
  423. {
  424. ESP_LOGD(TAG, "handleHttpRequest()");
  425. if (gpioMap == NULL)
  426. {
  427. std::string resp_str = "GPIO handler not initialized";
  428. httpd_resp_send(req, resp_str.c_str(), resp_str.length());
  429. return ESP_OK;
  430. }
  431. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "handler_switch_GPIO");
  432. char _query[200];
  433. char _valueGPIO[30];
  434. char _valueStatus[30];
  435. std::string gpio, status;
  436. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  437. {
  438. ESP_LOGD(TAG, "Query: %s", _query);
  439. if (httpd_query_key_value(_query, "GPIO", _valueGPIO, 30) == ESP_OK)
  440. {
  441. ESP_LOGD(TAG, "GPIO is found %s", _valueGPIO);
  442. gpio = std::string(_valueGPIO);
  443. }
  444. else
  445. {
  446. std::string resp_str = "GPIO No is not defined";
  447. httpd_resp_send(req, resp_str.c_str(), resp_str.length());
  448. return ESP_OK;
  449. }
  450. if (httpd_query_key_value(_query, "Status", _valueStatus, 30) == ESP_OK)
  451. {
  452. ESP_LOGD(TAG, "Status is found %s", _valueStatus);
  453. status = std::string(_valueStatus);
  454. }
  455. }
  456. else
  457. {
  458. const char *resp_str = "Error in call. Use /GPIO?GPIO=12&Status=high";
  459. httpd_resp_send(req, resp_str, strlen(resp_str));
  460. return ESP_OK;
  461. }
  462. status = to_upper(status);
  463. if ((status != "HIGH") && (status != "LOW") && (status != "TRUE") && (status != "FALSE") && (status != "0") && (status != "1") && (status != ""))
  464. {
  465. std::string temp_string = "Status not valid: " + status;
  466. httpd_resp_sendstr_chunk(req, temp_string.c_str());
  467. httpd_resp_sendstr_chunk(req, NULL);
  468. return ESP_OK;
  469. }
  470. int gpionum = stoi(gpio);
  471. // frei: 16; 12-15; 2; 4 // nur 12 und 13 funktionieren 2: reboot, 4: BlitzLED, 15: PSRAM, 14/15: DMA für SDKarte ???
  472. gpio_num_t gpio_num = resolvePinNr(gpionum);
  473. if (gpio_num == GPIO_NUM_NC)
  474. {
  475. std::string temp_string = "GPIO" + std::to_string(gpionum) + " unsupported - only 12 & 13 free";
  476. httpd_resp_sendstr_chunk(req, temp_string.c_str());
  477. httpd_resp_sendstr_chunk(req, NULL);
  478. return ESP_OK;
  479. }
  480. if (gpioMap->count(gpio_num) == 0)
  481. {
  482. char resp_str[30];
  483. sprintf(resp_str, "GPIO%d is not registred", gpio_num);
  484. httpd_resp_send(req, resp_str, strlen(resp_str));
  485. return ESP_OK;
  486. }
  487. if (status == "")
  488. {
  489. std::string resp_str = "";
  490. status = (*gpioMap)[gpio_num]->getValue(&resp_str) ? "HIGH" : "LOW";
  491. if (resp_str == "")
  492. {
  493. resp_str = status;
  494. }
  495. httpd_resp_sendstr_chunk(req, resp_str.c_str());
  496. httpd_resp_sendstr_chunk(req, NULL);
  497. }
  498. else
  499. {
  500. std::string resp_str = "";
  501. (*gpioMap)[gpio_num]->setValue((status == "HIGH") || (status == "TRUE") || (status == "1"), GPIO_SET_SOURCE_HTTP, &resp_str);
  502. if (resp_str == "")
  503. {
  504. resp_str = "GPIO" + std::to_string(gpionum) + " switched to " + status;
  505. }
  506. httpd_resp_sendstr_chunk(req, resp_str.c_str());
  507. httpd_resp_sendstr_chunk(req, NULL);
  508. }
  509. return ESP_OK;
  510. };
  511. void GpioHandler::flashLightEnable(bool value)
  512. {
  513. ESP_LOGD(TAG, "GpioHandler::flashLightEnable %s", value ? "true" : "false");
  514. if (gpioMap != NULL)
  515. {
  516. for (std::map<gpio_num_t, GpioPin *>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it)
  517. {
  518. if (it->second->getMode() == GPIO_PIN_MODE_BUILTIN_FLASH)
  519. {
  520. std::string resp_str = "";
  521. it->second->setValue(value, GPIO_SET_SOURCE_INTERNAL, &resp_str);
  522. if (resp_str == "")
  523. {
  524. ESP_LOGD(TAG, "Flash light pin GPIO %d switched to %s", (int)it->first, (value ? "on" : "off"));
  525. }
  526. else
  527. {
  528. ESP_LOGE(TAG, "Can't set flash light pin GPIO %d. Error: %s", (int)it->first, resp_str.c_str());
  529. }
  530. }
  531. else
  532. {
  533. if (it->second->getMode() == GPIO_PIN_MODE_OUTPUT_WS281X)
  534. {
  535. #ifdef __LEDGLOBAL
  536. if (leds_global == NULL)
  537. {
  538. ESP_LOGI(TAG, "init SmartLed: LEDNumber=%d, GPIO=%d", LEDNumbers, (int)it->second->getGPIO());
  539. leds_global = new SmartLed(LEDType, LEDNumbers, it->second->getGPIO(), 0, DoubleBuffer);
  540. }
  541. else
  542. {
  543. // wait until we can update: https://github.com/RoboticsBrno/SmartLeds/issues/10#issuecomment-386921623
  544. leds_global->wait();
  545. }
  546. #else
  547. SmartLed leds(LEDType, LEDNumbers, it->second->getGPIO(), 0, DoubleBuffer);
  548. #endif
  549. if (value)
  550. {
  551. for (int i = 0; i < LEDNumbers; ++i)
  552. #ifdef __LEDGLOBAL
  553. (*leds_global)[i] = LEDColor;
  554. #else
  555. leds[i] = LEDColor;
  556. #endif
  557. }
  558. else
  559. {
  560. for (int i = 0; i < LEDNumbers; ++i)
  561. #ifdef __LEDGLOBAL
  562. (*leds_global)[i] = Rgb{0, 0, 0};
  563. #else
  564. leds[i] = Rgb{0, 0, 0};
  565. #endif
  566. }
  567. #ifdef __LEDGLOBAL
  568. leds_global->show();
  569. #else
  570. leds.show();
  571. #endif
  572. }
  573. }
  574. }
  575. }
  576. }
  577. gpio_num_t GpioHandler::resolvePinNr(uint8_t pinNr)
  578. {
  579. switch (pinNr)
  580. {
  581. case 0:
  582. return GPIO_NUM_0;
  583. case 1:
  584. return GPIO_NUM_1;
  585. case 3:
  586. return GPIO_NUM_3;
  587. case 4:
  588. return GPIO_NUM_4;
  589. case 12:
  590. return GPIO_NUM_12;
  591. case 13:
  592. return GPIO_NUM_13;
  593. default:
  594. return GPIO_NUM_NC;
  595. }
  596. }
  597. /*
  598. gpio_num_t GpioHandler::resolvePinNr(uint8_t pinNr)
  599. {
  600. switch (pinNr)
  601. {
  602. case 1:
  603. return GPIO_IO1;
  604. case 2:
  605. return GPIO_IO2;
  606. case 3:
  607. return GPIO_IO3;
  608. case 4:
  609. return GPIO_IO4;
  610. }
  611. return GPIO_NUM_NC;
  612. }
  613. */
  614. gpio_pin_mode_t GpioHandler::resolvePinMode(std::string input)
  615. {
  616. if (input == "disabled")
  617. {
  618. return GPIO_PIN_MODE_DISABLED;
  619. }
  620. else if (input == "input")
  621. {
  622. return GPIO_PIN_MODE_INPUT;
  623. }
  624. else if (input == "input-pullup")
  625. {
  626. return GPIO_PIN_MODE_INPUT_PULLUP;
  627. }
  628. else if (input == "input-pulldown")
  629. {
  630. return GPIO_PIN_MODE_INPUT_PULLDOWN;
  631. }
  632. else if (input == "output")
  633. {
  634. return GPIO_PIN_MODE_OUTPUT;
  635. }
  636. else if (input == "built-in-led")
  637. {
  638. return GPIO_PIN_MODE_BUILTIN_FLASH;
  639. }
  640. else if (input == "output-pwm")
  641. {
  642. return GPIO_PIN_MODE_BUILTIN_FLASH_PWM;
  643. }
  644. else if (input == "external-flash-pwm")
  645. {
  646. return GPIO_PIN_MODE_OUTPUT_PWM;
  647. }
  648. else if (input == "external-flash-ws281x")
  649. {
  650. return GPIO_PIN_MODE_OUTPUT_WS281X;
  651. }
  652. return GPIO_PIN_MODE_DISABLED;
  653. }
  654. gpio_int_type_t GpioHandler::resolveIntType(std::string input)
  655. {
  656. if (input == "disabled")
  657. {
  658. return GPIO_INTR_DISABLE;
  659. }
  660. if (input == "rising-edge")
  661. {
  662. return GPIO_INTR_POSEDGE;
  663. }
  664. if (input == "falling-edge")
  665. {
  666. return GPIO_INTR_NEGEDGE;
  667. }
  668. if (input == "rising-and-falling")
  669. {
  670. return GPIO_INTR_ANYEDGE;
  671. }
  672. if (input == "low-level-trigger")
  673. {
  674. return GPIO_INTR_LOW_LEVEL;
  675. }
  676. if (input == "high-level-trigger")
  677. {
  678. return GPIO_INTR_HIGH_LEVEL;
  679. }
  680. return GPIO_INTR_DISABLE;
  681. }
  682. static GpioHandler *gpioHandler = NULL;
  683. void gpio_handler_create(httpd_handle_t server)
  684. {
  685. if (gpioHandler == NULL)
  686. {
  687. gpioHandler = new GpioHandler(CONFIG_FILE, server);
  688. }
  689. }
  690. void gpio_handler_init()
  691. {
  692. if (gpioHandler != NULL)
  693. {
  694. gpioHandler->init();
  695. }
  696. }
  697. void gpio_handler_deinit()
  698. {
  699. if (gpioHandler != NULL)
  700. {
  701. gpioHandler->deinit();
  702. }
  703. }
  704. void gpio_handler_destroy()
  705. {
  706. if (gpioHandler != NULL)
  707. {
  708. gpio_handler_deinit();
  709. delete gpioHandler;
  710. gpioHandler = NULL;
  711. }
  712. }
  713. GpioHandler *gpio_handler_get()
  714. {
  715. return gpioHandler;
  716. }