source: asp3_tinet_ecnl_arm/trunk/asp3_dcre/gdic/adafruit_st7735/adafruit_st7735.c@ 364

Last change on this file since 364 was 364, checked in by coas-nagasima, 5 years ago

TINETとSocket APIなどを更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 26.7 KB
Line 
1/*
2 * TOPPERS/ASP Kernel
3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Advanced Standard Profile Kernel
5 *
6 * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
7 * Toyohashi Univ. of Technology, JAPAN
8 * Copyright (C) 2004-2012 by Embedded and Real-Time Systems Laboratory
9 * Graduate School of Information Science, Nagoya Univ., JAPAN
10 * Copyright (C) 2015-2017 by TOPPERS PROJECT Educational Working Group.
11 *
12 * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
13 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
14 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
15 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
16 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
17 * スコード中に含まれていること.
18 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
19 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
20 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
21 * の無保証規定を掲載すること.
22 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
23 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
24 * と.
25 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
26 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
27 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
28 * 報告すること.
29 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
30 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
31 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
32 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
33 * 免責すること.
34 *
35 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
36 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
37 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
38 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
39 * の責任を負わない.
40 *
41 * $Id$
42 */
43
44/*
45 * Written by Limor Fried/Ladyada for Adafruit Industries.
46 * MIT license, all text above must be included in any redistribution
47 * http://opensource.org/licenses/mit-license.php
48 */
49
50/*
51 * ADAFRUIT ST7735 1.8"LCD制御プログラムの本体
52 */
53
54#include <kernel.h>
55#include <t_syslog.h>
56#include <t_stdlib.h>
57#include <target_syssvc.h>
58#include "syssvc/serial.h"
59#include "syssvc/syslog.h"
60#include "gpio_api.h"
61#include "spi_api.h"
62#include "adafruit_st7735.h"
63
64/*
65 * サービスコールのエラーのログ出力
66 */
67Inline void
68svc_perror(const char *file, int_t line, const char *expr, ER ercd)
69{
70 if (ercd < 0) {
71 t_perror(LOG_ERROR, file, line, expr, ercd);
72 }
73}
74
75#define SVC_PERROR(expr) svc_perror(__FILE__, __LINE__, #expr, (expr))
76#define ABS(X) ((X) > 0 ? (X) : -(X))
77
78
79// Rather than a bazillion writecommand() and writedata() calls, screen
80// initialization commands and arguments are organized in these tables
81// stored in PROGMEM. The table may look bulky, but that's mostly the
82// formatting -- storage-wise this is hundreds of bytes more compact
83// than the equivalent code. Companion function follows.
84#define DELAY 0x80
85static const uint8_t
86 Bcmd[] = { // Initialization commands for 7735B screens
87 18, // 18 commands in list:
88 ST7735_SWRESET, DELAY, // 1: Software reset, no args, w/delay
89 50, // 50 ms delay
90 ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, no args, w/delay
91 255, // 255 = 500 ms delay
92 ST7735_COLMOD , 1+DELAY, // 3: Set color mode, 1 arg + delay:
93 0x05, // 16-bit color
94 10, // 10 ms delay
95 ST7735_FRMCTR1, 3+DELAY, // 4: Frame rate control, 3 args + delay:
96 0x00, // fastest refresh
97 0x06, // 6 lines front porch
98 0x03, // 3 lines back porch
99 10, // 10 ms delay
100 ST7735_MADCTL , 1 , // 5: Memory access ctrl (directions), 1 arg:
101 0x08, // Row addr/col addr, bottom to top refresh
102 ST7735_DISSET5, 2 , // 6: Display settings #5, 2 args, no delay:
103 0x15, // 1 clk cycle nonoverlap, 2 cycle gate
104 // rise, 3 cycle osc equalize
105 0x02, // Fix on VTL
106 ST7735_INVCTR , 1 , // 7: Display inversion control, 1 arg:
107 0x0, // Line inversion
108 ST7735_PWCTR1 , 2+DELAY, // 8: Power control, 2 args + delay:
109 0x02, // GVDD = 4.7V
110 0x70, // 1.0uA
111 10, // 10 ms delay
112 ST7735_PWCTR2 , 1 , // 9: Power control, 1 arg, no delay:
113 0x05, // VGH = 14.7V, VGL = -7.35V
114 ST7735_PWCTR3 , 2 , // 10: Power control, 2 args, no delay:
115 0x01, // Opamp current small
116 0x02, // Boost frequency
117 ST7735_VMCTR1 , 2+DELAY, // 11: Power control, 2 args + delay:
118 0x3C, // VCOMH = 4V
119 0x38, // VCOML = -1.1V
120 10, // 10 ms delay
121 ST7735_PWCTR6 , 2 , // 12: Power control, 2 args, no delay:
122 0x11, 0x15,
123 ST7735_GMCTRP1,16 , // 13: Magical unicorn dust, 16 args, no delay:
124 0x09, 0x16, 0x09, 0x20, // (seriously though, not sure what
125 0x21, 0x1B, 0x13, 0x19, // these config values represent)
126 0x17, 0x15, 0x1E, 0x2B,
127 0x04, 0x05, 0x02, 0x0E,
128 ST7735_GMCTRN1,16+DELAY, // 14: Sparkles and rainbows, 16 args + delay:
129 0x0B, 0x14, 0x08, 0x1E, // (ditto)
130 0x22, 0x1D, 0x18, 0x1E,
131 0x1B, 0x1A, 0x24, 0x2B,
132 0x06, 0x06, 0x02, 0x0F,
133 10, // 10 ms delay
134 ST7735_CASET , 4 , // 15: Column addr set, 4 args, no delay:
135 0x00, 0x02, // XSTART = 2
136 0x00, 0x81, // XEND = 129
137 ST7735_RASET , 4 , // 16: Row addr set, 4 args, no delay:
138 0x00, 0x02, // XSTART = 1
139 0x00, 0x81, // XEND = 160
140 ST7735_NORON , DELAY, // 17: Normal display on, no args, w/delay
141 10, // 10 ms delay
142 ST7735_DISPON , DELAY, // 18: Main screen turn on, no args, w/delay
143 255}; // 255 = 500 ms delay
144
145static const uint8_t
146 Rcmd1[] = { // Init for 7735R, part 1 (red or green tab)
147 15, // 15 commands in list:
148 ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay
149 150, // 150 ms delay
150 ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, 0 args, w/delay
151 255, // 500 ms delay
152 ST7735_FRMCTR1, 3 , // 3: Frame rate ctrl - normal mode, 3 args:
153 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
154 ST7735_FRMCTR2, 3 , // 4: Frame rate control - idle mode, 3 args:
155 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
156 ST7735_FRMCTR3, 6 , // 5: Frame rate ctrl - partial mode, 6 args:
157 0x01, 0x2C, 0x2D, // Dot inversion mode
158 0x01, 0x2C, 0x2D, // Line inversion mode
159 ST7735_INVCTR , 1 , // 6: Display inversion ctrl, 1 arg, no delay:
160 0x07, // No inversion
161 ST7735_PWCTR1 , 3 , // 7: Power control, 3 args, no delay:
162 0xA2,
163 0x02, // -4.6V
164 0x84, // AUTO mode
165 ST7735_PWCTR2 , 1 , // 8: Power control, 1 arg, no delay:
166 0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
167 ST7735_PWCTR3 , 2 , // 9: Power control, 2 args, no delay:
168 0x0A, // Opamp current small
169 0x00, // Boost frequency
170 ST7735_PWCTR4 , 2 , // 10: Power control, 2 args, no delay:
171 0x8A, // BCLK/2, Opamp current small & Medium low
172 0x2A,
173 ST7735_PWCTR5 , 2 , // 11: Power control, 2 args, no delay:
174 0x8A, 0xEE,
175 ST7735_VMCTR1 , 1 , // 12: Power control, 1 arg, no delay:
176 0x0E,
177 ST7735_INVOFF , 0 , // 13: Don't invert display, no args, no delay
178 ST7735_MADCTL , 1 , // 14: Memory access control (directions), 1 arg:
179 0xC8, // row addr/col addr, bottom to top refresh
180 ST7735_COLMOD , 1 , // 15: set color mode, 1 arg, no delay:
181 0x05 }; // 16-bit color
182
183static const uint8_t
184 Rcmd2green[] = { // Init for 7735R, part 2 (green tab only)
185 2, // 2 commands in list:
186 ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
187 0x00, 0x02, // XSTART = 0
188 0x00, 0x7F+0x02, // XEND = 127
189 ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
190 0x00, 0x01, // XSTART = 0
191 0x00, 0x9F+0x01 }; // XEND = 159
192
193static const uint8_t
194 Rcmd2red[] = { // Init for 7735R, part 2 (red tab only)
195 2, // 2 commands in list:
196 ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
197 0x00, 0x00, // XSTART = 0
198 0x00, 0x7F, // XEND = 127
199 ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
200 0x00, 0x00, // XSTART = 0
201 0x00, 0x9F }; // XEND = 159
202
203static const uint8_t
204 Rcmd2green144[] = { // Init for 7735R, part 2 (green 1.44 tab)
205 2, // 2 commands in list:
206 ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
207 0x00, 0x00, // XSTART = 0
208 0x00, 0x7F, // XEND = 127
209 ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
210 0x00, 0x00, // XSTART = 0
211 0x00, 0x7F }; // XEND = 127
212
213static const uint8_t
214 Rcmd3[] = { // Init for 7735R, part 3 (red or green tab)
215 4, // 4 commands in list:
216 ST7735_GMCTRP1, 16 , // 1: Magical unicorn dust, 16 args, no delay:
217 0x02, 0x1c, 0x07, 0x12,
218 0x37, 0x32, 0x29, 0x2d,
219 0x29, 0x25, 0x2B, 0x39,
220 0x00, 0x01, 0x03, 0x10,
221 ST7735_GMCTRN1, 16 , // 2: Sparkles and rainbows, 16 args, no delay:
222 0x03, 0x1d, 0x07, 0x06,
223 0x2E, 0x2C, 0x29, 0x2D,
224 0x2E, 0x2E, 0x37, 0x3F,
225 0x00, 0x00, 0x02, 0x10,
226 ST7735_NORON , DELAY, // 3: Normal display on, no args, w/delay
227 10, // 10 ms delay
228 ST7735_DISPON , DELAY, // 4: Main screen turn on, no args w/delay
229 100 }; // 100 ms delay
230
231
232
233
234/* Buffer used for transmission */
235static uint8_t aTxBuffer[4];
236
237#define PORT_HIGH 1
238#define PORT_LOW 0
239
240#define cs_set(sw) gpio_write(&hlcd->cs_pin, sw)
241#define rs_set(sw) gpio_write(&hlcd->rs_pin, sw)
242#define rst_set(sw) gpio_write(&hlcd->rst_pin, sw)
243#define cs2_set(sw) gpio_write(&hlcd->cs2_pin, sw)
244
245/*
246 * LCDへのコマンド送信関数
247 */
248ER
249lcd_writecommand(LCD_Handler_t *hlcd, uint8_t c)
250{
251 ER ercd = E_OK;
252
253 if(hlcd->spi_lock != 0){
254 if((ercd = wai_sem(hlcd->spi_lock)) != E_OK)
255 return ercd;
256 }
257 rs_set(PORT_LOW);
258 cs_set(PORT_LOW);
259 spi_master_write(&hlcd->hspi, c);
260 cs_set(PORT_HIGH);
261 if(hlcd->spi_lock != 0)
262 sig_sem(hlcd->spi_lock);
263 return ercd;
264}
265
266/*
267 * LCDへのデータ送信関数
268 */
269ER
270lcd_writedata(LCD_Handler_t *hlcd, uint8_t c)
271{
272 ER ercd = E_OK;
273
274 if(hlcd->spi_lock != 0){
275 if((ercd = wai_sem(hlcd->spi_lock)) != E_OK)
276 return ercd;
277 }
278 rs_set(PORT_HIGH);
279 cs_set(PORT_LOW);
280 //dly_tsk(100/*us*/);
281 spi_master_write(&hlcd->hspi, c);
282 //dly_tsk(100/*us*/);
283 cs_set(PORT_HIGH);
284 if(hlcd->spi_lock != 0)
285 sig_sem(hlcd->spi_lock);
286 return ercd;
287}
288
289/*
290 * LCDへの2バイトデータ送信関数
291 */
292ER
293lcd_writedata2(LCD_Handler_t *hlcd, uint16_t c, int cnt)
294{
295 ER ercd = E_OK;
296 int i;
297
298 if(cnt == 0)
299 return ercd;
300 if(hlcd->spi_lock != 0){
301 if((ercd = wai_sem(hlcd->spi_lock)) != E_OK)
302 return ercd;
303 }
304 rs_set(PORT_HIGH);
305 cs_set(PORT_LOW);
306 //dly_tsk(100/*us*/);
307 for(i = 0 ; i < cnt ; i++){
308 spi_master_write(&hlcd->hspi, c >> 8);
309 spi_master_write(&hlcd->hspi, c);
310 }
311 //dly_tsk(100/*us*/);
312 cs_set(PORT_HIGH);
313 if(hlcd->spi_lock != 0)
314 sig_sem(hlcd->spi_lock);
315 return ercd;
316}
317
318/*
319 * LCDへのRGBデータ送信関数
320 */
321ER
322lcd_writedata3(LCD_Handler_t *hlcd, uint8_t *pbmp, uint16_t width, uint16_t height)
323{
324 ER ercd = E_OK;
325 int index, i;
326
327 if(width == 0 || height == 0)
328 return ercd;
329 if(hlcd->spi_lock != 0){
330 if((ercd = wai_sem(hlcd->spi_lock)) != E_OK)
331 return ercd;
332 }
333 rs_set(PORT_HIGH);
334 cs_set(PORT_LOW);
335 //dly_tsk(100/*us*/);
336 for(index=0; index < height; index++){
337 uint8_t *p = pbmp;
338 for (i = 0; i < width; i++){
339 aTxBuffer[0] = (*p++) & 0xf8;
340 aTxBuffer[0] |= *p >> 5;
341 aTxBuffer[1] = (*p++ << 3) & 0xE0;
342 aTxBuffer[1] |= (*p++ >> 3) & 0x1F;
343 spi_master_write(&hlcd->hspi, aTxBuffer[0]);
344 spi_master_write(&hlcd->hspi, aTxBuffer[1]);
345 }
346 pbmp -= width*3;
347 }
348 //dly_tsk(100/*us*/);
349 cs_set(PORT_HIGH);
350 if(hlcd->spi_lock != 0)
351 sig_sem(hlcd->spi_lock);
352 return ercd;
353}
354
355/*
356 * LCDへのRGBデータ送信関数
357 */
358ER
359lcd_writedata4(LCD_Handler_t *hlcd, uint8_t *pbmp, uint16_t width, uint16_t height)
360{
361 ER ercd = E_OK;
362 int index, i;
363
364 if(width == 0 || height == 0)
365 return ercd;
366 if(hlcd->spi_lock != 0){
367 if((ercd = wai_sem(hlcd->spi_lock)) != E_OK)
368 return ercd;
369 }
370 rs_set(PORT_HIGH);
371 cs_set(PORT_LOW);
372 //dly_tsk(100/*us*/);
373 for(index=0; index < height; index++){
374 uint8_t *p = pbmp;
375 for (i = 0; i < width; i++){
376 spi_master_write(&hlcd->hspi, *p++);
377 spi_master_write(&hlcd->hspi, *p++);
378 }
379 pbmp -= width*2;
380 }
381 //dly_tsk(100/*us*/);
382 cs_set(PORT_HIGH);
383 if(hlcd->spi_lock != 0)
384 sig_sem(hlcd->spi_lock);
385 return ercd;
386}
387
388/*
389 * ST7735へのコマンドリスト送信
390 */
391static void
392Adafruit_ST7735_commandList(LCD_Handler_t *hlcd, const uint8_t *addr)
393{
394 uint8_t numCommands, numArgs;
395 uint16_t ms;
396
397 numCommands = *addr++; // Number of commands to follow
398 while(numCommands--) { // For each command...
399 SVC_PERROR(lcd_writecommand(hlcd, *addr++)); // Read, issue command
400 numArgs = *addr++; // Number of args to follow
401 ms = numArgs & DELAY; // If hibit set, delay follows args
402 numArgs &= ~DELAY; // Mask out delay bit
403 while(numArgs--) { // For each argument...
404 SVC_PERROR(lcd_writedata(hlcd, *addr++)); // Read, issue argument
405 }
406
407 if(ms) {
408 ms = *addr++; // Read post-command delay time (ms)
409 if(ms == 255) ms = 500; // If 255, delay for 500 ms
410 dly_tsk(ms * 1000);
411 }
412 }
413}
414
415
416/*
417 * ST7735B または ST7735RタイプのLEDの共通初期化関数
418 */
419static void
420Adafruit_ST7735_commonInit(LCD_Handler_t *hlcd, const uint8_t *cmdList)
421{
422 hlcd->colstart = hlcd->rowstart = 0; // May be overridden in init func
423 hlcd->_width = ST7735_TFTWIDTH;
424 hlcd->_height = ST7735_TFTHEIGHT_18;
425
426 // toggle RST low to reset; CS low so it'll listen to us
427 cs_set(PORT_LOW);
428 rst_set(PORT_HIGH);
429 dly_tsk(500 * 1000);
430 rst_set(PORT_LOW);
431 dly_tsk(500 * 1000);
432 rst_set(PORT_HIGH);
433
434 if(cmdList) Adafruit_ST7735_commandList(hlcd, cmdList);
435}
436
437
438/*
439 * 表示ウィンドウ設定
440 */
441void
442lcd_setAddrWindow(LCD_Handler_t *hlcd, uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1)
443{
444 SVC_PERROR(lcd_writecommand(hlcd, ST7735_CASET)); // Column addr set
445 SVC_PERROR(lcd_writedata(hlcd, 0x00));
446 SVC_PERROR(lcd_writedata(hlcd, x0+hlcd->colstart)); // XSTART
447 SVC_PERROR(lcd_writedata(hlcd, 0x00));
448 SVC_PERROR(lcd_writedata(hlcd, x1+hlcd->colstart)); // XEND
449
450 SVC_PERROR(lcd_writecommand(hlcd, ST7735_RASET)); // Row addr set
451 SVC_PERROR(lcd_writedata(hlcd, 0x00));
452 SVC_PERROR(lcd_writedata(hlcd, y0+hlcd->rowstart)); // YSTART
453 SVC_PERROR(lcd_writedata(hlcd, 0x00));
454 SVC_PERROR(lcd_writedata(hlcd, y1+hlcd->rowstart)); // YEND
455
456 SVC_PERROR(lcd_writecommand(hlcd, ST7735_RAMWR)); // write to RAM
457}
458
459/*
460 * ST7735B screensの初期化関数
461 */
462void
463lcd_initB(LCD_Handler_t *hlcd)
464{
465 Adafruit_ST7735_commonInit(hlcd, Bcmd);
466}
467
468/*
469 * ST7735R screens (green or red tabs)の初期化関数
470 */
471void
472lcd_initR(LCD_Handler_t *hlcd, uint8_t options)
473{
474 Adafruit_ST7735_commonInit(hlcd, Rcmd1);
475
476 if(options == INITR_GREENTAB){
477 Adafruit_ST7735_commandList(hlcd, Rcmd2green);
478 hlcd->colstart = 2;
479 hlcd->rowstart = 1;
480 }
481 else if(options == INITR_144GREENTAB){
482 hlcd->_height = ST7735_TFTHEIGHT_144;
483 Adafruit_ST7735_commandList(hlcd, Rcmd2green144);
484 hlcd->colstart = 2;
485 hlcd->rowstart = 3;
486 }
487 else{
488 // colstart, rowstart left at default '0' values
489 Adafruit_ST7735_commandList(hlcd, Rcmd2red);
490 }
491 Adafruit_ST7735_commandList(hlcd, Rcmd3);
492
493 // if black, change MADCTL color filter
494 if(options == INITR_BLACKTAB){
495 SVC_PERROR(lcd_writecommand(hlcd, ST7735_MADCTL));
496 SVC_PERROR(lcd_writedata(hlcd, 0xC0));
497 }
498 syslog_2(LOG_NOTICE, "## width(%d) height(%d) ##", hlcd->_width, hlcd->_height);
499 hlcd->tabcolor = options;
500}
501
502/*
503 * RECTANGLEのフィル描画
504 * param1 hlcd: Pointer to LCD Handler
505 * param2 x: start X position
506 * param3 y: start Y position
507 * param4 w: width
508 * param5 h: height
509 * param6 color: color value
510 */
511void
512lcd_fillRect(LCD_Handler_t *hlcd, int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
513{
514 // rudimentary clipping (drawChar w/big text requires this)
515 if((x >= hlcd->_width) || (y >= hlcd->_height)) return;
516 if((x + w - 1) >= hlcd->_width) w = hlcd->_width - x;
517 if((y + h - 1) >= hlcd->_height) h = hlcd->_height - y;
518
519 lcd_setAddrWindow(hlcd, x, y, x+w-1, y+h-1);
520 SVC_PERROR(lcd_writedata2(hlcd, color, h*w));
521}
522
523void
524lcd_pushColor(LCD_Handler_t *hlcd, uint16_t color)
525{
526 SVC_PERROR(lcd_writedata2(hlcd, color, 1));
527}
528
529/*
530 * PIXEL描画
531 * param1 hlcd: Pointer to LCD Handler
532 * param2 x: X position
533 * param3 y: Y position
534 * param4 color: color value
535 */
536void
537lcd_drawPixel(LCD_Handler_t *hlcd, int16_t x, int16_t y, uint16_t color)
538{
539 if((x < 0) ||(x >= hlcd->_width) || (y < 0) || (y >= hlcd->_height)) return;
540
541 lcd_setAddrWindow(hlcd, x, y, x+1, y+1);
542 SVC_PERROR(lcd_writedata2(hlcd, color, 1));
543}
544
545/*
546 * 垂直方向LINEの高速描画
547 * param1 hlcd: Pointer to LCD Handler
548 * param2 x: start X position
549 * param3 y: start Y position
550 * param4 h: height
551 * param5 color: color value
552 */
553void
554lcd_drawFastVLine(LCD_Handler_t *hlcd, int16_t x, int16_t y, int16_t h, uint16_t color)
555{
556 // Rudimentary clipping
557 if((x >= hlcd->_width) || (y >= hlcd->_height)) return;
558 if((y+h-1) >= hlcd->_height) h = hlcd->_height-y;
559 lcd_setAddrWindow(hlcd, x, y, x, y+h-1);
560 SVC_PERROR(lcd_writedata2(hlcd, color, h));
561}
562
563/*
564 * 水平方向LINEの高速描画
565 * param1 hlcd: Pointer to LCD Handler
566 * param2 x: start X position
567 * param3 y: start Y position
568 * param4 w: width
569 * param5 color: color value
570 */
571void lcd_drawFastHLine(LCD_Handler_t *hlcd, int16_t x, int16_t y, int16_t w, uint16_t color)
572{
573 // Rudimentary clipping
574 if((x >= hlcd->_width) || (y >= hlcd->_height)) return;
575 if((x+w-1) >= hlcd->_width) w = hlcd->_width-x;
576 lcd_setAddrWindow(hlcd, x, y, x+w-1, y);
577 SVC_PERROR(lcd_writedata2(hlcd, color, w));
578}
579
580/*
581 * BITMAP描画
582 * param1 hlcd: Pointer to LCD Handler
583 * param2 x0: Bmp X position in the LCD
584 * param3 y0: Bmp Y position in the LCD
585 * param4 pbmp: Pointer to Bmp picture address in the internal Flash
586 */
587void
588lcd_drawBitmap(LCD_Handler_t *hlcd, uint16_t x0, uint16_t y0, uint8_t *pbmp)
589{
590 uint32_t index = 0, width = 0, height = 0, bit_pixel = 0;
591 uint32_t input_color_mode = 0;
592
593 /* Get bitmap data address offset */
594 index = *(uint16_t *) (pbmp + 10);
595 index |= (*(uint16_t *) (pbmp + 12)) << 16;
596
597 /* Read bitmap width */
598 width = *(uint16_t *) (pbmp + 18);
599 width |= (*(uint16_t *) (pbmp + 20)) << 16;
600
601 /* Read bitmap height */
602 height = *(uint16_t *) (pbmp + 22);
603 height |= (*(uint16_t *) (pbmp + 24)) << 16;
604
605 /* Read bit/pixel */
606 bit_pixel = *(uint16_t *) (pbmp + 28);
607
608 /* Get the layer pixel format */
609 if ((bit_pixel/8) == 4){
610 input_color_mode = CM_ARGB8888;
611 }
612 else if ((bit_pixel/8) == 2){
613 input_color_mode = CM_RGB565;
614 }
615 else{
616 input_color_mode = CM_RGB888;
617 }
618
619 /* Bypass the bitmap header */
620 pbmp += (index + (width * (height - 1) * (bit_pixel/8)));
621 syslog_4(LOG_NOTICE, "## input_color_mode(%d) width(%d) height(%d) bit_pixel(%d) ##", input_color_mode, width, height, bit_pixel);
622
623 lcd_setAddrWindow(hlcd, x0, y0, x0+width-1, y0+height-1);
624 lcd_writedata3(hlcd, pbmp, width, height);
625}
626
627void
628lcd_drawImage(LCD_Handler_t *hlcd, uint16_t x0, uint16_t y0, int16_t w, int16_t h, uint8_t *pbmp)
629{
630 lcd_setAddrWindow(hlcd, x0, y0, x0+w-1, y0+h-1);
631 pbmp += w * (h - 1) * 3;
632 lcd_writedata3(hlcd, pbmp, w, h);
633}
634
635/*
636 * INVERT DISPLAY
637 * param1 hlcd: Pointer to LCD Handler
638 * param2 i: invert value
639 */
640void
641lcd_invertDisplay(LCD_Handler_t *hlcd, bool_t i)
642{
643 SVC_PERROR(lcd_writecommand(hlcd, i ? ST7735_INVON : ST7735_INVOFF));
644}
645
646
647/*
648 * スクリーンフィル
649 * param1 pDrawProp: Pointer to Draw Prop
650 */
651void
652lcd_fillScreen(LCD_DrawProp_t *pDrawProp)
653{
654 lcd_fillRect(pDrawProp->hlcd, 0, 0, pDrawProp->hlcd->_width, pDrawProp->hlcd->_height, pDrawProp->BackColor);
655}
656
657/*
658 * RECTANGLE描画
659 * param1 pDrawProp: Pointer to Draw Prop
660 * param2 x: left X position
661 * param3 y: top Y position
662 * param4 w: width
663 * param5 h: height
664 */
665void
666lcd_drawRect(LCD_DrawProp_t *pDrawProp, int16_t x, int16_t y, int16_t w, int16_t h)
667{
668 LCD_Handler_t *hlcd = pDrawProp->hlcd;
669 uint16_t color;
670
671 // rudimentary clipping (drawChar w/big text requires this)
672 if((x >= hlcd->_width) || (y >= hlcd->_height)) return;
673 if((x + w - 1) >= hlcd->_width) w = hlcd->_width - x;
674 if((y + h - 1) >= hlcd->_height) h = hlcd->_height - y;
675
676 color = pDrawProp->TextColor;
677 lcd_drawFastVLine(hlcd, x, y, h, color);
678 lcd_drawFastHLine(hlcd, x, y+h-1, w, color);
679 lcd_drawFastVLine(hlcd, x+w-1, y, h, color);
680 lcd_drawFastHLine(hlcd, x, y, w, color);
681}
682
683/*
684 * 円描画
685 * param1 pDrawProp: Pointer to Draw Prop
686 * param2 x0: X position
687 * param3 y0: Y position
688 * param4 Radius: Circle radius
689 */
690void
691lcd_DrawCircle(LCD_DrawProp_t *pDrawProp, uint16_t x0, uint16_t y0, uint16_t Radius)
692{
693 LCD_Handler_t *hlcd = pDrawProp->hlcd;
694 int32_t decision; /* Decision Variable */
695 uint32_t current_x; /* Current X Value */
696 uint32_t current_y; /* Current Y Value */
697
698 decision = 3 - (Radius << 1);
699 current_x = 0;
700 current_y = Radius;
701
702 while(current_x <= current_y){
703 lcd_drawPixel(hlcd, (x0 + current_x), (y0 - current_y), pDrawProp->TextColor);
704 lcd_drawPixel(hlcd, (x0 - current_x), (y0 - current_y), pDrawProp->TextColor);
705 lcd_drawPixel(hlcd, (x0 + current_y), (y0 - current_x), pDrawProp->TextColor);
706 lcd_drawPixel(hlcd, (x0 - current_y), (y0 - current_x), pDrawProp->TextColor);
707 lcd_drawPixel(hlcd, (x0 + current_x), (y0 + current_y), pDrawProp->TextColor);
708 lcd_drawPixel(hlcd, (x0 - current_x), (y0 + current_y), pDrawProp->TextColor);
709 lcd_drawPixel(hlcd, (x0 + current_y), (y0 + current_x), pDrawProp->TextColor);
710 lcd_drawPixel(hlcd, (x0 - current_y), (y0 + current_x), pDrawProp->TextColor);
711
712 if (decision < 0){
713 decision += (current_x << 2) + 6;
714 }
715 else{
716 decision += ((current_x - current_y) << 2) + 10;
717 current_y--;
718 }
719 current_x++;
720 }
721}
722
723/*
724 * 線描画
725 * param1 pDrawProp: Pointer to Draw Prop
726 * param2 x1: Point 1 X position
727 * param3 y1: Point 1 Y position
728 * param4 x2: Point 2 X position
729 * param5 y2: Point 2 Y position
730 */
731void
732lcd_drawLine(LCD_DrawProp_t *pDrawProp, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
733{
734 LCD_Handler_t *hlcd = pDrawProp->hlcd;
735 int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0,
736 yinc1 = 0, yinc2 = 0, den = 0, num = 0, num_add = 0, num_pixels = 0,
737 curpixel = 0;
738
739 deltax = ABS(x2 - x1); /* The difference between the x's */
740 deltay = ABS(y2 - y1); /* The difference between the y's */
741 x = x1; /* Start x off at the first pixel */
742 y = y1; /* Start y off at the first pixel */
743
744 if(x2 >= x1){ /* The x-values are increasing */
745 xinc1 = 1;
746 xinc2 = 1;
747 }
748 else{ /* The x-values are decreasing */
749 xinc1 = -1;
750 xinc2 = -1;
751 }
752
753 if(y2 >= y1){ /* The y-values are increasing */
754 yinc1 = 1;
755 yinc2 = 1;
756 }
757 else{ /* The y-values are decreasing */
758 yinc1 = -1;
759 yinc2 = -1;
760 }
761
762 if(deltax >= deltay){ /* There is at least one x-value for every y-value */
763 xinc1 = 0; /* Don't change the x when numerator >= denominator */
764 yinc2 = 0; /* Don't change the y for every iteration */
765 den = deltax;
766 num = deltax / 2;
767 num_add = deltay;
768 num_pixels = deltax; /* There are more x-values than y-values */
769 }
770 else{ /* There is at least one y-value for every x-value */
771 xinc2 = 0; /* Don't change the x for every iteration */
772 yinc1 = 0; /* Don't change the y when numerator >= denominator */
773 den = deltay;
774 num = deltay / 2;
775 num_add = deltax;
776 num_pixels = deltay; /* There are more y-values than x-values */
777 }
778
779 for (curpixel = 0; curpixel <= num_pixels; curpixel++){
780 lcd_drawPixel(hlcd, x, y, pDrawProp->TextColor); /* Draw the current pixel */
781 num += num_add; /* Increase the numerator by the top of the fraction */
782 if(num >= den){ /* Check if numerator >= denominator */
783 num -= den; /* Calculate the new numerator value */
784 x += xinc1; /* Change the x as appropriate */
785 y += yinc1; /* Change the y as appropriate */
786 }
787 x += xinc2; /* Change the x as appropriate */
788 y += yinc2; /* Change the y as appropriate */
789 }
790}
791
792/*
793 * PLOY-LINE描画
794 * param1 pDrawProp: Pointer to Draw Prop
795 * param2 Points: Pointer to the points array
796 * param3 PointCount: Number of points
797 */
798void
799lcd_drawPolygon(LCD_DrawProp_t *pDrawProp, pPoint Points, uint16_t PointCount)
800{
801 int16_t x = 0, y = 0;
802
803 if(PointCount < 2){
804 return;
805 }
806
807 lcd_drawLine(pDrawProp, Points->X, Points->Y, (Points+PointCount-1)->X, (Points+PointCount-1)->Y);
808 while(--PointCount){
809 x = Points->X;
810 y = Points->Y;
811 Points++;
812 lcd_drawLine(pDrawProp, x, y, Points->X, Points->Y);
813 }
814}
815
Note: See TracBrowser for help on using the repository browser.