server_GPIO.cpp 18 KB

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