server_GPIO.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include <string>
  2. #include "string.h"
  3. #include <string.h>
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "esp_system.h"
  7. #include "esp_event.h"
  8. #include "esp_log.h"
  9. #include "driver/gpio.h"
  10. //#include "errno.h"
  11. #include <sys/stat.h>
  12. #include <vector>
  13. //#include <regex>
  14. #include "server_GPIO.h"
  15. #include "ClassLogFile.h"
  16. #include "configFile.h"
  17. #include "Helper.h"
  18. // #define DEBUG_DETAIL_ON
  19. GpioPin::GpioPin(gpio_num_t gpio, const char* name, gpio_pin_mode_t mode, gpio_int_type_t interruptType)
  20. {
  21. _gpio = gpio;
  22. _name = name;
  23. _mode = mode;
  24. _interruptType = interruptType;
  25. initGPIO();
  26. }
  27. void GpioPin::initGPIO()
  28. {
  29. gpio_config_t io_conf;
  30. //disable interrupt
  31. io_conf.intr_type = _interruptType;
  32. //set as output mode
  33. io_conf.mode = _mode == gpio_pin_mode_t::GPIO_PIN_MODE_OUTPUT ? gpio_mode_t::GPIO_MODE_OUTPUT : gpio_mode_t::GPIO_MODE_INPUT;
  34. //bit mask of the pins that you want to set,e.g.GPIO18/19
  35. io_conf.pin_bit_mask = (1ULL << _gpio);
  36. //disable pull-down mode
  37. io_conf.pull_down_en = _mode == gpio_pin_mode_t::GPIO_PIN_MODE_INPUT_PULLDOWN ? gpio_pulldown_t::GPIO_PULLDOWN_ENABLE : gpio_pulldown_t::GPIO_PULLDOWN_DISABLE;
  38. //disable pull-up mode
  39. io_conf.pull_up_en = _mode == gpio_pin_mode_t::GPIO_PIN_MODE_INPUT_PULLDOWN ? gpio_pullup_t::GPIO_PULLUP_ENABLE : gpio_pullup_t::GPIO_PULLUP_DISABLE;
  40. //configure GPIO with the given settings
  41. gpio_config(&io_conf);
  42. }
  43. void GpioPin::setValue(bool value)
  44. {
  45. gpio_set_level(_gpio, value);
  46. }
  47. esp_err_t callHandleHttpRequest(httpd_req_t *req)
  48. {
  49. return ((GpioHandler*)req->user_ctx)->handleHttpRequest(req);
  50. }
  51. GpioHandler::GpioHandler(std::string configFile, httpd_handle_t server)
  52. {
  53. _configFile = configFile;
  54. gpioMap = new std::map<gpio_num_t, GpioPin*>();
  55. readConfig();
  56. registerGpioUri(server);
  57. }
  58. bool GpioHandler::readConfig()
  59. {
  60. ConfigFile configFile(_configFile);
  61. std::vector<std::string> zerlegt;
  62. std::string line = "";
  63. bool disabledLine = false;
  64. bool eof = false;
  65. while ((!configFile.GetNextParagraph(line, disabledLine, eof) || (line.compare("[GPIO]") != 0)) && !disabledLine && !eof) {}
  66. if (eof)
  67. return false;
  68. while (configFile.getNextLine(&line, disabledLine, eof) && !configFile.isNewParagraph(line))
  69. {
  70. zerlegt = configFile.ZerlegeZeile(line);
  71. // const std::regex pieces_regex("IO([0-9]{1,2})");
  72. // std::smatch pieces_match;
  73. // if (std::regex_match(zerlegt[0], pieces_match, pieces_regex) && (pieces_match.size() == 2))
  74. // {
  75. // std::string gpioStr = pieces_match[1];
  76. if (zerlegt[0].rfind("IO", 0) == 0)
  77. {
  78. std::string gpioStr = zerlegt[0].substr(2, 2);
  79. gpio_num_t gpioNr = (gpio_num_t)atoi(gpioStr.c_str());
  80. gpio_pin_mode_t pinMode = resolvePinMode(zerlegt[1]);
  81. gpio_int_type_t intType = resolveIntType(zerlegt[2]);
  82. (*gpioMap)[gpio_num_t::GPIO_NUM_16] = new GpioPin(gpioNr, zerlegt[3].c_str(), pinMode, intType);
  83. }
  84. }
  85. return true;
  86. }
  87. void GpioHandler::registerGpioUri(httpd_handle_t server) {
  88. ESP_LOGI(TAGPARTGPIO, "server_GPIO - Registering URI handlers");
  89. httpd_uri_t camuri = { };
  90. camuri.method = HTTP_GET;
  91. camuri.uri = "/GPIO";
  92. camuri.handler = callHandleHttpRequest;
  93. camuri.user_ctx = (void*) this;
  94. httpd_register_uri_handler(server, &camuri);
  95. }
  96. IRAM_ATTR esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
  97. {
  98. #ifdef DEBUG_DETAIL_ON
  99. LogFile.WriteHeapInfo("handler_switch_GPIO - Start");
  100. #endif
  101. LogFile.WriteToFile("handler_switch_GPIO");
  102. char _query[200];
  103. char _valueGPIO[30];
  104. char _valueStatus[30];
  105. std::string gpio, status, zw;
  106. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  107. {
  108. printf("Query: "); printf(_query); printf("\n");
  109. if (httpd_query_key_value(_query, "GPIO", _valueGPIO, 30) == ESP_OK)
  110. {
  111. printf("GPIO is found"); printf(_valueGPIO); printf("\n");
  112. gpio = std::string(_valueGPIO);
  113. }
  114. if (httpd_query_key_value(_query, "Status", _valueStatus, 30) == ESP_OK)
  115. {
  116. printf("Status is found"); printf(_valueStatus); printf("\n");
  117. status = std::string(_valueStatus);
  118. }
  119. };
  120. status = toUpper(status);
  121. if (!(status == "HIGH") && !(status == "LOW"))
  122. {
  123. zw = "Status not valid: " + status;;
  124. httpd_resp_sendstr_chunk(req, zw.c_str());
  125. httpd_resp_sendstr_chunk(req, NULL);
  126. return ESP_OK;
  127. }
  128. int gpionum = stoi(gpio);
  129. // frei: 16; 12-15; 2; 4 // nur 12 und 13 funktionieren 2: reboot, 4: BlitzLED, 14/15: DMA für SDKarte ???
  130. gpio_num_t gpio_num = resolvePinNr(gpionum);
  131. if (gpio_num == GPIO_NUM_NC)
  132. {
  133. zw = "GPIO" + std::to_string(gpionum) + " not support - only 12 & 13 free";
  134. httpd_resp_sendstr_chunk(req, zw.c_str());
  135. httpd_resp_sendstr_chunk(req, NULL);
  136. return ESP_OK;
  137. }
  138. (*gpioMap)[gpio_num]->setValue((status == "HIGH") || (status == "TRUE") || (status == "1"));
  139. zw = "GPIO" + std::to_string(gpionum) + " switched to " + status;
  140. httpd_resp_sendstr_chunk(req, zw.c_str());
  141. httpd_resp_sendstr_chunk(req, NULL);
  142. return ESP_OK;
  143. };
  144. gpio_num_t GpioHandler::resolvePinNr(uint8_t pinNr)
  145. {
  146. switch(pinNr) {
  147. case 0:
  148. return GPIO_NUM_0;
  149. case 4:
  150. return GPIO_NUM_4;
  151. case 12:
  152. return GPIO_NUM_12;
  153. case 13:
  154. return GPIO_NUM_13;
  155. case 16:
  156. return GPIO_NUM_16;
  157. default:
  158. return GPIO_NUM_NC;
  159. }
  160. }
  161. gpio_pin_mode_t GpioHandler::resolvePinMode(std::string input)
  162. {
  163. if( input == "disabled" ) return GPIO_PIN_MODE_DISABLED;
  164. if( input == "input" ) return GPIO_PIN_MODE_INPUT;
  165. if( input == "input-pullup" ) return GPIO_PIN_MODE_INPUT_PULLUP;
  166. if( input == "input-pulldown" ) return GPIO_PIN_MODE_INPUT_PULLDOWN;
  167. if( input == "input-output" ) return GPIO_PIN_MODE_OUTPUT;
  168. if( input == "input-output-pwm" ) return GPIO_PIN_MODE_OUTPUT_PWM;
  169. return GPIO_PIN_MODE_DISABLED;
  170. }
  171. gpio_int_type_t GpioHandler::resolveIntType(std::string input)
  172. {
  173. if( input == "disabled" ) return GPIO_INTR_DISABLE;
  174. if( input == "rising-edge" ) return GPIO_INTR_POSEDGE;
  175. if( input == "falling-edge" ) return GPIO_INTR_NEGEDGE;
  176. if( input == "rising-and-falling" ) return GPIO_INTR_ANYEDGE ;
  177. if( input == "low-level-trigger" ) return GPIO_INTR_LOW_LEVEL;
  178. if( input == "high-level-trigger" ) return GPIO_INTR_HIGH_LEVEL;
  179. return GPIO_INTR_DISABLE;
  180. }