server_GPIO.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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_log.h"
  9. #include <sys/stat.h>
  10. #include <vector>
  11. #include "../../include/defines.h"
  12. #include "server_GPIO.h"
  13. #include "ClassLogFile.h"
  14. #include "configFile.h"
  15. #include "Helper.h"
  16. #ifdef ENABLE_MQTT
  17. #include "interface_mqtt.h"
  18. #include "server_mqtt.h"
  19. #endif //ENABLE_MQTT
  20. #include "basic_auth.h"
  21. static const char *TAG = "GPIO";
  22. QueueHandle_t gpio_queue_handle = NULL;
  23. 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)
  24. {
  25. _gpio = gpio;
  26. _name = name;
  27. _mode = mode;
  28. _interruptType = interruptType;
  29. _mqttTopic = mqttTopic;
  30. }
  31. GpioPin::~GpioPin()
  32. {
  33. ESP_LOGD(TAG,"reset GPIO pin %d", _gpio);
  34. if (_interruptType != GPIO_INTR_DISABLE) {
  35. //hook isr handler for specific gpio pin
  36. gpio_isr_handler_remove(_gpio);
  37. }
  38. gpio_reset_pin(_gpio);
  39. }
  40. static void IRAM_ATTR gpio_isr_handler(void* arg)
  41. {
  42. GpioResult gpioResult;
  43. gpioResult.gpio = *(gpio_num_t*) arg;
  44. gpioResult.value = gpio_get_level(gpioResult.gpio);
  45. BaseType_t ContextSwitchRequest = pdFALSE;
  46. xQueueSendToBackFromISR(gpio_queue_handle,(void*)&gpioResult,&ContextSwitchRequest);
  47. if(ContextSwitchRequest){
  48. taskYIELD();
  49. }
  50. }
  51. static void gpioHandlerTask(void *arg) {
  52. ESP_LOGD(TAG,"start interrupt task");
  53. while(1){
  54. if(uxQueueMessagesWaiting(gpio_queue_handle)){
  55. while(uxQueueMessagesWaiting(gpio_queue_handle)){
  56. GpioResult gpioResult;
  57. xQueueReceive(gpio_queue_handle,(void*)&gpioResult,10);
  58. ESP_LOGD(TAG,"gpio: %d state: %d", gpioResult.gpio, gpioResult.value);
  59. ((GpioHandler*)arg)->gpioInterrupt(&gpioResult);
  60. }
  61. }
  62. ((GpioHandler*)arg)->taskHandler();
  63. vTaskDelay(pdMS_TO_TICKS(1000));
  64. }
  65. }
  66. void GpioPin::gpioInterrupt(int value) {
  67. #ifdef ENABLE_MQTT
  68. if (_mqttTopic.compare("") != 0) {
  69. ESP_LOGD(TAG, "gpioInterrupt %s %d", _mqttTopic.c_str(), value);
  70. MQTTPublish(_mqttTopic, value ? "true" : "false", 1);
  71. }
  72. #endif //ENABLE_MQTT
  73. currentState = value;
  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) { // ohne GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X, wenn das genutzt wird, dann soll auch der Handler hier nicht initialisiert werden, da das dann über SmartLED erfolgt.
  91. if ((_interruptType != GPIO_INTR_DISABLE) && (_interruptType != GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X)) {
  92. //hook isr handler for specific gpio pin
  93. ESP_LOGD(TAG, "GpioPin::init add isr handler for GPIO %d", _gpio);
  94. gpio_isr_handler_add(_gpio, gpio_isr_handler, (void*)&_gpio);
  95. }
  96. #ifdef ENABLE_MQTT
  97. if ((_mqttTopic.compare("") != 0) && ((_mode == GPIO_PIN_MODE_OUTPUT) || (_mode == GPIO_PIN_MODE_OUTPUT_PWM) || (_mode == GPIO_PIN_MODE_BUILT_IN_FLASH_LED))) {
  98. std::function<bool(std::string, char*, int)> f = std::bind(&GpioPin::handleMQTT, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
  99. MQTTregisterSubscribeFunction(_mqttTopic, f);
  100. }
  101. #endif //ENABLE_MQTT
  102. }
  103. bool GpioPin::getValue(std::string* errorText)
  104. {
  105. if ((_mode != GPIO_PIN_MODE_INPUT) && (_mode != GPIO_PIN_MODE_INPUT_PULLUP) && (_mode != GPIO_PIN_MODE_INPUT_PULLDOWN)) {
  106. (*errorText) = "GPIO is not in input mode";
  107. }
  108. return gpio_get_level(_gpio) == 1;
  109. }
  110. void GpioPin::setValue(bool value, gpio_set_source setSource, std::string* errorText)
  111. {
  112. ESP_LOGD(TAG, "GpioPin::setValue %d", value);
  113. if ((_mode != GPIO_PIN_MODE_OUTPUT) && (_mode != GPIO_PIN_MODE_OUTPUT_PWM) && (_mode != GPIO_PIN_MODE_BUILT_IN_FLASH_LED)) {
  114. (*errorText) = "GPIO is not in output mode";
  115. } else {
  116. gpio_set_level(_gpio, value);
  117. #ifdef ENABLE_MQTT
  118. if ((_mqttTopic.compare("") != 0) && (setSource != GPIO_SET_SOURCE_MQTT)) {
  119. MQTTPublish(_mqttTopic, value ? "true" : "false", 1);
  120. }
  121. #endif //ENABLE_MQTT
  122. }
  123. }
  124. void GpioPin::publishState() {
  125. int newState = gpio_get_level(_gpio);
  126. if (newState != currentState) {
  127. ESP_LOGD(TAG,"publish state of GPIO %d new state %d", _gpio, newState);
  128. #ifdef ENABLE_MQTT
  129. if (_mqttTopic.compare("") != 0)
  130. MQTTPublish(_mqttTopic, newState ? "true" : "false", 1);
  131. #endif //ENABLE_MQTT
  132. currentState = newState;
  133. }
  134. }
  135. #ifdef ENABLE_MQTT
  136. bool GpioPin::handleMQTT(std::string, char* data, int data_len) {
  137. ESP_LOGD(TAG, "GpioPin::handleMQTT data %.*s", data_len, data);
  138. std::string dataStr(data, data_len);
  139. dataStr = toLower(dataStr);
  140. std::string errorText = "";
  141. if ((dataStr == "true") || (dataStr == "1")) {
  142. setValue(true, GPIO_SET_SOURCE_MQTT, &errorText);
  143. } else if ((dataStr == "false") || (dataStr == "0")) {
  144. setValue(false, GPIO_SET_SOURCE_MQTT, &errorText);
  145. } else {
  146. errorText = "wrong value ";
  147. errorText.append(data, data_len);
  148. }
  149. if (errorText != "") {
  150. ESP_LOGE(TAG, "%s", errorText.c_str());
  151. }
  152. return (errorText == "");
  153. }
  154. #endif //ENABLE_MQTT
  155. esp_err_t callHandleHttpRequest(httpd_req_t *req)
  156. {
  157. ESP_LOGD(TAG,"callHandleHttpRequest");
  158. GpioHandler *gpioHandler = (GpioHandler*)req->user_ctx;
  159. return gpioHandler->handleHttpRequest(req);
  160. }
  161. void taskGpioHandler(void *pvParameter)
  162. {
  163. ESP_LOGD(TAG,"taskGpioHandler");
  164. ((GpioHandler*)pvParameter)->init();
  165. }
  166. GpioHandler::GpioHandler(std::string configFile, httpd_handle_t httpServer)
  167. {
  168. ESP_LOGI(TAG,"start GpioHandler");
  169. _configFile = configFile;
  170. _httpServer = httpServer;
  171. ESP_LOGI(TAG, "register GPIO Uri");
  172. registerGpioUri();
  173. }
  174. GpioHandler::~GpioHandler() {
  175. if (gpioMap != NULL) {
  176. clear();
  177. delete gpioMap;
  178. }
  179. }
  180. void GpioHandler::init()
  181. {
  182. // TickType_t xDelay = 60000 / portTICK_PERIOD_MS;
  183. // ESP_LOGD(TAG, "wait before start %ldms", (long) xDelay);
  184. // vTaskDelay( xDelay );
  185. ESP_LOGD(TAG, "*************** Start GPIOHandler_Init *****************");
  186. if (gpioMap == NULL) {
  187. gpioMap = new std::map<gpio_num_t, GpioPin*>();
  188. } else {
  189. clear();
  190. }
  191. ESP_LOGI(TAG, "read GPIO config and init GPIO");
  192. if (!readConfig()) {
  193. clear();
  194. delete gpioMap;
  195. gpioMap = NULL;
  196. ESP_LOGI(TAG, "GPIO init completed, handler is disabled");
  197. return;
  198. }
  199. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
  200. it->second->init();
  201. }
  202. #ifdef ENABLE_MQTT
  203. std::function<void()> f = std::bind(&GpioHandler::handleMQTTconnect, this);
  204. MQTTregisterConnectFunction("gpio-handler", f);
  205. #endif //ENABLE_MQTT
  206. if (xHandleTaskGpio == NULL) {
  207. gpio_queue_handle = xQueueCreate(10,sizeof(GpioResult));
  208. BaseType_t xReturned = xTaskCreate(&gpioHandlerTask, "gpio_int", 3 * 1024, (void *)this, tskIDLE_PRIORITY + 4, &xHandleTaskGpio);
  209. if(xReturned == pdPASS ) {
  210. ESP_LOGD(TAG, "xHandletaskGpioHandler started");
  211. } else {
  212. ESP_LOGD(TAG, "xHandletaskGpioHandler not started %d ", (int)xHandleTaskGpio);
  213. }
  214. }
  215. ESP_LOGI(TAG, "GPIO init completed, is enabled");
  216. }
  217. void GpioHandler::taskHandler() {
  218. if (gpioMap != NULL) {
  219. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
  220. if ((it->second->getInterruptType() == GPIO_INTR_DISABLE))
  221. it->second->publishState();
  222. }
  223. }
  224. }
  225. #ifdef ENABLE_MQTT
  226. void GpioHandler::handleMQTTconnect()
  227. {
  228. if (gpioMap != NULL) {
  229. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
  230. if ((it->second->getMode() == GPIO_PIN_MODE_INPUT) || (it->second->getMode() == GPIO_PIN_MODE_INPUT_PULLDOWN) || (it->second->getMode() == GPIO_PIN_MODE_INPUT_PULLUP))
  231. it->second->publishState();
  232. }
  233. }
  234. }
  235. #endif //ENABLE_MQTT
  236. void GpioHandler::deinit() {
  237. #ifdef ENABLE_MQTT
  238. MQTTunregisterConnectFunction("gpio-handler");
  239. #endif //ENABLE_MQTT
  240. clear();
  241. if (xHandleTaskGpio != NULL) {
  242. vTaskDelete(xHandleTaskGpio);
  243. xHandleTaskGpio = NULL;
  244. }
  245. }
  246. void GpioHandler::gpioInterrupt(GpioResult* gpioResult) {
  247. if ((gpioMap != NULL) && (gpioMap->find(gpioResult->gpio) != gpioMap->end())) {
  248. (*gpioMap)[gpioResult->gpio]->gpioInterrupt(gpioResult->value);
  249. }
  250. }
  251. bool GpioHandler::readConfig()
  252. {
  253. if (!gpioMap->empty())
  254. clear();
  255. ConfigFile configFile = ConfigFile(_configFile);
  256. std::vector<std::string> splitted;
  257. std::string line = "";
  258. bool disabledLine = false;
  259. bool eof = false;
  260. gpio_num_t gpioExtLED = (gpio_num_t) 0;
  261. // ESP_LOGD(TAG, "readConfig - Start 1");
  262. while ((!configFile.GetNextParagraph(line, disabledLine, eof) || (line.compare("[GPIO]") != 0)) && !eof) {}
  263. if (eof)
  264. return false;
  265. // ESP_LOGD(TAG, "readConfig - Start 2 line: %s, disabbledLine: %d", line.c_str(), (int) disabledLine);
  266. _isEnabled = !disabledLine;
  267. if (!_isEnabled)
  268. return false;
  269. // ESP_LOGD(TAG, "readConfig - Start 3");
  270. #ifdef ENABLE_MQTT
  271. // std::string mainTopicMQTT = "";
  272. std::string mainTopicMQTT = mqttServer_getMainTopic();
  273. if (mainTopicMQTT.length() > 0)
  274. {
  275. mainTopicMQTT = mainTopicMQTT + "/GPIO";
  276. ESP_LOGD(TAG, "MAINTOPICMQTT found");
  277. }
  278. #endif // ENABLE_MQTT
  279. bool registerISR = false;
  280. while (configFile.getNextLine(&line, disabledLine, eof) && !configFile.isNewParagraph(line))
  281. {
  282. splitted = ZerlegeZeile(line);
  283. // const std::regex pieces_regex("IO([0-9]{1,2})");
  284. // std::smatch pieces_match;
  285. // if (std::regex_match(splitted[0], pieces_match, pieces_regex) && (pieces_match.size() == 2))
  286. // {
  287. // std::string gpioStr = pieces_match[1];
  288. ESP_LOGD(TAG, "conf param %s", toUpper(splitted[0]).c_str());
  289. if (toUpper(splitted[0]) == "MAINTOPICMQTT") {
  290. // ESP_LOGD(TAG, "MAINTOPICMQTT found");
  291. // mainTopicMQTT = splitted[1];
  292. } else if ((splitted[0].rfind("IO", 0) == 0) && (splitted.size() >= 6))
  293. {
  294. ESP_LOGI(TAG,"Enable GP%s in %s mode", splitted[0].c_str(), splitted[1].c_str());
  295. std::string gpioStr = splitted[0].substr(2, 2);
  296. gpio_num_t gpioNr = (gpio_num_t)atoi(gpioStr.c_str());
  297. gpio_pin_mode_t pinMode = resolvePinMode(toLower(splitted[1]));
  298. gpio_int_type_t intType = resolveIntType(toLower(splitted[2]));
  299. uint16_t dutyResolution = (uint8_t)atoi(splitted[3].c_str());
  300. #ifdef ENABLE_MQTT
  301. bool mqttEnabled = (toLower(splitted[4]) == "true");
  302. #endif // ENABLE_MQTT
  303. bool httpEnabled = (toLower(splitted[5]) == "true");
  304. char gpioName[100];
  305. if (splitted.size() >= 7) {
  306. strcpy(gpioName, trim(splitted[6]).c_str());
  307. } else {
  308. sprintf(gpioName, "GPIO%d", gpioNr);
  309. }
  310. #ifdef ENABLE_MQTT
  311. std::string mqttTopic = mqttEnabled ? (mainTopicMQTT + "/" + gpioName) : "";
  312. #else // ENABLE_MQTT
  313. std::string mqttTopic = "";
  314. #endif // ENABLE_MQTT
  315. GpioPin* gpioPin = new GpioPin(gpioNr, gpioName, pinMode, intType,dutyResolution, mqttTopic, httpEnabled);
  316. (*gpioMap)[gpioNr] = gpioPin;
  317. if (pinMode == GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X)
  318. {
  319. ESP_LOGD(TAG, "Set WS2812 to GPIO %d", gpioNr);
  320. gpioExtLED = gpioNr;
  321. }
  322. if (intType != GPIO_INTR_DISABLE) {
  323. registerISR = true;
  324. }
  325. }
  326. if (toUpper(splitted[0]) == "LEDNUMBERS")
  327. {
  328. LEDNumbers = stoi(splitted[1]);
  329. }
  330. if (toUpper(splitted[0]) == "LEDCOLOR")
  331. {
  332. uint8_t _r, _g, _b;
  333. _r = stoi(splitted[1]);
  334. _g = stoi(splitted[2]);
  335. _b = stoi(splitted[3]);
  336. LEDColor = Rgb{_r, _g, _b};
  337. }
  338. if (toUpper(splitted[0]) == "LEDTYPE")
  339. {
  340. if (splitted[1] == "WS2812")
  341. LEDType = LED_WS2812;
  342. if (splitted[1] == "WS2812B")
  343. LEDType = LED_WS2812B;
  344. if (splitted[1] == "SK6812")
  345. LEDType = LED_SK6812;
  346. if (splitted[1] == "WS2813")
  347. LEDType = LED_WS2813;
  348. }
  349. }
  350. if (registerISR) {
  351. //install gpio isr service
  352. gpio_install_isr_service(ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM);
  353. }
  354. if (gpioExtLED > 0)
  355. {
  356. // LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Startsequence 06"); // Nremove
  357. // vTaskDelay( xDelay );
  358. // xDelay = 5000 / portTICK_PERIOD_MS;
  359. // ESP_LOGD(TAG, "main: sleep for: %ldms", (long) xDelay);
  360. // SmartLed leds( LED_WS2812, 2, GPIO_NUM_12, 0, DoubleBuffer );
  361. // leds[ 0 ] = Rgb{ 255, 0, 0 };
  362. // leds[ 1 ] = Rgb{ 255, 255, 255 };
  363. // leds.show();
  364. // SmartLed leds = new SmartLed(LEDType, LEDNumbers, gpioExtLED, 0, DoubleBuffer);
  365. // _SmartLED = new SmartLed( LED_WS2812, 2, GPIO_NUM_12, 0, DoubleBuffer );
  366. }
  367. return true;
  368. }
  369. void GpioHandler::clear()
  370. {
  371. ESP_LOGD(TAG, "GpioHandler::clear");
  372. if (gpioMap != NULL) {
  373. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
  374. delete it->second;
  375. }
  376. gpioMap->clear();
  377. }
  378. // gpio_uninstall_isr_service(); can't uninstall, isr service is used by camera
  379. }
  380. void GpioHandler::registerGpioUri()
  381. {
  382. ESP_LOGI(TAG, "server_GPIO - Registering URI handlers");
  383. httpd_uri_t camuri = { };
  384. camuri.method = HTTP_GET;
  385. camuri.uri = "/GPIO";
  386. camuri.handler = APPLY_BASIC_AUTH_FILTER(callHandleHttpRequest);
  387. camuri.user_ctx = (void*)this;
  388. httpd_register_uri_handler(_httpServer, &camuri);
  389. }
  390. esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
  391. {
  392. ESP_LOGD(TAG, "handleHttpRequest");
  393. if (gpioMap == NULL) {
  394. std::string resp_str = "GPIO handler not initialized";
  395. httpd_resp_send(req, resp_str.c_str(), resp_str.length());
  396. return ESP_OK;
  397. }
  398. #ifdef DEBUG_DETAIL_ON
  399. LogFile.WriteHeapInfo("handler_switch_GPIO - Start");
  400. #endif
  401. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "handler_switch_GPIO");
  402. char _query[200];
  403. char _valueGPIO[30];
  404. char _valueStatus[30];
  405. std::string gpio, status;
  406. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK) {
  407. ESP_LOGD(TAG, "Query: %s", _query);
  408. if (httpd_query_key_value(_query, "GPIO", _valueGPIO, 30) == ESP_OK)
  409. {
  410. ESP_LOGD(TAG, "GPIO is found %s", _valueGPIO);
  411. gpio = std::string(_valueGPIO);
  412. } else {
  413. std::string resp_str = "GPIO No is not defined";
  414. httpd_resp_send(req, resp_str.c_str(), resp_str.length());
  415. return ESP_OK;
  416. }
  417. if (httpd_query_key_value(_query, "Status", _valueStatus, 30) == ESP_OK)
  418. {
  419. ESP_LOGD(TAG, "Status is found %s", _valueStatus);
  420. status = std::string(_valueStatus);
  421. }
  422. } else {
  423. const char* resp_str = "Error in call. Use /GPIO?GPIO=12&Status=high";
  424. httpd_resp_send(req, resp_str, strlen(resp_str));
  425. return ESP_OK;
  426. }
  427. status = toUpper(status);
  428. if ((status != "HIGH") && (status != "LOW") && (status != "TRUE") && (status != "FALSE") && (status != "0") && (status != "1") && (status != ""))
  429. {
  430. std::string zw = "Status not valid: " + status;
  431. httpd_resp_sendstr_chunk(req, zw.c_str());
  432. httpd_resp_sendstr_chunk(req, NULL);
  433. return ESP_OK;
  434. }
  435. int gpionum = stoi(gpio);
  436. // frei: 16; 12-15; 2; 4 // nur 12 und 13 funktionieren 2: reboot, 4: BlitzLED, 15: PSRAM, 14/15: DMA für SDKarte ???
  437. gpio_num_t gpio_num = resolvePinNr(gpionum);
  438. if (gpio_num == GPIO_NUM_NC)
  439. {
  440. std::string zw = "GPIO" + std::to_string(gpionum) + " unsupported - only 12 & 13 free";
  441. httpd_resp_sendstr_chunk(req, zw.c_str());
  442. httpd_resp_sendstr_chunk(req, NULL);
  443. return ESP_OK;
  444. }
  445. if (gpioMap->count(gpio_num) == 0) {
  446. char resp_str [30];
  447. sprintf(resp_str, "GPIO%d is not registred", gpio_num);
  448. httpd_resp_send(req, resp_str, strlen(resp_str));
  449. return ESP_OK;
  450. }
  451. if (status == "")
  452. {
  453. std::string resp_str = "";
  454. status = (*gpioMap)[gpio_num]->getValue(&resp_str) ? "HIGH" : "LOW";
  455. if (resp_str == "") {
  456. resp_str = status;
  457. }
  458. httpd_resp_sendstr_chunk(req, resp_str.c_str());
  459. httpd_resp_sendstr_chunk(req, NULL);
  460. }
  461. else
  462. {
  463. std::string resp_str = "";
  464. (*gpioMap)[gpio_num]->setValue((status == "HIGH") || (status == "TRUE") || (status == "1"), GPIO_SET_SOURCE_HTTP, &resp_str);
  465. if (resp_str == "") {
  466. resp_str = "GPIO" + std::to_string(gpionum) + " switched to " + status;
  467. }
  468. httpd_resp_sendstr_chunk(req, resp_str.c_str());
  469. httpd_resp_sendstr_chunk(req, NULL);
  470. }
  471. return ESP_OK;
  472. };
  473. void GpioHandler::flashLightEnable(bool value)
  474. {
  475. ESP_LOGD(TAG, "GpioHandler::flashLightEnable %s", value ? "true" : "false");
  476. if (gpioMap != NULL) {
  477. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it)
  478. {
  479. 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))
  480. {
  481. std::string resp_str = "";
  482. it->second->setValue(value, GPIO_SET_SOURCE_INTERNAL, &resp_str);
  483. if (resp_str == "") {
  484. ESP_LOGD(TAG, "Flash light pin GPIO %d switched to %s", (int)it->first, (value ? "on" : "off"));
  485. } else {
  486. ESP_LOGE(TAG, "Can't set flash light pin GPIO %d. Error: %s", (int)it->first, resp_str.c_str());
  487. }
  488. } else
  489. {
  490. if (it->second->getMode() == GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X)
  491. {
  492. #ifdef __LEDGLOBAL
  493. if (leds_global == NULL) {
  494. ESP_LOGI(TAG, "init SmartLed: LEDNumber=%d, GPIO=%d", LEDNumbers, (int)it->second->getGPIO());
  495. leds_global = new SmartLed( LEDType, LEDNumbers, it->second->getGPIO(), 0, DoubleBuffer );
  496. } else {
  497. // wait until we can update: https://github.com/RoboticsBrno/SmartLeds/issues/10#issuecomment-386921623
  498. leds_global->wait();
  499. }
  500. #else
  501. SmartLed leds( LEDType, LEDNumbers, it->second->getGPIO(), 0, DoubleBuffer );
  502. #endif
  503. if (value)
  504. {
  505. for (int i = 0; i < LEDNumbers; ++i)
  506. #ifdef __LEDGLOBAL
  507. (*leds_global)[i] = LEDColor;
  508. #else
  509. leds[i] = LEDColor;
  510. #endif
  511. }
  512. else
  513. {
  514. for (int i = 0; i < LEDNumbers; ++i)
  515. #ifdef __LEDGLOBAL
  516. (*leds_global)[i] = Rgb{0, 0, 0};
  517. #else
  518. leds[i] = Rgb{0, 0, 0};
  519. #endif
  520. }
  521. #ifdef __LEDGLOBAL
  522. leds_global->show();
  523. #else
  524. leds.show();
  525. #endif
  526. }
  527. }
  528. }
  529. }
  530. }
  531. gpio_num_t GpioHandler::resolvePinNr(uint8_t pinNr)
  532. {
  533. switch(pinNr) {
  534. case 0:
  535. return GPIO_NUM_0;
  536. case 1:
  537. return GPIO_NUM_1;
  538. case 3:
  539. return GPIO_NUM_3;
  540. case 4:
  541. return GPIO_NUM_4;
  542. case 12:
  543. return GPIO_NUM_12;
  544. case 13:
  545. return GPIO_NUM_13;
  546. default:
  547. return GPIO_NUM_NC;
  548. }
  549. }
  550. gpio_pin_mode_t GpioHandler::resolvePinMode(std::string input)
  551. {
  552. if( input == "disabled" ) return GPIO_PIN_MODE_DISABLED;
  553. if( input == "input" ) return GPIO_PIN_MODE_INPUT;
  554. if( input == "input-pullup" ) return GPIO_PIN_MODE_INPUT_PULLUP;
  555. if( input == "input-pulldown" ) return GPIO_PIN_MODE_INPUT_PULLDOWN;
  556. if( input == "output" ) return GPIO_PIN_MODE_OUTPUT;
  557. if( input == "built-in-led" ) return GPIO_PIN_MODE_BUILT_IN_FLASH_LED;
  558. if( input == "output-pwm" ) return GPIO_PIN_MODE_OUTPUT_PWM;
  559. if( input == "external-flash-pwm" ) return GPIO_PIN_MODE_EXTERNAL_FLASH_PWM;
  560. if( input == "external-flash-ws281x" ) return GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X;
  561. return GPIO_PIN_MODE_DISABLED;
  562. }
  563. gpio_int_type_t GpioHandler::resolveIntType(std::string input)
  564. {
  565. if( input == "disabled" ) return GPIO_INTR_DISABLE;
  566. if( input == "rising-edge" ) return GPIO_INTR_POSEDGE;
  567. if( input == "falling-edge" ) return GPIO_INTR_NEGEDGE;
  568. if( input == "rising-and-falling" ) return GPIO_INTR_ANYEDGE ;
  569. if( input == "low-level-trigger" ) return GPIO_INTR_LOW_LEVEL;
  570. if( input == "high-level-trigger" ) return GPIO_INTR_HIGH_LEVEL;
  571. return GPIO_INTR_DISABLE;
  572. }
  573. static GpioHandler *gpioHandler = NULL;
  574. void gpio_handler_create(httpd_handle_t server)
  575. {
  576. if (gpioHandler == NULL)
  577. gpioHandler = new GpioHandler(CONFIG_FILE, server);
  578. }
  579. void gpio_handler_init()
  580. {
  581. if (gpioHandler != NULL) {
  582. gpioHandler->init();
  583. }
  584. }
  585. void gpio_handler_deinit() {
  586. if (gpioHandler != NULL) {
  587. gpioHandler->deinit();
  588. }
  589. }
  590. void gpio_handler_destroy()
  591. {
  592. if (gpioHandler != NULL) {
  593. gpio_handler_deinit();
  594. delete gpioHandler;
  595. gpioHandler = NULL;
  596. }
  597. }
  598. GpioHandler* gpio_handler_get()
  599. {
  600. return gpioHandler;
  601. }