server_GPIO.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. if (!readConfig()) {
  181. clear();
  182. delete gpioMap;
  183. gpioMap = NULL;
  184. return;
  185. }
  186. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
  187. it->second->init();
  188. }
  189. std::function<void()> f = std::bind(&GpioHandler::handleMQTTconnect, this);
  190. MQTTregisterConnectFunction("gpio-handler", f);
  191. if (xHandleTaskGpio == NULL) {
  192. gpio_queue_handle = xQueueCreate(10,sizeof(GpioResult));
  193. BaseType_t xReturned = xTaskCreate(&gpioHandlerTask, "gpio_int", configMINIMAL_STACK_SIZE * 8, (void *)this, tskIDLE_PRIORITY + 2, &xHandleTaskGpio);
  194. if(xReturned == pdPASS ) {
  195. ESP_LOGD(TAG_SERVERGPIO, "xHandletaskGpioHandler started");
  196. } else {
  197. ESP_LOGD(TAG_SERVERGPIO, "xHandletaskGpioHandler not started %d ", (int)xHandleTaskGpio);
  198. }
  199. }
  200. ESP_LOGI(TAG_SERVERGPIO, "GPIO init comleted");
  201. }
  202. void GpioHandler::taskHandler() {
  203. if (gpioMap != NULL) {
  204. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
  205. if ((it->second->getInterruptType() == GPIO_INTR_DISABLE))
  206. it->second->publishState();
  207. }
  208. }
  209. }
  210. void GpioHandler::handleMQTTconnect()
  211. {
  212. if (gpioMap != NULL) {
  213. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
  214. if ((it->second->getMode() == GPIO_PIN_MODE_INPUT) || (it->second->getMode() == GPIO_PIN_MODE_INPUT_PULLDOWN) || (it->second->getMode() == GPIO_PIN_MODE_INPUT_PULLUP))
  215. it->second->publishState();
  216. }
  217. }
  218. }
  219. void GpioHandler::deinit() {
  220. MQTTunregisterConnectFunction("gpio-handler");
  221. clear();
  222. if (xHandleTaskGpio != NULL) {
  223. vTaskDelete(xHandleTaskGpio);
  224. xHandleTaskGpio = NULL;
  225. }
  226. }
  227. void GpioHandler::gpioInterrupt(GpioResult* gpioResult) {
  228. if ((gpioMap != NULL) && (gpioMap->find(gpioResult->gpio) != gpioMap->end())) {
  229. (*gpioMap)[gpioResult->gpio]->gpioInterrupt(gpioResult->value);
  230. }
  231. }
  232. bool GpioHandler::readConfig()
  233. {
  234. if (!gpioMap->empty())
  235. clear();
  236. ConfigFile configFile = ConfigFile(_configFile);
  237. std::vector<std::string> zerlegt;
  238. std::string line = "";
  239. bool disabledLine = false;
  240. bool eof = false;
  241. while ((!configFile.GetNextParagraph(line, disabledLine, eof) || (line.compare("[GPIO]") != 0)) && !disabledLine && !eof) {}
  242. if (eof)
  243. return false;
  244. _isEnabled = !disabledLine;
  245. if (!_isEnabled)
  246. return false;
  247. std::string mainTopicMQTT = "";
  248. bool registerISR = false;
  249. while (configFile.getNextLine(&line, disabledLine, eof) && !configFile.isNewParagraph(line))
  250. {
  251. zerlegt = configFile.ZerlegeZeile(line);
  252. // const std::regex pieces_regex("IO([0-9]{1,2})");
  253. // std::smatch pieces_match;
  254. // if (std::regex_match(zerlegt[0], pieces_match, pieces_regex) && (pieces_match.size() == 2))
  255. // {
  256. // std::string gpioStr = pieces_match[1];
  257. ESP_LOGD(TAG_SERVERGPIO, "conf param %s\r\n", toUpper(zerlegt[0]).c_str());
  258. if (toUpper(zerlegt[0]) == "MAINTOPICMQTT") {
  259. ESP_LOGD(TAG_SERVERGPIO, "MAINTOPICMQTT found\r\n");
  260. mainTopicMQTT = zerlegt[1];
  261. } else if ((zerlegt[0].rfind("IO", 0) == 0) && (zerlegt.size() >= 6))
  262. {
  263. ESP_LOGI(TAG_SERVERGPIO,"Enable GP%s in %s mode", zerlegt[0].c_str(), zerlegt[1].c_str());
  264. std::string gpioStr = zerlegt[0].substr(2, 2);
  265. gpio_num_t gpioNr = (gpio_num_t)atoi(gpioStr.c_str());
  266. gpio_pin_mode_t pinMode = resolvePinMode(toLower(zerlegt[1]));
  267. gpio_int_type_t intType = resolveIntType(toLower(zerlegt[2]));
  268. uint16_t dutyResolution = (uint8_t)atoi(zerlegt[3].c_str());
  269. bool mqttEnabled = toLower(zerlegt[4]) == "true";
  270. bool httpEnabled = toLower(zerlegt[5]) == "true";
  271. char gpioName[100];
  272. if (zerlegt.size() >= 7) {
  273. strcpy(gpioName, trim(zerlegt[6]).c_str());
  274. } else {
  275. sprintf(gpioName, "GPIO%d", gpioNr);
  276. }
  277. std::string mqttTopic = mqttEnabled ? (mainTopicMQTT + "/" + gpioName) : "";
  278. GpioPin* gpioPin = new GpioPin(gpioNr, gpioName, pinMode, intType,dutyResolution, mqttTopic, httpEnabled);
  279. (*gpioMap)[gpioNr] = gpioPin;
  280. if (intType != GPIO_INTR_DISABLE) {
  281. registerISR = true;
  282. }
  283. }
  284. }
  285. if (registerISR) {
  286. //install gpio isr service
  287. gpio_install_isr_service(ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM);
  288. }
  289. return true;
  290. }
  291. void GpioHandler::clear()
  292. {
  293. ESP_LOGD(TAG_SERVERGPIO, "GpioHandler::clear\r\n");
  294. if (gpioMap != NULL) {
  295. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
  296. delete it->second;
  297. }
  298. gpioMap->clear();
  299. }
  300. // gpio_uninstall_isr_service(); can't uninstall, isr service is used by camera
  301. }
  302. void GpioHandler::registerGpioUri()
  303. {
  304. ESP_LOGI(TAG_SERVERGPIO, "server_GPIO - Registering URI handlers");
  305. httpd_uri_t camuri = { };
  306. camuri.method = HTTP_GET;
  307. camuri.uri = "/GPIO";
  308. camuri.handler = callHandleHttpRequest;
  309. camuri.user_ctx = (void*)this;
  310. httpd_register_uri_handler(_httpServer, &camuri);
  311. }
  312. esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
  313. {
  314. ESP_LOGD(TAG_SERVERGPIO, "handleHttpRequest");
  315. if (gpioMap == NULL) {
  316. std::string resp_str = "GPIO handler not initialized";
  317. httpd_resp_send(req, resp_str.c_str(), resp_str.length());
  318. return ESP_OK;
  319. }
  320. #ifdef DEBUG_DETAIL_ON
  321. LogFile.WriteHeapInfo("handler_switch_GPIO - Start");
  322. #endif
  323. LogFile.WriteToFile("handler_switch_GPIO");
  324. char _query[200];
  325. char _valueGPIO[30];
  326. char _valueStatus[30];
  327. std::string gpio, status;
  328. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK) {
  329. ESP_LOGD(TAG_SERVERGPIO, "Query: %s", _query);
  330. if (httpd_query_key_value(_query, "GPIO", _valueGPIO, 30) == ESP_OK)
  331. {
  332. ESP_LOGD(TAG_SERVERGPIO, "GPIO is found %s", _valueGPIO);
  333. gpio = std::string(_valueGPIO);
  334. } else {
  335. std::string resp_str = "GPIO No is not defined";
  336. httpd_resp_send(req, resp_str.c_str(), resp_str.length());
  337. return ESP_OK;
  338. }
  339. if (httpd_query_key_value(_query, "Status", _valueStatus, 30) == ESP_OK)
  340. {
  341. ESP_LOGD(TAG_SERVERGPIO, "Status is found %s", _valueStatus);
  342. status = std::string(_valueStatus);
  343. }
  344. } else {
  345. const char* resp_str = "Error in call. Use /GPIO?GPIO=12&Status=high";
  346. httpd_resp_send(req, resp_str, strlen(resp_str));
  347. return ESP_OK;
  348. }
  349. status = toUpper(status);
  350. if ((status != "HIGH") && (status != "LOW") && (status != "TRUE") && (status != "FALSE") && (status != "0") && (status != "1") && (status != ""))
  351. {
  352. std::string zw = "Status not valid: " + status;
  353. httpd_resp_sendstr_chunk(req, zw.c_str());
  354. httpd_resp_sendstr_chunk(req, NULL);
  355. return ESP_OK;
  356. }
  357. int gpionum = stoi(gpio);
  358. // frei: 16; 12-15; 2; 4 // nur 12 und 13 funktionieren 2: reboot, 4: BlitzLED, 15: PSRAM, 14/15: DMA für SDKarte ???
  359. gpio_num_t gpio_num = resolvePinNr(gpionum);
  360. if (gpio_num == GPIO_NUM_NC)
  361. {
  362. std::string zw = "GPIO" + std::to_string(gpionum) + " not support - only 12 & 13 free";
  363. httpd_resp_sendstr_chunk(req, zw.c_str());
  364. httpd_resp_sendstr_chunk(req, NULL);
  365. return ESP_OK;
  366. }
  367. if (gpioMap->count(gpio_num) == 0) {
  368. char resp_str [30];
  369. sprintf(resp_str, "GPIO%d is not registred", gpio_num);
  370. httpd_resp_send(req, resp_str, strlen(resp_str));
  371. return ESP_OK;
  372. }
  373. if (status == "")
  374. {
  375. std::string resp_str = "";
  376. status = (*gpioMap)[gpio_num]->getValue(&resp_str) ? "HIGH" : "LOW";
  377. if (resp_str == "") {
  378. resp_str = status;
  379. }
  380. httpd_resp_sendstr_chunk(req, resp_str.c_str());
  381. httpd_resp_sendstr_chunk(req, NULL);
  382. }
  383. else
  384. {
  385. std::string resp_str = "";
  386. (*gpioMap)[gpio_num]->setValue((status == "HIGH") || (status == "TRUE") || (status == "1"), GPIO_SET_SOURCE_HTTP, &resp_str);
  387. if (resp_str == "") {
  388. resp_str = "GPIO" + std::to_string(gpionum) + " switched to " + status;
  389. }
  390. httpd_resp_sendstr_chunk(req, resp_str.c_str());
  391. httpd_resp_sendstr_chunk(req, NULL);
  392. }
  393. return ESP_OK;
  394. };
  395. void GpioHandler::flashLightEnable(bool value)
  396. {
  397. ESP_LOGD(TAG_SERVERGPIO, "GpioHandler::flashLightEnable %s\r\n", value ? "true" : "false");
  398. if (gpioMap != NULL) {
  399. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it)
  400. {
  401. 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))
  402. {
  403. std::string resp_str = "";
  404. it->second->setValue(value, GPIO_SET_SOURCE_INTERNAL, &resp_str);
  405. if (resp_str == "") {
  406. ESP_LOGD(TAG_SERVERGPIO, "Flash light pin GPIO %d switched to %s\r\n", (int)it->first, (value ? "on" : "off"));
  407. } else {
  408. ESP_LOGE(TAG_SERVERGPIO, "Can't set flash light pin GPIO %d. Error: %s\r\n", (int)it->first, resp_str.c_str());
  409. }
  410. }
  411. }
  412. }
  413. }
  414. gpio_num_t GpioHandler::resolvePinNr(uint8_t pinNr)
  415. {
  416. switch(pinNr) {
  417. case 0:
  418. return GPIO_NUM_0;
  419. case 1:
  420. return GPIO_NUM_1;
  421. case 3:
  422. return GPIO_NUM_3;
  423. case 4:
  424. return GPIO_NUM_4;
  425. case 12:
  426. return GPIO_NUM_12;
  427. case 13:
  428. return GPIO_NUM_13;
  429. default:
  430. return GPIO_NUM_NC;
  431. }
  432. }
  433. gpio_pin_mode_t GpioHandler::resolvePinMode(std::string input)
  434. {
  435. if( input == "disabled" ) return GPIO_PIN_MODE_DISABLED;
  436. if( input == "input" ) return GPIO_PIN_MODE_INPUT;
  437. if( input == "input-pullup" ) return GPIO_PIN_MODE_INPUT_PULLUP;
  438. if( input == "input-pulldown" ) return GPIO_PIN_MODE_INPUT_PULLDOWN;
  439. if( input == "output" ) return GPIO_PIN_MODE_OUTPUT;
  440. if( input == "built-in-led" ) return GPIO_PIN_MODE_BUILT_IN_FLASH_LED;
  441. if( input == "output-pwm" ) return GPIO_PIN_MODE_OUTPUT_PWM;
  442. if( input == "external-flash-pwm" ) return GPIO_PIN_MODE_EXTERNAL_FLASH_PWM;
  443. if( input == "external-flash-ws281x" ) return GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X;
  444. return GPIO_PIN_MODE_DISABLED;
  445. }
  446. gpio_int_type_t GpioHandler::resolveIntType(std::string input)
  447. {
  448. if( input == "disabled" ) return GPIO_INTR_DISABLE;
  449. if( input == "rising-edge" ) return GPIO_INTR_POSEDGE;
  450. if( input == "falling-edge" ) return GPIO_INTR_NEGEDGE;
  451. if( input == "rising-and-falling" ) return GPIO_INTR_ANYEDGE ;
  452. if( input == "low-level-trigger" ) return GPIO_INTR_LOW_LEVEL;
  453. if( input == "high-level-trigger" ) return GPIO_INTR_HIGH_LEVEL;
  454. return GPIO_INTR_DISABLE;
  455. }
  456. static GpioHandler *gpioHandler = NULL;
  457. void gpio_handler_create(httpd_handle_t server)
  458. {
  459. if (gpioHandler == NULL)
  460. gpioHandler = new GpioHandler(CONFIG_FILE, server);
  461. }
  462. void gpio_handler_init()
  463. {
  464. if (gpioHandler != NULL) {
  465. gpioHandler->init();
  466. }
  467. }
  468. void gpio_handler_deinit() {
  469. if (gpioHandler != NULL) {
  470. gpioHandler->deinit();
  471. }
  472. }
  473. void gpio_handler_destroy()
  474. {
  475. if (gpioHandler != NULL) {
  476. delete gpioHandler;
  477. gpioHandler = NULL;
  478. }
  479. }
  480. GpioHandler* gpio_handler_get()
  481. {
  482. return gpioHandler;
  483. }