1 /*
2  * Copyright (C) 2021-2023 Alibaba Group Holding Limited
3  */
4 
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <ulog/ulog.h>
8 #include "ugraphics.h"
9 
10 #if AOS_COMP_CLI
11 #include "aos/cli.h"
12 #endif
13 
14 #define TAG "ugraphics_example"
15 #define LOG printf
16 
17 #define WIDTH  SCREEN_W
18 #define HEIGHT SCREEN_H
19 
ugraphics_comp_example(int argc,char ** argv)20 static void ugraphics_comp_example(int argc, char **argv)
21 {
22     int ret;
23 
24     if (argc < 2)
25         LOGE(TAG, "wrong parameter number\n");
26 
27     if (!strncmp(argv[1], "init", 4)) {
28         /*Initialize ugraphics window*/
29         ret = ugraphics_init(WIDTH, HEIGHT);
30         if (ret < 0) {
31             LOGE(TAG, "ugraphics init fail, ret: %d\n", ret);
32             return;
33         }
34         LOG("ugraphics init ok!\n");
35     } else if (!strncmp(argv[1], "draw", 4)) {
36         /*Set background color*/
37         ret = ugraphics_set_color(COLOR_BLACK);
38         if (ret < 0) {
39             LOGE(TAG, "ugraphics set color fail, ret: %d\n", ret);
40             return;
41         }
42         /*Draw background rectangle*/
43         ret = ugraphics_fill_rect(0, 0, WIDTH, HEIGHT);
44         if (ret < 0) {
45             LOGE(TAG, "ugraphics fill rect fail, ret: %d\n", ret);
46             return;
47         }
48         if (!strncmp(argv[2], "rect", 4)) {
49             /*Set rect boarder color*/
50             ret = ugraphics_set_color(COLOR_RED);
51             if (ret < 0) {
52                 LOGE(TAG, "ugraphics set color fail, ret: %d\n", ret);
53                 return;
54             }
55 
56             /*Draw empty rectangle*/
57             ret = ugraphics_draw_rect(0, 0, WIDTH / 2, HEIGHT / 2);
58             if (ret < 0) {
59                 LOGE(TAG, "ugraphics draw rect fail, ret: %d\n", ret);
60                 return;
61             }
62             LOG("ugraphics draw rectangle ok!\n");
63         } else if (!strncmp(argv[2], "line", 4)) {
64             /*Set line color*/
65             ret = ugraphics_set_color(COLOR_RED);
66             if (ret < 0) {
67                 LOGE(TAG, "ugraphics set color fail, ret: %d\n", ret);
68                 return;
69             }
70 
71             /*Draw line*/
72             ret = ugraphics_draw_line(0, HEIGHT / 4, WIDTH, HEIGHT / 4);
73             if (ret < 0) {
74                 LOGE(TAG, "ugraphics draw line fail, ret: %d\n", ret);
75                 return;
76             }
77 
78             LOG("ugraphics draw line ok!\n");
79         } else if (!strncmp(argv[2], "jpg", 3)) {
80             /*Draw image*/
81             ret = ugraphics_draw_image("/data/ugraphics_image/object.jpg", 0, 0);
82             if (ret < 0) {
83                 LOGE(TAG, "ugraphics draw jpg image fail, ret: %d\n", ret);
84                 return;
85             }
86             LOG("ugraphics draw jpg ok!\n");
87         } else if (!strncmp(argv[2], "png", 3)) {
88             /*Draw image*/
89             ret = ugraphics_draw_image("/data/ugraphics_image/anime.png", 0, 0);
90             if (ret < 0) {
91                 LOGE(TAG, "ugraphics draw png image fail, ret: %d\n", ret);
92                 return;
93             }
94             LOG("ugraphics draw png ok!\n");
95         } else if (!strncmp(argv[2], "string", 6)) {
96             /*Set string color*/
97             ret = ugraphics_set_color(COLOR_RED);
98             if (ret < 0) {
99                 LOGE(TAG, "ugraphics set color fail, ret: %d\n", ret);
100                 return;
101             }
102 
103             /*Load default ttf font*/
104             ret = ugraphics_load_font("/data/font/Alibaba-PuHuiTi-Heavy.ttf", 18);
105             if (ret < 0) {
106                 LOGE(TAG, "ugraphics load font fail, ret: %d\n", ret);
107                 return;
108             }
109 
110             /*Set font style*/
111             ugraphics_set_font_style(UGRAPHICS_FONT_STYLE);
112 
113             /*Draw string*/
114             ugraphics_draw_string("Welcome to AliOS Things!", WIDTH / 8, HEIGHT / 2);
115             LOG("ugraphics draw string ok!\n");
116         }
117 
118         /*Show graphics on screen*/
119         ugraphics_flip();
120     } else if (!strncmp(argv[1], "fill", 4)) {
121         /*Set background color*/
122         ret = ugraphics_set_color(COLOR_BLACK);
123         if (ret < 0) {
124             LOGE(TAG, "ugraphics set color fail, ret: %d\n", ret);
125             return;
126         }
127         /*Draw background rectangle*/
128         ret = ugraphics_fill_rect(0, 0, WIDTH, HEIGHT);
129         if (ret < 0) {
130             LOGE(TAG, "ugraphics fill rect fail, ret: %d\n", ret);
131             return;
132         }
133         if (!strncmp(argv[2], "rect", 4)) {
134             /*Set background color*/
135             ret = ugraphics_set_color(COLOR_RED);
136             if (ret < 0) {
137                 LOGE(TAG, "ugraphics set color fail, ret: %d\n", ret);
138                 return;
139             }
140 
141             /*Fill full rectangle*/
142             ret = ugraphics_fill_rect(0, 0, WIDTH / 2, HEIGHT / 2);
143             if (ret < 0) {
144                 LOGE(TAG, "ugraphics fill rect fail, ret: %d\n", ret);
145                 return;
146             }
147             LOG("ugraphics fill rectangle ok!\n");
148         } else {
149             LOG("unkown command\n");
150         }
151 
152         /*Show graphics on screen*/
153         ugraphics_flip();
154     } else if (!strncmp(argv[1], "clear", 5)) {
155         /*Clear buffer on screen*/
156         ret = ugraphics_clear();
157         if (ret < 0) {
158             LOGE(TAG, "ugraphics clear fail, ret: %d\n", ret);
159             return;
160         }
161 
162         /*Set background color*/
163         ret = ugraphics_set_color(COLOR_BLACK);
164         if (ret < 0) {
165             LOGE(TAG, "ugraphics set color fail, ret: %d\n", ret);
166             return;
167         }
168 
169         /*Draw background rectangle*/
170         ret = ugraphics_fill_rect(0, 0, WIDTH, HEIGHT);
171         if (ret < 0) {
172             LOGE(TAG, "ugraphics fill rect fail, ret: %d\n", ret);
173             return;
174         }
175 
176         /*Show graphics on screen*/
177         ugraphics_flip();
178         LOG("ugraphics clear screen ok!\n");
179     } else if (!strncmp(argv[1], "quit", 4)) {
180         /*Quit ugraphics component*/
181         ugraphics_quit();
182         LOG("ugraphics quit ok!\n");
183     }
184 
185     return;
186 }
187 
188 #if AOS_COMP_CLI
189 /* reg args: fun, cmd, description*/
190 ALIOS_CLI_CMD_REGISTER(ugraphics_comp_example, ugraphics, ugraphics component base example)
191 #endif
192