server_GPIO.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. #include <string>
  2. #include <functional>
  3. #include "string.h"
  4. #include <string.h>
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "esp_system.h"
  8. #include "esp_event.h"
  9. //#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  10. #include "esp_log.h"
  11. //#include "errno.h"
  12. #include <sys/stat.h>
  13. #include <vector>
  14. //#include <regex>
  15. #include "defines.h"
  16. #include "server_GPIO.h"
  17. #include "ClassLogFile.h"
  18. #include "configFile.h"
  19. #include "Helper.h"
  20. #include "interface_mqtt.h"
  21. static const char *TAG_SERVERGPIO = "server_GPIO";
  22. QueueHandle_t gpio_queue_handle = NULL;
  23. #define DEBUG_DETAIL_ON
  24. 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)
  25. {
  26. _gpio = gpio;
  27. _name = name;
  28. _mode = mode;
  29. _interruptType = interruptType;
  30. _mqttTopic = mqttTopic;
  31. }
  32. GpioPin::~GpioPin()
  33. {
  34. ESP_LOGD(TAG_SERVERGPIO,"reset GPIO pin %d", _gpio);
  35. if (_interruptType != GPIO_INTR_DISABLE) {
  36. //hook isr handler for specific gpio pin
  37. gpio_isr_handler_remove(_gpio);
  38. }
  39. gpio_reset_pin(_gpio);
  40. }
  41. static void IRAM_ATTR gpio_isr_handler(void* arg)
  42. {
  43. GpioResult gpioResult;
  44. gpioResult.gpio = *(gpio_num_t*) arg;
  45. gpioResult.value = gpio_get_level(gpioResult.gpio);
  46. BaseType_t ContextSwitchRequest = pdFALSE;
  47. xQueueSendToBackFromISR(gpio_queue_handle,(void*)&gpioResult,&ContextSwitchRequest);
  48. if(ContextSwitchRequest){
  49. taskYIELD();
  50. }
  51. }
  52. static void gpioHandlerTask(void *arg) {
  53. ESP_LOGD(TAG_SERVERGPIO,"start interrupt task");
  54. while(1){
  55. if(uxQueueMessagesWaiting(gpio_queue_handle)){
  56. while(uxQueueMessagesWaiting(gpio_queue_handle)){
  57. GpioResult gpioResult;
  58. xQueueReceive(gpio_queue_handle,(void*)&gpioResult,10);
  59. ESP_LOGD(TAG_SERVERGPIO,"gpio: %d state: %d", gpioResult.gpio, gpioResult.value);
  60. ((GpioHandler*)arg)->gpioInterrupt(&gpioResult);
  61. }
  62. }
  63. ((GpioHandler*)arg)->taskHandler();
  64. vTaskDelay(pdMS_TO_TICKS(1000));
  65. }
  66. }
  67. void GpioPin::gpioInterrupt(int value) {
  68. if (_mqttTopic != "") {
  69. ESP_LOGD(TAG_SERVERGPIO, "gpioInterrupt %s %d", _mqttTopic.c_str(), value);
  70. MQTTPublish(_mqttTopic, value ? "true" : "false");
  71. currentState = value;
  72. }
  73. }
  74. void GpioPin::init()
  75. {
  76. gpio_config_t io_conf;
  77. //set interrupt
  78. io_conf.intr_type = _interruptType;
  79. //set as output mode
  80. io_conf.mode = (_mode == GPIO_PIN_MODE_OUTPUT) || (_mode == GPIO_PIN_MODE_BUILT_IN_FLASH_LED) ? gpio_mode_t::GPIO_MODE_OUTPUT : gpio_mode_t::GPIO_MODE_INPUT;
  81. //bit mask of the pins that you want to set,e.g.GPIO18/19
  82. io_conf.pin_bit_mask = (1ULL << _gpio);
  83. //set pull-down mode
  84. io_conf.pull_down_en = _mode == GPIO_PIN_MODE_INPUT_PULLDOWN ? gpio_pulldown_t::GPIO_PULLDOWN_ENABLE : gpio_pulldown_t::GPIO_PULLDOWN_DISABLE;
  85. //set pull-up mode
  86. io_conf.pull_up_en = _mode == GPIO_PIN_MODE_INPUT_PULLDOWN ? gpio_pullup_t::GPIO_PULLUP_ENABLE : gpio_pullup_t::GPIO_PULLUP_DISABLE;
  87. //configure GPIO with the given settings
  88. gpio_config(&io_conf);
  89. if (_interruptType != GPIO_INTR_DISABLE) {
  90. //hook isr handler for specific gpio pin
  91. ESP_LOGD(TAG_SERVERGPIO, "GpioPin::init add isr handler for GPIO %d\r\n", _gpio);
  92. gpio_isr_handler_add(_gpio, gpio_isr_handler, (void*)&_gpio);
  93. }
  94. if ((_mqttTopic != "") && ((_mode == GPIO_PIN_MODE_OUTPUT) || (_mode == GPIO_PIN_MODE_OUTPUT_PWM) || (_mode == GPIO_PIN_MODE_BUILT_IN_FLASH_LED))) {
  95. std::function<bool(std::string, char*, int)> f = std::bind(&GpioPin::handleMQTT, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
  96. MQTTregisterSubscribeFunction(_mqttTopic, f);
  97. }
  98. }
  99. bool GpioPin::getValue(std::string* errorText)
  100. {
  101. if ((_mode != GPIO_PIN_MODE_INPUT) && (_mode != GPIO_PIN_MODE_INPUT_PULLUP) && (_mode != GPIO_PIN_MODE_INPUT_PULLDOWN)) {
  102. (*errorText) = "GPIO is not in input mode";
  103. }
  104. return gpio_get_level(_gpio) == 1;
  105. }
  106. void GpioPin::setValue(bool value, gpio_set_source setSource, std::string* errorText)
  107. {
  108. ESP_LOGD(TAG_SERVERGPIO, "GpioPin::setValue %d\r\n", value);
  109. if ((_mode != GPIO_PIN_MODE_OUTPUT) && (_mode != GPIO_PIN_MODE_OUTPUT_PWM) && (_mode != GPIO_PIN_MODE_BUILT_IN_FLASH_LED)) {
  110. (*errorText) = "GPIO is not in output mode";
  111. } else {
  112. gpio_set_level(_gpio, value);
  113. if ((_mqttTopic != "") && (setSource != GPIO_SET_SOURCE_MQTT)) {
  114. MQTTPublish(_mqttTopic, value ? "true" : "false");
  115. }
  116. }
  117. }
  118. void GpioPin::publishState() {
  119. int newState = gpio_get_level(_gpio);
  120. if (newState != currentState) {
  121. ESP_LOGD(TAG_SERVERGPIO,"publish state of GPIO %d new state %d", _gpio, newState);
  122. MQTTPublish(_mqttTopic, newState ? "true" : "false");
  123. currentState = newState;
  124. }
  125. }
  126. bool GpioPin::handleMQTT(std::string, char* data, int data_len) {
  127. ESP_LOGD(TAG_SERVERGPIO, "GpioPin::handleMQTT data %.*s\r\n", data_len, data);
  128. std::string dataStr(data, data_len);
  129. dataStr = toLower(dataStr);
  130. std::string errorText = "";
  131. if ((dataStr == "true") || (dataStr == "1")) {
  132. setValue(true, GPIO_SET_SOURCE_MQTT, &errorText);
  133. } else if ((dataStr == "false") || (dataStr == "0")) {
  134. setValue(false, GPIO_SET_SOURCE_MQTT, &errorText);
  135. } else {
  136. errorText = "wrong value ";
  137. errorText.append(data, data_len);
  138. }
  139. if (errorText != "") {
  140. ESP_LOGE(TAG_SERVERGPIO, "%s", errorText.c_str());
  141. }
  142. return (errorText == "");
  143. }
  144. esp_err_t callHandleHttpRequest(httpd_req_t *req)
  145. {
  146. ESP_LOGD(TAG_SERVERGPIO,"callHandleHttpRequest");
  147. GpioHandler *gpioHandler = (GpioHandler*)req->user_ctx;
  148. return gpioHandler->handleHttpRequest(req);
  149. }
  150. void taskGpioHandler(void *pvParameter)
  151. {
  152. ESP_LOGD(TAG_SERVERGPIO,"taskGpioHandler");
  153. ((GpioHandler*)pvParameter)->init();
  154. }
  155. GpioHandler::GpioHandler(std::string configFile, httpd_handle_t httpServer)
  156. {
  157. ESP_LOGI(TAG_SERVERGPIO,"start GpioHandler");
  158. _configFile = configFile;
  159. _httpServer = httpServer;
  160. ESP_LOGI(TAG_SERVERGPIO, "register GPIO Uri");
  161. registerGpioUri();
  162. }
  163. GpioHandler::~GpioHandler() {
  164. if (gpioMap != NULL) {
  165. clear();
  166. delete gpioMap;
  167. }
  168. }
  169. void GpioHandler::init()
  170. {
  171. // TickType_t xDelay = 60000 / portTICK_PERIOD_MS;
  172. // printf("wait before start %ldms\r\n", (long) xDelay);
  173. // vTaskDelay( xDelay );
  174. if (gpioMap == NULL) {
  175. gpioMap = new std::map<gpio_num_t, GpioPin*>();
  176. } else {
  177. clear();
  178. }
  179. ESP_LOGI(TAG_SERVERGPIO, "read GPIO config and init GPIO");
  180. readConfig();
  181. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
  182. it->second->init();
  183. }
  184. std::function<void()> f = std::bind(&GpioHandler::handleMQTTconnect, this);
  185. MQTTregisterConnectFunction("gpio-handler", f);
  186. if (xHandleTaskGpio == NULL) {
  187. gpio_queue_handle = xQueueCreate(10,sizeof(GpioResult));
  188. BaseType_t xReturned = xTaskCreate(&gpioHandlerTask, "gpio_int", configMINIMAL_STACK_SIZE * 8, (void *)this, tskIDLE_PRIORITY + 2, &xHandleTaskGpio);
  189. if(xReturned == pdPASS ) {
  190. ESP_LOGD(TAG_SERVERGPIO, "xHandletaskGpioHandler started");
  191. } else {
  192. ESP_LOGD(TAG_SERVERGPIO, "xHandletaskGpioHandler not started %d ", (int)xHandleTaskGpio);
  193. }
  194. }
  195. ESP_LOGI(TAG_SERVERGPIO, "GPIO init comleted");
  196. }
  197. void GpioHandler::taskHandler() {
  198. if (gpioMap != NULL) {
  199. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
  200. if ((it->second->getInterruptType() == GPIO_INTR_DISABLE))
  201. it->second->publishState();
  202. }
  203. }
  204. }
  205. void GpioHandler::handleMQTTconnect()
  206. {
  207. if (gpioMap != NULL) {
  208. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
  209. if ((it->second->getMode() == GPIO_PIN_MODE_INPUT) || (it->second->getMode() == GPIO_PIN_MODE_INPUT_PULLDOWN) || (it->second->getMode() == GPIO_PIN_MODE_INPUT_PULLUP))
  210. it->second->publishState();
  211. }
  212. }
  213. }
  214. void GpioHandler::deinit() {
  215. MQTTunregisterConnectFunction("gpio-handler");
  216. clear();
  217. if (xHandleTaskGpio != NULL) {
  218. vTaskDelete(xHandleTaskGpio);
  219. xHandleTaskGpio = NULL;
  220. }
  221. }
  222. void GpioHandler::gpioInterrupt(GpioResult* gpioResult) {
  223. if ((gpioMap != NULL) && (gpioMap->find(gpioResult->gpio) != gpioMap->end())) {
  224. (*gpioMap)[gpioResult->gpio]->gpioInterrupt(gpioResult->value);
  225. }
  226. }
  227. bool GpioHandler::readConfig()
  228. {
  229. if (!gpioMap->empty())
  230. clear();
  231. ConfigFile configFile = ConfigFile(_configFile);
  232. std::vector<std::string> zerlegt;
  233. std::string line = "";
  234. bool disabledLine = false;
  235. bool eof = false;
  236. while ((!configFile.GetNextParagraph(line, disabledLine, eof) || (line.compare("[GPIO]") != 0)) && !disabledLine && !eof) {}
  237. if (eof)
  238. return false;
  239. _isEnabled = true;
  240. std::string mainTopicMQTT = "";
  241. bool registerISR = false;
  242. while (configFile.getNextLine(&line, disabledLine, eof) && !configFile.isNewParagraph(line))
  243. {
  244. zerlegt = configFile.ZerlegeZeile(line);
  245. // const std::regex pieces_regex("IO([0-9]{1,2})");
  246. // std::smatch pieces_match;
  247. // if (std::regex_match(zerlegt[0], pieces_match, pieces_regex) && (pieces_match.size() == 2))
  248. // {
  249. // std::string gpioStr = pieces_match[1];
  250. ESP_LOGD(TAG_SERVERGPIO, "conf param %s\r\n", toUpper(zerlegt[0]).c_str());
  251. if (toUpper(zerlegt[0]) == "MAINTOPICMQTT") {
  252. ESP_LOGD(TAG_SERVERGPIO, "MAINTOPICMQTT found\r\n");
  253. mainTopicMQTT = zerlegt[1];
  254. } else if ((zerlegt[0].rfind("IO", 0) == 0) && (zerlegt.size() >= 6))
  255. {
  256. ESP_LOGI(TAG_SERVERGPIO,"Enable GP%s in %s mode", zerlegt[0].c_str(), zerlegt[1].c_str());
  257. std::string gpioStr = zerlegt[0].substr(2, 2);
  258. gpio_num_t gpioNr = (gpio_num_t)atoi(gpioStr.c_str());
  259. gpio_pin_mode_t pinMode = resolvePinMode(toLower(zerlegt[1]));
  260. gpio_int_type_t intType = resolveIntType(toLower(zerlegt[2]));
  261. uint16_t dutyResolution = (uint8_t)atoi(zerlegt[3].c_str());
  262. bool mqttEnabled = toLower(zerlegt[4]) == "true";
  263. bool httpEnabled = toLower(zerlegt[5]) == "true";
  264. char gpioName[100];
  265. if (zerlegt.size() >= 7) {
  266. strcpy(gpioName, trim(zerlegt[6]).c_str());
  267. } else {
  268. sprintf(gpioName, "GPIO%d", gpioNr);
  269. }
  270. std::string mqttTopic = mqttEnabled ? (mainTopicMQTT + "/" + gpioName) : "";
  271. GpioPin* gpioPin = new GpioPin(gpioNr, gpioName, pinMode, intType,dutyResolution, mqttTopic, httpEnabled);
  272. (*gpioMap)[gpioNr] = gpioPin;
  273. if (intType != GPIO_INTR_DISABLE) {
  274. registerISR = true;
  275. }
  276. }
  277. }
  278. if (registerISR) {
  279. //install gpio isr service
  280. gpio_install_isr_service(ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM);
  281. }
  282. return true;
  283. }
  284. void GpioHandler::clear()
  285. {
  286. ESP_LOGD(TAG_SERVERGPIO, "GpioHandler::clear\r\n");
  287. if (gpioMap != NULL) {
  288. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
  289. delete it->second;
  290. }
  291. gpioMap->clear();
  292. }
  293. // gpio_uninstall_isr_service(); can't uninstall, isr service is used by camera
  294. }
  295. void GpioHandler::registerGpioUri()
  296. {
  297. ESP_LOGI(TAG_SERVERGPIO, "server_GPIO - Registering URI handlers");
  298. httpd_uri_t camuri = { };
  299. camuri.method = HTTP_GET;
  300. camuri.uri = "/GPIO";
  301. camuri.handler = callHandleHttpRequest;
  302. camuri.user_ctx = (void*)this;
  303. httpd_register_uri_handler(_httpServer, &camuri);
  304. }
  305. esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
  306. {
  307. ESP_LOGD(TAG_SERVERGPIO, "handleHttpRequest");
  308. if (gpioMap == NULL) {
  309. std::string resp_str = "GPIO handler not initialized";
  310. httpd_resp_send(req, resp_str.c_str(), resp_str.length());
  311. return ESP_OK;
  312. }
  313. #ifdef DEBUG_DETAIL_ON
  314. LogFile.WriteHeapInfo("handler_switch_GPIO - Start");
  315. #endif
  316. LogFile.WriteToFile("handler_switch_GPIO");
  317. char _query[200];
  318. char _valueGPIO[30];
  319. char _valueStatus[30];
  320. std::string gpio, status;
  321. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK) {
  322. ESP_LOGD(TAG_SERVERGPIO, "Query: %s", _query);
  323. if (httpd_query_key_value(_query, "GPIO", _valueGPIO, 30) == ESP_OK)
  324. {
  325. ESP_LOGD(TAG_SERVERGPIO, "GPIO is found %s", _valueGPIO);
  326. gpio = std::string(_valueGPIO);
  327. } else {
  328. std::string resp_str = "GPIO No is not defined";
  329. httpd_resp_send(req, resp_str.c_str(), resp_str.length());
  330. return ESP_OK;
  331. }
  332. if (httpd_query_key_value(_query, "Status", _valueStatus, 30) == ESP_OK)
  333. {
  334. ESP_LOGD(TAG_SERVERGPIO, "Status is found %s", _valueStatus);
  335. status = std::string(_valueStatus);
  336. }
  337. } else {
  338. const char* resp_str = "Error in call. Use /GPIO?GPIO=12&Status=high";
  339. httpd_resp_send(req, resp_str, strlen(resp_str));
  340. return ESP_OK;
  341. }
  342. status = toUpper(status);
  343. if ((status != "HIGH") && (status != "LOW") && (status != "TRUE") && (status != "FALSE") && (status != "0") && (status != "1") && (status != ""))
  344. {
  345. std::string zw = "Status not valid: " + status;
  346. httpd_resp_sendstr_chunk(req, zw.c_str());
  347. httpd_resp_sendstr_chunk(req, NULL);
  348. return ESP_OK;
  349. }
  350. int gpionum = stoi(gpio);
  351. // frei: 16; 12-15; 2; 4 // nur 12 und 13 funktionieren 2: reboot, 4: BlitzLED, 15: PSRAM, 14/15: DMA für SDKarte ???
  352. gpio_num_t gpio_num = resolvePinNr(gpionum);
  353. if (gpio_num == GPIO_NUM_NC)
  354. {
  355. std::string zw = "GPIO" + std::to_string(gpionum) + " not support - only 12 & 13 free";
  356. httpd_resp_sendstr_chunk(req, zw.c_str());
  357. httpd_resp_sendstr_chunk(req, NULL);
  358. return ESP_OK;
  359. }
  360. if (gpioMap->count(gpio_num) == 0) {
  361. char resp_str [30];
  362. sprintf(resp_str, "GPIO%d is not registred", gpio_num);
  363. httpd_resp_send(req, resp_str, strlen(resp_str));
  364. return ESP_OK;
  365. }
  366. if (status == "")
  367. {
  368. std::string resp_str = "";
  369. status = (*gpioMap)[gpio_num]->getValue(&resp_str) ? "HIGH" : "LOW";
  370. if (resp_str == "") {
  371. resp_str = status;
  372. }
  373. httpd_resp_sendstr_chunk(req, resp_str.c_str());
  374. httpd_resp_sendstr_chunk(req, NULL);
  375. }
  376. else
  377. {
  378. std::string resp_str = "";
  379. (*gpioMap)[gpio_num]->setValue((status == "HIGH") || (status == "TRUE") || (status == "1"), GPIO_SET_SOURCE_HTTP, &resp_str);
  380. if (resp_str == "") {
  381. resp_str = "GPIO" + std::to_string(gpionum) + " switched to " + status;
  382. }
  383. httpd_resp_sendstr_chunk(req, resp_str.c_str());
  384. httpd_resp_sendstr_chunk(req, NULL);
  385. }
  386. return ESP_OK;
  387. };
  388. void GpioHandler::flashLightEnable(bool value)
  389. {
  390. ESP_LOGD(TAG_SERVERGPIO, "GpioHandler::flashLightEnable %s\r\n", value ? "true" : "false");
  391. if (gpioMap != NULL) {
  392. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it)
  393. {
  394. if (it->second->getMode() == GPIO_PIN_MODE_BUILT_IN_FLASH_LED) //|| (it->second->getMode() == GPIO_PIN_MODE_EXTERNAL_FLASH_PWM) || (it->second->getMode() == GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X))
  395. {
  396. std::string resp_str = "";
  397. it->second->setValue(value, GPIO_SET_SOURCE_INTERNAL, &resp_str);
  398. if (resp_str == "") {
  399. ESP_LOGD(TAG_SERVERGPIO, "Flash light pin GPIO %d switched to %s\r\n", (int)it->first, (value ? "on" : "off"));
  400. } else {
  401. ESP_LOGE(TAG_SERVERGPIO, "Can't set flash light pin GPIO %d. Error: %s\r\n", (int)it->first, resp_str.c_str());
  402. }
  403. }
  404. }
  405. }
  406. }
  407. gpio_num_t GpioHandler::resolvePinNr(uint8_t pinNr)
  408. {
  409. switch(pinNr) {
  410. case 0:
  411. return GPIO_NUM_0;
  412. case 1:
  413. return GPIO_NUM_1;
  414. case 3:
  415. return GPIO_NUM_3;
  416. case 4:
  417. return GPIO_NUM_4;
  418. case 12:
  419. return GPIO_NUM_12;
  420. case 13:
  421. return GPIO_NUM_13;
  422. default:
  423. return GPIO_NUM_NC;
  424. }
  425. }
  426. gpio_pin_mode_t GpioHandler::resolvePinMode(std::string input)
  427. {
  428. if( input == "disabled" ) return GPIO_PIN_MODE_DISABLED;
  429. if( input == "input" ) return GPIO_PIN_MODE_INPUT;
  430. if( input == "input-pullup" ) return GPIO_PIN_MODE_INPUT_PULLUP;
  431. if( input == "input-pulldown" ) return GPIO_PIN_MODE_INPUT_PULLDOWN;
  432. if( input == "output" ) return GPIO_PIN_MODE_OUTPUT;
  433. if( input == "built-in-led" ) return GPIO_PIN_MODE_BUILT_IN_FLASH_LED;
  434. if( input == "output-pwm" ) return GPIO_PIN_MODE_OUTPUT_PWM;
  435. if( input == "external-flash-pwm" ) return GPIO_PIN_MODE_EXTERNAL_FLASH_PWM;
  436. if( input == "external-flash-ws281x" ) return GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X;
  437. return GPIO_PIN_MODE_DISABLED;
  438. }
  439. gpio_int_type_t GpioHandler::resolveIntType(std::string input)
  440. {
  441. if( input == "disabled" ) return GPIO_INTR_DISABLE;
  442. if( input == "rising-edge" ) return GPIO_INTR_POSEDGE;
  443. if( input == "falling-edge" ) return GPIO_INTR_NEGEDGE;
  444. if( input == "rising-and-falling" ) return GPIO_INTR_ANYEDGE ;
  445. if( input == "low-level-trigger" ) return GPIO_INTR_LOW_LEVEL;
  446. if( input == "high-level-trigger" ) return GPIO_INTR_HIGH_LEVEL;
  447. return GPIO_INTR_DISABLE;
  448. }
  449. static GpioHandler *gpioHandler = NULL;
  450. void gpio_handler_create(httpd_handle_t server)
  451. {
  452. if (gpioHandler == NULL)
  453. gpioHandler = new GpioHandler(CONFIG_FILE, server);
  454. }
  455. void gpio_handler_init()
  456. {
  457. if (gpioHandler != NULL) {
  458. gpioHandler->init();
  459. }
  460. }
  461. void gpio_handler_deinit() {
  462. if (gpioHandler != NULL) {
  463. gpioHandler->deinit();
  464. }
  465. }
  466. void gpio_handler_destroy()
  467. {
  468. if (gpioHandler != NULL) {
  469. delete gpioHandler;
  470. gpioHandler = NULL;
  471. }
  472. }
  473. GpioHandler* gpio_handler_get()
  474. {
  475. return gpioHandler;
  476. }