1+ /*
2+ More about peripheral mode: https://inkplate.readthedocs.io/en/latest/peripheral-mode.html
3+ */
4+
5+ char strTemp [2001 ];
6+
7+ int hexToChar (char c )
8+ {
9+ if (c >= '0' && c <= '9' )
10+ return c - '0' ;
11+ if (c >= 'A' && c <= 'F' )
12+ return c - 'A' + 10 ;
13+ if (c >= 'a' && c <= 'f' )
14+ return c - 'a' + 10 ;
15+ return -1 ;
16+ }
17+
18+ void run (char commandBuffer [], size_t n , Inkplate * display )
19+ {
20+ char * s = NULL ;
21+ char * e = NULL ;
22+ for (int i = 0 ; i < n ; i ++ )
23+ {
24+ if (commandBuffer [i ] == '#' && s == NULL )
25+ s = & commandBuffer [i ];
26+ if (commandBuffer [i ] == '*' && e == NULL )
27+ e = & commandBuffer [i ];
28+ }
29+ if (s != NULL && e != NULL )
30+ {
31+ if ((e - s ) > 0 )
32+ {
33+ int x , x1 , x2 , y , y1 , y2 , x3 , y3 , l , c , w , h , r , n ;
34+ char b ;
35+ switch (* (s + 1 ))
36+ {
37+ case '?' :
38+ Serial .print ("OK" );
39+ break ;
40+
41+ case '0' :
42+ sscanf (s + 3 , "%d,%d,%d" , & x , & y , & c );
43+ display -> drawPixel (x , y , c );
44+ break ;
45+
46+ case '1' :
47+ sscanf (s + 3 , "%d,%d,%d,%d,%d" , & x1 , & y1 , & x2 , & y2 , & c );
48+ display -> drawLine (x1 , y1 , x2 , y2 , c );
49+ break ;
50+
51+ case '2' :
52+ sscanf (s + 3 , "%d,%d,%d,%d" , & x , & y , & l , & c );
53+ display -> drawFastVLine (x , y , l , c );
54+ break ;
55+
56+ case '3' :
57+ sscanf (s + 3 , "%d,%d,%d,%d" , & x , & y , & l , & c );
58+ display -> drawFastHLine (x , y , l , c );
59+ break ;
60+
61+ case '4' :
62+ sscanf (s + 3 , "%d,%d,%d,%d,%d" , & x , & y , & w , & h , & c );
63+ display -> drawRect (x , y , w , h , c );
64+ break ;
65+
66+ case '5' :
67+ sscanf (s + 3 , "%d,%d,%d,%d" , & x , & y , & r , & c );
68+ display -> drawCircle (x , y , r , c );
69+ break ;
70+
71+ case '6' :
72+ sscanf (s + 3 , "%d,%d,%d,%d,%d,%d,%d" , & x1 , & y1 , & x2 , & y2 , & x3 , & y3 , & c );
73+ display -> drawTriangle (x1 , y1 , x2 , y2 , x3 , y3 , c );
74+ break ;
75+
76+ case '7' :
77+ sscanf (s + 3 , "%d,%d,%d,%d,%d,%d" , & x , & y , & w , & h , & r , & c );
78+ display -> drawRoundRect (x , y , w , h , r , c );
79+ break ;
80+
81+ case '8' :
82+ sscanf (s + 3 , "%d,%d,%d,%d,%d" , & x , & y , & w , & h , & c );
83+ display -> fillRect (x , y , w , h , c );
84+ break ;
85+
86+ case '9' :
87+ sscanf (s + 3 , "%d,%d,%d,%d" , & x , & y , & r , & c );
88+ display -> fillCircle (x , y , r , c );
89+ break ;
90+
91+ case 'A' :
92+ sscanf (s + 3 , "%d,%d,%d,%d,%d,%d,%d" , & x1 , & y1 , & x2 , & y2 , & x3 , & y3 , & c );
93+ display -> fillTriangle (x1 , y1 , x2 , y2 , x3 , y3 , c );
94+ break ;
95+
96+ case 'B' :
97+ sscanf (s + 3 , "%d,%d,%d,%d,%d,%d" , & x , & y , & w , & h , & r , & c );
98+ display -> fillRoundRect (x , y , w , h , r , c );
99+ break ;
100+
101+ case 'C' :
102+ sscanf (s + 3 , "\"%2000[^\"]\"" , strTemp );
103+ n = strlen (strTemp );
104+ for (int i = 0 ; i < n ; i ++ )
105+ {
106+ strTemp [i ] = toupper (strTemp [i ]);
107+ }
108+ for (int i = 0 ; i < n ; i += 2 )
109+ {
110+ strTemp [i / 2 ] = (hexToChar (strTemp [i ]) << 4 ) | (hexToChar (strTemp [i + 1 ]) & 0x0F );
111+ }
112+ strTemp [n / 2 ] = 0 ;
113+ display -> print (strTemp );
114+ break ;
115+
116+ case 'D' :
117+ sscanf (s + 3 , "%d" , & c );
118+ display -> setTextSize (c );
119+ break ;
120+
121+ case 'E' :
122+ sscanf (s + 3 , "%d,%d" , & x , & y );
123+ display -> setCursor (x , y );
124+ break ;
125+
126+ case 'F' :
127+ sscanf (s + 3 , "%c" , & b );
128+ if (b == 'T' )
129+ display -> setTextWrap (true);
130+ if (b == 'F' )
131+ display -> setTextWrap (false);
132+ break ;
133+
134+ case 'G' :
135+ sscanf (s + 3 , "%d" , & c );
136+ c &= 3 ;
137+ display -> setRotation (c );
138+ break ;
139+
140+ case 'H' :
141+ sscanf (s + 3 , "%d,%d,\"%149[^\"]\"" , & x , & y , strTemp );
142+ n = strlen (strTemp );
143+ for (int i = 0 ; i < n ; i ++ )
144+ {
145+ strTemp [i ] = toupper (strTemp [i ]);
146+ }
147+ for (int i = 0 ; i < n ; i += 2 )
148+ {
149+ strTemp [i / 2 ] = (hexToChar (strTemp [i ]) << 4 ) | (hexToChar (strTemp [i + 1 ]) & 0x0F );
150+ }
151+ strTemp [n / 2 ] = 0 ;
152+ r = display -> sdCardInit ();
153+ if (r )
154+ {
155+ r = display -> image .drawBitmapFromSd (strTemp , x , y );
156+ Serial .print ("#H(" );
157+ Serial .print (r , DEC );
158+ Serial .println (")*" );
159+ Serial .flush ();
160+ }
161+ else
162+ {
163+ Serial .println ("#H(-1)*" );
164+ Serial .flush ();
165+ }
166+ break ;
167+
168+ case 'K' :
169+ sscanf (s + 3 , "%c" , & b );
170+ if (b == '1' )
171+ {
172+ display -> clearDisplay ();
173+ }
174+ break ;
175+
176+ case 'L' :
177+ sscanf (s + 3 , "%c" , & b );
178+ if (b == '1' )
179+ {
180+ display -> display ();
181+ }
182+ break ;
183+
184+ case 'O' :
185+ sscanf (s + 3 , "%d" , & c );
186+ if (c >= 0 && c <= 2 )
187+ {
188+ Serial .print ("#O(" );
189+ // Serial.print(display->readTouchpad(c), DEC);
190+ Serial .println (")*" );
191+ Serial .flush ();
192+ }
193+ break ;
194+
195+ case 'P' :
196+ sscanf (s + 3 , "%c" , & b );
197+ if (b == '?' )
198+ {
199+ Serial .print ("#P(" );
200+ Serial .print (display -> readBattery (), 2 );
201+ Serial .println (")*" );
202+ Serial .flush ();
203+ }
204+ break ;
205+ }
206+ * s = 0 ;
207+ * e = 0 ;
208+ }
209+ }
210+ }
0 commit comments