server_GPIO.cpp 19 KB

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