server_GPIO.cpp 23 KB

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