Advertisement
Guest User

Just GUI and relais switching

a guest
Oct 19th, 2019
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.02 KB | None | 0 0
  1. #include <lvgl.h>
  2. #include <Ticker.h>
  3. #include <TFT_eSPI.h>
  4. //#include "lv_test_theme_1.h"
  5. #define LVGL_TICK_PERIOD 60
  6. //const int relaisPin = 13;
  7. Ticker tick; /* timer for interrupt handler */
  8. TFT_eSPI tft = TFT_eSPI(); /* TFT instance */
  9. static lv_disp_buf_t disp_buf;
  10. static lv_color_t buf[LV_HOR_RES_MAX * 10];
  11. static void create_tab1(lv_obj_t * parent);
  12. static void create_tab2(lv_obj_t * parent);
  13. static void create_tab3(lv_obj_t * parent);
  14. lv_obj_t * slider_label;
  15. int screenWidth = 480;
  16. int screenHeight = 320;
  17.  
  18. const int relaisPin = 13; //relais 3.3v dispaly + touch, bme,hx711
  19. const int relaisPin1 = 02; //relais gsm 4.1v
  20. const int relaisPin2 = 15; //relais 5v hx711
  21.  
  22.  
  23.  
  24. #if USE_LV_LOG != 1
  25. /* Serial debugging */
  26. void my_print(lv_log_level_t level, const char * file, uint32_t line, const char * dsc)
  27. {
  28.  
  29. Serial.printf("%s@%d->%s\r\n", file, line, dsc);
  30. delay(100);
  31. }
  32. #endif
  33.  
  34. /* Display flushing */
  35. void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
  36. {
  37. uint16_t c;
  38.  
  39. tft.startWrite(); /* Start new TFT transaction */
  40. tft.setAddrWindow(area->x1, area->y1, (area->x2 - area->x1 + 1), (area->y2 - area->y1 + 1)); /* set the working window */
  41. for (int y = area->y1; y <= area->y2; y++) {
  42. for (int x = area->x1; x <= area->x2; x++) {
  43. c = color_p->full;
  44. tft.writeColor(c, 1);
  45. color_p++;
  46. }
  47. }
  48. tft.endWrite(); /* terminate TFT transaction */
  49. lv_disp_flush_ready(disp); /* tell lvgl that flushing is done */
  50. }
  51.  
  52. bool my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data)
  53. {
  54. uint16_t touchX, touchY;
  55.  
  56. bool touched = tft.getTouch(&touchX, &touchY, 600);
  57.  
  58. if(!touched)
  59. {
  60. return false;
  61. }
  62.  
  63. if(touchX>screenWidth || touchY > screenHeight)
  64. {
  65. Serial.println("Y or y outside of expected parameters..");
  66. Serial.print("y:");
  67. Serial.print(touchX);
  68. Serial.print(" x:");
  69. Serial.print(touchY);
  70. }
  71. else
  72. {
  73.  
  74. data->state = touched ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
  75.  
  76. /*Save the state and save the pressed coordinate*/
  77. //if(data->state == LV_INDEV_STATE_PR) touchpad_get_xy(&last_x, &last_y);
  78.  
  79. /*Set the coordinates (if released use the last pressed coordinates)*/
  80. data->point.x = touchX;
  81. data->point.y = touchY;
  82.  
  83. Serial.print("Data x");
  84. Serial.println(touchX);
  85.  
  86. Serial.print("Data y");
  87. Serial.println(touchY);
  88.  
  89. }
  90.  
  91. return false; /*Return `false` because we are not buffering and no more data to read*/
  92. }
  93.  
  94. /* Interrupt driven periodic handler */
  95. static void lv_tick_handler(void)
  96. {
  97.  
  98. lv_tick_inc(LVGL_TICK_PERIOD);
  99. }
  100.  
  101. void setup() {
  102. pinMode (relaisPin, OUTPUT);
  103. digitalWrite (relaisPin, HIGH);
  104. delay(1000);
  105. pinMode (relaisPin2, OUTPUT);
  106. digitalWrite (relaisPin2, HIGH);
  107. delay(1000);
  108. pinMode (relaisPin1, OUTPUT);
  109. digitalWrite (relaisPin1, HIGH);
  110. delay(1000);
  111. pinMode (4, OUTPUT);
  112. digitalWrite (4, HIGH);
  113.  
  114. ledcSetup(10, 5000/*freq*/, 10 /*resolution*/);
  115. ledcAttachPin(32, 10);
  116. analogReadResolution(10);
  117. ledcWrite(10,768);
  118. //const char test = '9' ;
  119. #define MAX_STRING_SIZE 5
  120. float test = 52.4;
  121. char test2[MAX_STRING_SIZE];
  122. snprintf(test2, MAX_STRING_SIZE, "%f", test);
  123.  
  124.  
  125.  
  126.  
  127. Serial.begin(115200); /* prepare for possible serial debug */
  128.  
  129. lv_init();
  130.  
  131. #if USE_LV_LOG != 0
  132. lv_log_register_print(my_print); /* register print function for debugging */
  133. #endif
  134.  
  135. tft.begin(); /* TFT init */
  136. tft.setRotation(1);
  137.  
  138. uint16_t calData[5] = { 275, 3620, 264, 3532, 1 };
  139. tft.setTouch(calData);
  140.  
  141. lv_disp_buf_init(&disp_buf, buf, NULL, LV_HOR_RES_MAX * 10);
  142.  
  143. /*Initialize the display*/
  144. lv_disp_drv_t disp_drv;
  145. lv_disp_drv_init(&disp_drv);
  146. disp_drv.hor_res = 320;
  147. disp_drv.ver_res = 240;
  148. disp_drv.flush_cb = my_disp_flush;
  149. disp_drv.buffer = &disp_buf;
  150. lv_disp_drv_register(&disp_drv);
  151.  
  152. lv_indev_drv_t indev_drv;
  153. lv_indev_drv_init(&indev_drv); /*Descriptor of a input device driver*/
  154. indev_drv.type = LV_INDEV_TYPE_POINTER; /*Touch pad is a pointer-like device*/
  155. indev_drv.read_cb = my_touchpad_read; /*Set your driver function*/
  156. lv_indev_drv_register(&indev_drv); /*Finally register the driver*/
  157.  
  158.  
  159. /*Initialize the touch pad*/
  160. //lv_indev_drv_t indev_drv;
  161. //lv_indev_drv_init(&indev_drv);
  162. //indev_drv.type = LV_INDEV_TYPE_ENCODER;
  163. //indev_drv.read_cb = read_encoder;
  164. //lv_indev_drv_register(&indev_drv);
  165.  
  166. /*Initialize the graphics library's tick*/
  167. tick.attach_ms(LVGL_TICK_PERIOD, lv_tick_handler);
  168.  
  169. //Set the theme..
  170. lv_theme_t * th = lv_theme_material_init(210, NULL); //Set a HUE value and a Font for the Night Theme
  171. lv_theme_set_current(th);
  172.  
  173. lv_obj_t * scr = lv_cont_create(NULL, NULL);
  174. lv_disp_load_scr(scr);
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. /*Create a Tab view object*/
  183. lv_obj_t *tabview;
  184. tabview = lv_tabview_create(lv_scr_act(), NULL);
  185.  
  186. /*Add 3 tabs (the tabs are page (lv_page) and can be scrolled*/
  187. lv_obj_t *tab1 = lv_tabview_add_tab(tabview, "Uebersicht");
  188. lv_obj_t *tab2 = lv_tabview_add_tab(tabview, "Visualisierung");
  189. lv_obj_t *tab3 = lv_tabview_add_tab(tabview, "Einstellungen");
  190.  
  191.  
  192.  
  193.  
  194. /*Create a normal cell style*/
  195. static lv_style_t style_cell1;
  196. lv_style_copy(&style_cell1, &lv_style_plain);
  197. style_cell1.body.border.width = 2;
  198. style_cell1.body.border.color = LV_COLOR_BLACK;
  199.  
  200. /*Crealte a header cell style*/
  201. static lv_style_t style_cell2;
  202. lv_style_copy(&style_cell2, &lv_style_plain);
  203. style_cell2.body.border.width = 2;
  204. style_cell2.body.border.color = LV_COLOR_BLACK;
  205. style_cell2.body.main_color = LV_COLOR_SILVER;
  206. style_cell2.body.grad_color = LV_COLOR_SILVER;
  207.  
  208. lv_obj_t * table = lv_table_create(tab1, NULL);
  209. lv_table_set_style(table, LV_TABLE_STYLE_CELL1, &style_cell1);
  210. lv_table_set_style(table, LV_TABLE_STYLE_CELL2, &style_cell2);
  211. lv_table_set_style(table, LV_TABLE_STYLE_BG, &lv_style_transp_tight);
  212. lv_table_set_col_cnt(table, 2);
  213. lv_table_set_row_cnt(table, 5);
  214. lv_obj_align(table, NULL, LV_ALIGN_CENTER, -18, 0);
  215.  
  216. /*Make the cells of the first row center aligned */
  217. lv_table_set_cell_align(table, 0, 0, LV_LABEL_ALIGN_CENTER);
  218. lv_table_set_cell_align(table, 0, 1, LV_LABEL_ALIGN_CENTER);
  219.  
  220. /*Make the cells of the first row TYPE = 2 (use `style_cell2`) */
  221. lv_table_set_cell_type(table, 0, 0, 2);
  222. lv_table_set_cell_type(table, 0, 1, 2);
  223.  
  224.  
  225.  
  226. /*Fill the first column*/
  227. lv_table_set_cell_value(table, 0, 0, "Sensor");
  228. lv_table_set_cell_value(table, 1, 0, "Gewicht" );
  229. lv_table_set_cell_value(table, 2, 0, "Temperatur");
  230. lv_table_set_cell_value(table, 3, 0, "Luftfeuchtigkeit");
  231. lv_table_set_cell_value(table, 4, 0, "Luftdruck");
  232. /*Fill the second column*/
  233. lv_table_set_cell_value(table, 0, 1, "Wert");
  234. lv_table_set_cell_value(table, 1, 1, test2 );
  235. lv_table_set_cell_value(table, 2, 1, "0");
  236. lv_table_set_cell_value(table, 3, 1, "0");
  237. lv_table_set_cell_value(table, 4, 1, "0");
  238. //Width of cell
  239. lv_table_set_col_width(table, 0, 130);
  240. // Cell grows not with words
  241. lv_table_set_cell_crop(table, 3, 0, true);
  242.  
  243. /*Add content to the tabs*/
  244. lv_obj_t * label = lv_label_create(tab1, NULL);
  245. lv_label_set_text(label, "");
  246. label = lv_label_create(tab2, NULL);
  247. lv_label_set_text(label, "");
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254. label = lv_label_create(tab3, NULL);
  255. lv_label_set_text(label, "");
  256.  
  257.  
  258.  
  259. // lv_obj_t * label;
  260.  
  261. lv_obj_t * btn1 = lv_btn_create(tab3, NULL);
  262. lv_obj_set_event_cb(btn1, event_handler);
  263. lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, -40);
  264.  
  265. label = lv_label_create(btn1, NULL);
  266. lv_label_set_text(label, "Deepsleep");
  267.  
  268. lv_obj_t * btn2 = lv_btn_create(tab3, NULL);
  269. lv_obj_set_event_cb(btn2, event_handler);
  270. lv_obj_align(btn2, NULL, LV_ALIGN_CENTER, 0, 40);
  271. lv_btn_set_toggle(btn2, true);
  272. lv_btn_toggle(btn2);
  273. lv_btn_set_fit2(btn2, LV_FIT_NONE, LV_FIT_TIGHT);
  274.  
  275. label = lv_label_create(btn2, NULL);
  276. lv_label_set_text(label, "Handy Nr.");
  277.  
  278.  
  279.  
  280. lv_coord_t w = lv_page_get_scrl_width(tab2);
  281.  
  282. lv_obj_t * chart = lv_chart_create(tab2, NULL);
  283. lv_chart_set_type(chart, LV_CHART_TYPE_AREA);
  284. lv_obj_set_size(chart, w / 1.5, lv_disp_get_ver_res(NULL) / 1.5);
  285. lv_obj_set_pos(chart, LV_DPI / 10, LV_DPI / 10);
  286. lv_chart_series_t * s1 = lv_chart_add_series(chart, LV_COLOR_RED);
  287. lv_chart_set_next(chart, s1, 30);
  288. lv_chart_set_next(chart, s1, 20);
  289. lv_chart_set_next(chart, s1, 10);
  290. lv_chart_set_next(chart, s1, 12);
  291. lv_chart_set_next(chart, s1, 20);
  292. lv_chart_set_next(chart, s1, 27);
  293. lv_chart_set_next(chart, s1, 35);
  294. lv_chart_set_next(chart, s1, 55);
  295. lv_chart_set_next(chart, s1, 70);
  296. lv_chart_set_next(chart, s1, test);
  297.  
  298.  
  299.  
  300.  
  301. }
  302.  
  303. void loop() {
  304.  
  305. lv_task_handler(); /* let the GUI do its work */
  306. delay(5);
  307.  
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement