accessors.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include "esp_log.h"
  9. #include "driver/gpio.h"
  10. #include "driver/i2c.h"
  11. #include "driver/spi_master.h"
  12. #include "platform_config.h"
  13. #include "accessors.h"
  14. #include "globdefs.h"
  15. #include "display.h"
  16. #include "display.h"
  17. #include "cJSON.h"
  18. #include "driver/gpio.h"
  19. #include "stdbool.h"
  20. #include "driver/adc.h"
  21. static const char *TAG = "services";
  22. static const char *i2c_name="I2C";
  23. static const char *spi_name="SPI";
  24. static cJSON * gpio_list=NULL;
  25. #define min(a,b) (((a) < (b)) ? (a) : (b))
  26. #ifndef QUOTE
  27. #define QUOTE(name) #name
  28. #endif
  29. #ifndef STR
  30. #define STR(macro) QUOTE(macro)
  31. #endif
  32. /****************************************************************************************
  33. *
  34. */
  35. esp_err_t config_i2c_set(const i2c_config_t * config, int port){
  36. int buffer_size=255;
  37. char * config_buffer=calloc(buffer_size,1);
  38. if(config_buffer) {
  39. snprintf(config_buffer,buffer_size,"scl=%u sda=%u speed=%u port=%u",config->scl_io_num,config->sda_io_num,config->master.clk_speed,port);
  40. ESP_LOGI(TAG,"Updating i2c configuration to %s",config_buffer);
  41. config_set_value(NVS_TYPE_STR, "i2c_config", config_buffer);
  42. free(config_buffer);
  43. }
  44. return ESP_OK;
  45. }
  46. /****************************************************************************************
  47. *
  48. */
  49. esp_err_t config_spi_set(const spi_bus_config_t * config, int host, int dc){
  50. int buffer_size=255;
  51. char * config_buffer=calloc(buffer_size,1);
  52. if(config_buffer) {
  53. snprintf(config_buffer,buffer_size,"data=%u,clk=%u,dc=%u,host=%u",config->mosi_io_num,config->sclk_io_num,dc,host);
  54. ESP_LOGI(TAG,"Updating SPI configuration to %s",config_buffer);
  55. config_set_value(NVS_TYPE_STR, "spi_config", config_buffer);
  56. free(config_buffer);
  57. }
  58. return ESP_OK;
  59. }
  60. /****************************************************************************************
  61. *
  62. */
  63. const display_config_t * config_display_get(){
  64. static display_config_t dstruct;
  65. char *config = config_alloc_get(NVS_TYPE_STR, "display_config");
  66. if (!config) {
  67. return NULL;
  68. }
  69. char * p=NULL;
  70. if ((p = strcasestr(config, "driver")) != NULL){
  71. dstruct.drivername = display_conf_get_driver_name(strchr(p, '=') + 1);
  72. }
  73. dstruct.drivername=dstruct.drivername?dstruct.drivername:"SSD1306";
  74. if ((p = strcasestr(config, "width")) != NULL) dstruct.width = atoi(strchr(p, '=') + 1);
  75. if ((p = strcasestr(config, "height")) != NULL) dstruct.height = atoi(strchr(p, '=') + 1);
  76. if ((p = strcasestr(config, "reset")) != NULL) dstruct.RST_pin = atoi(strchr(p, '=') + 1);
  77. dstruct.i2c_system_port=i2c_system_port;
  78. if (strstr(config, "I2C") ) dstruct.type=i2c_name;
  79. if ((p = strcasestr(config, "address")) != NULL) dstruct.address = atoi(strchr(p, '=') + 1);
  80. if (strstr(config, "SPI") ) dstruct.type=spi_name;
  81. if ((p = strcasestr(config, "cs")) != NULL) dstruct.CS_pin = atoi(strchr(p, '=') + 1);
  82. if ((p = strcasestr(config, "speed")) != NULL) dstruct.speed = atoi(strchr(p, '=') + 1);
  83. dstruct.hflip= strcasestr(config, "HFlip") ? true : false;
  84. dstruct.vflip= strcasestr(config, "VFlip") ? true : false;
  85. dstruct.rotate= strcasestr(config, "rotate") ? true : false;
  86. return &dstruct;
  87. }
  88. /****************************************************************************************
  89. *
  90. */
  91. const i2c_config_t * config_i2c_get(int * i2c_port) {
  92. char *nvs_item, *p;
  93. static i2c_config_t i2c = {
  94. .mode = I2C_MODE_MASTER,
  95. .sda_io_num = -1,
  96. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  97. .scl_io_num = -1,
  98. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  99. .master.clk_speed = 0,
  100. };
  101. i2c.master.clk_speed = i2c_system_speed;
  102. nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config");
  103. if (nvs_item) {
  104. if ((p = strcasestr(nvs_item, "scl")) != NULL) i2c.scl_io_num = atoi(strchr(p, '=') + 1);
  105. if ((p = strcasestr(nvs_item, "sda")) != NULL) i2c.sda_io_num = atoi(strchr(p, '=') + 1);
  106. if ((p = strcasestr(nvs_item, "speed")) != NULL) i2c.master.clk_speed = atoi(strchr(p, '=') + 1);
  107. if ((p = strcasestr(nvs_item, "port")) != NULL) i2c_system_port = atoi(strchr(p, '=') + 1);
  108. free(nvs_item);
  109. }
  110. if(i2c_port) *i2c_port=i2c_system_port;
  111. return &i2c;
  112. }
  113. /****************************************************************************************
  114. *
  115. */
  116. const spi_bus_config_t * config_spi_get(spi_host_device_t * spi_host) {
  117. char *nvs_item, *p;
  118. static spi_bus_config_t spi = {
  119. .mosi_io_num = -1,
  120. .sclk_io_num = -1,
  121. .miso_io_num = -1,
  122. .quadwp_io_num = -1,
  123. .quadhd_io_num = -1
  124. };
  125. nvs_item = config_alloc_get_str("spi_config", CONFIG_SPI_CONFIG, NULL);
  126. if (nvs_item) {
  127. if ((p = strcasestr(nvs_item, "data")) != NULL) spi.mosi_io_num = atoi(strchr(p, '=') + 1);
  128. if ((p = strcasestr(nvs_item, "clk")) != NULL) spi.sclk_io_num = atoi(strchr(p, '=') + 1);
  129. if ((p = strcasestr(nvs_item, "dc")) != NULL) spi_system_dc_gpio = atoi(strchr(p, '=') + 1);
  130. if ((p = strcasestr(nvs_item, "host")) != NULL) spi_system_host = atoi(strchr(p, '=') + 1);
  131. free(nvs_item);
  132. }
  133. if(spi_host) *spi_host = spi_system_host;
  134. return &spi;
  135. }
  136. /****************************************************************************************
  137. *
  138. */
  139. void parse_set_GPIO(void (*cb)(int gpio, char *value)) {
  140. char *nvs_item, *p, type[16];
  141. int gpio;
  142. if ((nvs_item = config_alloc_get(NVS_TYPE_STR, "set_GPIO")) == NULL) return;
  143. p = nvs_item;
  144. do {
  145. if (sscanf(p, "%d=%15[^,]", &gpio, type) > 0) cb(gpio, type);
  146. p = strchr(p, ',');
  147. } while (p++);
  148. free(nvs_item);
  149. }
  150. /****************************************************************************************
  151. *
  152. */
  153. cJSON * get_gpio_entry(const char * name, const char * prefix, int gpio, bool fixed){
  154. cJSON * entry = cJSON_CreateObject();
  155. cJSON_AddNumberToObject(entry,"gpio",gpio);
  156. cJSON_AddStringToObject(entry,"name",name);
  157. cJSON_AddStringToObject(entry,"group",prefix);
  158. cJSON_AddBoolToObject(entry,"fixed",fixed);
  159. return entry;
  160. }
  161. /****************************************************************************************
  162. *
  163. */
  164. cJSON * get_GPIO_list() {
  165. cJSON * list = cJSON_CreateArray();
  166. char *nvs_item, *p, type[16];
  167. int gpio;
  168. if ((nvs_item = config_alloc_get(NVS_TYPE_STR, "set_GPIO")) == NULL) return list;
  169. p = nvs_item;
  170. do {
  171. if (sscanf(p, "%d=%15[^,]", &gpio, type) > 0 && (GPIO_IS_VALID_GPIO(gpio) || gpio==GPIO_NUM_NC)){
  172. cJSON_AddItemToArray(list,get_gpio_entry(type,"gpio", gpio, false));
  173. }
  174. p = strchr(p, ',');
  175. } while (p++);
  176. free(nvs_item);
  177. return list;
  178. }
  179. /****************************************************************************************
  180. *
  181. */
  182. cJSON * get_GPIO_from_string(const char * nvs_item, const char * prefix, cJSON * list, bool fixed){
  183. cJSON * llist = list;
  184. int gpio=0,offset=0,soffset=0,ret1=0,sret=0;
  185. if(!llist){
  186. llist = cJSON_CreateArray();
  187. }
  188. const char *p=NULL;
  189. char type[16];
  190. int slen=strlen(nvs_item)+1;
  191. char * buf1=malloc(slen);
  192. char * buf2=malloc(slen);
  193. ESP_LOGD(TAG,"Parsing string %s",nvs_item);
  194. p = strchr(nvs_item, ':');
  195. p=p?p+1:nvs_item;
  196. while((((ret1=sscanf(p, "%[^=]=%d%n", type,&gpio,&offset)) ==2) || ((sret=sscanf(p, "%[^=]=%[^, ],%n", buf1,buf2,&soffset)) > 0 )) && (offset || soffset)){
  197. if(ret1==2 && (GPIO_IS_VALID_GPIO(gpio) || gpio==GPIO_NUM_NC)){
  198. cJSON_AddItemToArray(list,get_gpio_entry(type,prefix,gpio,fixed));
  199. p+=offset;
  200. } else {
  201. p+=soffset;
  202. }
  203. while(*p==' ' || *p==',') p++;
  204. gpio=-1;
  205. }
  206. free(buf1);
  207. free(buf2);
  208. return llist;
  209. }
  210. /****************************************************************************************
  211. *
  212. */
  213. cJSON * get_GPIO_from_nvs(const char * item, const char * prefix, cJSON * list, bool fixed){
  214. char * nvs_item=NULL;
  215. cJSON * llist=list;
  216. if ((nvs_item = config_alloc_get(NVS_TYPE_STR, item)) == NULL) return list;
  217. llist = get_GPIO_from_string(nvs_item,prefix,list, fixed);
  218. free(nvs_item);
  219. return llist;
  220. }
  221. /****************************************************************************************
  222. *
  223. */
  224. esp_err_t get_gpio_structure(cJSON * gpio_entry, gpio_entry_t ** gpio){
  225. esp_err_t err = ESP_OK;
  226. *gpio = malloc(sizeof(gpio_entry_t));
  227. //gpio,name,fixed
  228. cJSON * val = cJSON_GetObjectItem(gpio_entry,"gpio");
  229. if(val){
  230. (*gpio)->gpio= (int)val->valuedouble;
  231. } else {
  232. ESP_LOGE(TAG,"gpio pin not found");
  233. err=ESP_FAIL;
  234. }
  235. val = cJSON_GetObjectItem(gpio_entry,"name");
  236. if(val){
  237. (*gpio)->name= strdup(cJSON_GetStringValue(val));
  238. } else {
  239. ESP_LOGE(TAG,"gpio name value not found");
  240. err=ESP_FAIL;
  241. }
  242. val = cJSON_GetObjectItem(gpio_entry,"group");
  243. if(val){
  244. (*gpio)->group= strdup(cJSON_GetStringValue(val));
  245. } else {
  246. ESP_LOGE(TAG,"gpio group value not found");
  247. err=ESP_FAIL;
  248. }
  249. val = cJSON_GetObjectItem(gpio_entry,"fixed");
  250. if(val){
  251. (*gpio)->fixed= cJSON_IsTrue(val);
  252. } else {
  253. ESP_LOGE(TAG,"gpio fixed indicator not found");
  254. err=ESP_FAIL;
  255. }
  256. return err;
  257. }
  258. /****************************************************************************************
  259. *
  260. */
  261. esp_err_t free_gpio_entry( gpio_entry_t ** gpio) {
  262. if(* gpio){
  263. free((* gpio)->name);
  264. free((* gpio)->group);
  265. free(* gpio);
  266. * gpio=NULL;
  267. return ESP_OK;
  268. }
  269. return ESP_FAIL;
  270. }
  271. /****************************************************************************************
  272. *
  273. */
  274. gpio_entry_t * get_gpio_by_no(int gpionum, bool refresh){
  275. cJSON * gpio_header=NULL;
  276. gpio_entry_t * gpio=NULL;
  277. if(refresh){
  278. get_gpio_list();
  279. }
  280. cJSON_ArrayForEach(gpio_header,gpio_list)
  281. {
  282. if(get_gpio_structure(gpio_header, &gpio)==ESP_OK && gpio->gpio==gpionum){
  283. ESP_LOGD(TAG,"Found GPIO: %s=%d %s", gpio->name,gpio->gpio,gpio->fixed?"(FIXED)":"(VARIABLE)");
  284. }
  285. }
  286. return gpio;
  287. }
  288. /****************************************************************************************
  289. *
  290. */
  291. gpio_entry_t * get_gpio_by_name(char * name,char * group, bool refresh){
  292. cJSON * gpio_header=NULL;
  293. if(refresh){
  294. get_gpio_list();
  295. }
  296. gpio_entry_t * gpio=NULL;
  297. cJSON_ArrayForEach(gpio_header,gpio_list)
  298. {
  299. if(get_gpio_structure(gpio_header, &gpio)==ESP_OK && strcasecmp(gpio->name,name)&& strcasecmp(gpio->group,group)){
  300. ESP_LOGD(TAG,"Found GPIO: %s=%d %s", gpio->name,gpio->gpio,gpio->fixed?"(FIXED)":"(VARIABLE)");
  301. }
  302. }
  303. return gpio;
  304. }
  305. /****************************************************************************************
  306. *
  307. */
  308. cJSON * get_gpio_list() {
  309. gpio_num_t gpio_num;
  310. if(gpio_list){
  311. cJSON_free(gpio_list);
  312. }
  313. gpio_list = get_GPIO_list();
  314. #ifndef CONFIG_BAT_LOCKED
  315. char *bat_config = config_alloc_get_default(NVS_TYPE_STR, "bat_config", NULL, 0);
  316. if (bat_config) {
  317. char *p;
  318. int channel;
  319. if ((p = strcasestr(bat_config, "channel") ) != NULL) {
  320. channel = atoi(strchr(p, '=') + 1);
  321. if(channel != -1){
  322. if(adc1_pad_get_io_num(channel,&gpio_num )==ESP_OK){
  323. cJSON_AddItemToArray(gpio_list,get_gpio_entry("bat","",gpio_num,false));
  324. }
  325. }
  326. }
  327. free(bat_config);
  328. }
  329. #else
  330. if(adc1_pad_get_io_num(CONFIG_BAT_CHANNEL,&gpio_num )==ESP_OK){
  331. cJSON_AddItemToArray(gpio_list,get_gpio_entry("bat","",gpio_num,true));
  332. }
  333. #endif
  334. gpio_list = get_GPIO_from_nvs("i2c_config","i2c", gpio_list, false);
  335. gpio_list = get_GPIO_from_nvs("spi_config","spi", gpio_list, false);
  336. char *spdif_config = config_alloc_get_str("spdif_config", CONFIG_SPDIF_CONFIG, "bck=" STR(CONFIG_SPDIF_BCK_IO)
  337. ",ws=" STR(CONFIG_SPDIF_WS_IO) ",do=" STR(CONFIG_SPDIF_DO_IO));
  338. gpio_list=get_GPIO_from_string(spdif_config,"spdif", gpio_list, (strlen(CONFIG_SPDIF_CONFIG)>0 || CONFIG_SPDIF_DO_IO>0 ));
  339. char *dac_config = config_alloc_get_str("dac_config", CONFIG_DAC_CONFIG, "model=i2s,bck=" STR(CONFIG_I2S_BCK_IO)
  340. ",ws=" STR(CONFIG_I2S_WS_IO) ",do=" STR(CONFIG_I2S_DO_IO)
  341. ",sda=" STR(CONFIG_I2C_SDA) ",scl=" STR(CONFIG_I2C_SCL)
  342. ",mute=" STR(CONFIG_MUTE_GPIO));
  343. gpio_list=get_GPIO_from_string(dac_config,"dac", gpio_list, (strlen(CONFIG_DAC_CONFIG)>0 || CONFIG_I2S_DO_IO>0 ));
  344. free(spdif_config);
  345. free(dac_config);
  346. return gpio_list;
  347. }