Skip to content

Commit fd536e5

Browse files
committed
Added a column-major demo for lcddrvce
1 parent 4772fde commit fd536e5

File tree

3 files changed

+218
-0
lines changed

3 files changed

+218
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
obj/
2+
bin/
3+
src/gfx/*.c
4+
src/gfx/*.h
5+
src/gfx/*.8xv
6+
.DS_Store
7+
convimg.yaml.lst
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# ----------------------------
2+
# Makefile Options
3+
# ----------------------------
4+
5+
NAME = DEMO
6+
ICON = icon.png
7+
DESCRIPTION = "CE C Toolchain Demo"
8+
COMPRESSED = NO
9+
10+
CFLAGS = -Wall -Wextra -Oz
11+
CXXFLAGS = -Wall -Wextra -Oz
12+
13+
# ----------------------------
14+
15+
include $(shell cedev-config --makefile)
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
#include <stdint.h>
2+
#include <stdlib.h>
3+
#include <math.h>
4+
#include <ti/getcsc.h>
5+
#include <graphx.h>
6+
#include <sys/lcd.h>
7+
#include <lcddrvce.h>
8+
9+
#define ORG (-50)
10+
#define DEG (6.0)
11+
#define ANG (DEG * M_PI / 180.0)
12+
13+
typedef struct {
14+
float sin_ang;
15+
float cos_ang;
16+
float midx1;
17+
float midy1;
18+
float midx2;
19+
float midy2;
20+
} cube_t;
21+
22+
static float face1[4][2] = {
23+
{ 140, 45 },
24+
{ 240, 45 },
25+
{ 240, 145 },
26+
{ 140, 145 },
27+
};
28+
29+
static float face2[4][2] = {
30+
{ 140 + ORG, 45 - ORG },
31+
{ 240 + ORG, 45 - ORG },
32+
{ 240 + ORG, 145 - ORG },
33+
{ 140 + ORG, 145 - ORG },
34+
};
35+
36+
uint8_t line_color;
37+
38+
void draw_line(uint24_t x0, uint8_t y0, uint24_t x1, uint8_t y1);
39+
40+
void rotate(cube_t *cube);
41+
42+
int main(void) {
43+
static cube_t cube;
44+
45+
/* Set up the intial cube positions */
46+
cube.sin_ang = sin(ANG);
47+
cube.cos_ang = cos(ANG);
48+
cube.midx1 = (face1[0][0] + face1[1][0]) / 2;
49+
cube.midy1 = (face1[1][1] + face1[2][1]) / 2;
50+
cube.midx2 = (face2[0][0] + face2[1][0]) / 2;
51+
cube.midy2 = (face2[1][1] + face2[2][1]) / 2;
52+
53+
/* Start the graphics routines */
54+
gfx_Begin();
55+
gfx_SetDrawBuffer();
56+
57+
/* Setup Column-Major mode (240x320) */
58+
lcd_Init();
59+
lcd_SetRamAccessOrder(LCD_MADCTL_DEFAULT ^ LCD_MV);
60+
lcd_SetColumnAddress(0, LCD_HEIGHT - 1);
61+
lcd_SetRowAddress(0, LCD_WIDTH - 1);
62+
63+
/* Loop until a key is pressed */
64+
do {
65+
/* Rotate the cube */
66+
rotate(&cube);
67+
68+
/* Swap the buffer with the screen */
69+
gfx_SwapDraw();
70+
71+
} while (!os_GetCSC());
72+
73+
/* Restore Row-Major mode (320x240) */
74+
lcd_SetRamAccessOrder(LCD_MADCTL_DEFAULT);
75+
lcd_SetColumnAddress(0, LCD_WIDTH - 1);
76+
lcd_SetRowAddress(0, LCD_HEIGHT - 1);
77+
lcd_Cleanup();
78+
79+
/* End graphics drawing */
80+
gfx_End();
81+
82+
return 0;
83+
}
84+
85+
/* Cube rotation */
86+
void rotate(cube_t *cube) {
87+
static uint8_t drawnTimes = 0;
88+
89+
/* Compute the new vertex coordinates */
90+
for (uint8_t i = 0; i < 4; ++i) {
91+
float tmp1, tmp2;
92+
93+
tmp1 = face1[i][0] - cube->midx1;
94+
tmp2 = face1[i][1] - cube->midy1;
95+
96+
face1[i][0] = cube->midx1 + (tmp1 * cube->cos_ang) - (tmp2 * cube->sin_ang);
97+
face1[i][1] = cube->midy1 + (tmp1 * cube->sin_ang) + (tmp2 * cube->cos_ang);
98+
99+
tmp1 = face2[i][0] - cube->midx2;
100+
tmp2 = face2[i][1] - cube->midy2;
101+
102+
face2[i][0] = cube->midx2 + (tmp1 * cube->cos_ang) - (tmp2 * cube->sin_ang);
103+
face2[i][1] = cube->midy2 + (tmp1 * cube->sin_ang) + (tmp2 * cube->cos_ang);
104+
}
105+
106+
/* Clear the old cube */
107+
gfx_FillScreen(255);
108+
109+
/* Every 200 draws, change the color */
110+
if (drawnTimes % 200 == 0) {
111+
line_color = rand();
112+
}
113+
drawnTimes++;
114+
115+
/* Draw the new cube */
116+
for (uint8_t i = 0; i < 4; ++i) {
117+
uint8_t j = (i + 1) & 3;
118+
draw_line(face1[i][0], face1[i][1], face1[j][0], face1[j][1]);
119+
draw_line(face2[i][0], face2[i][1], face2[j][0], face2[j][1]);
120+
draw_line(face1[i][0], face1[i][1], face2[i][0], face2[i][1]);
121+
}
122+
}
123+
124+
/* Bresenham line drawing code */
125+
126+
static void draw_line0(int x0, int y0, int x1, int y1) {
127+
int dX = x1 - x0;
128+
int dY = y1 - y0;
129+
int y_incr = 1;
130+
if (dY < 0) {
131+
y_incr = -1;
132+
dY = -dY;
133+
}
134+
int dD = 2 * dY - dX;
135+
const int dD_jump = 2 * (dY - dX);
136+
dY *= 2;
137+
138+
uint8_t* buffer = (uint8_t*)gfx_vbuffer + (x0 * LCD_HEIGHT) + y0;
139+
const uint8_t color = line_color;
140+
141+
for (uint24_t x = (uint24_t)dX; x > 0; x--) {
142+
*buffer = color;
143+
/* Moves right one pixel */
144+
buffer += LCD_HEIGHT;
145+
if (dD > 0) {
146+
buffer += y_incr;
147+
dD += dD_jump;
148+
} else {
149+
dD += dY;
150+
}
151+
}
152+
}
153+
154+
static void draw_line1(int x0, int y0, int x1, int y1) {
155+
int dX = x1 - x0;
156+
int dY = y1 - y0;
157+
int x_incr = LCD_HEIGHT;
158+
if (dX < 0) {
159+
x_incr = -LCD_HEIGHT;
160+
dX = -dX;
161+
}
162+
int dD = (2 * dX) - dY;
163+
const int dD_jump = 2 * (dX - dY);
164+
dX *= 2;
165+
166+
uint8_t* buffer = (uint8_t*)gfx_vbuffer + (x0 * LCD_HEIGHT) + y0;
167+
const uint8_t color = line_color;
168+
169+
for (uint8_t y = (uint8_t)dY; y > 0; y--) {
170+
*buffer = color;
171+
/* Moves down one pixel */
172+
++buffer;
173+
if (dD > 0) {
174+
buffer += x_incr;
175+
dD += dD_jump;
176+
} else {
177+
dD += dX;
178+
}
179+
}
180+
}
181+
182+
void draw_line(uint24_t x0, uint8_t y0, uint24_t x1, uint8_t y1) {
183+
if (abs((int)y1 - (int)y0) < abs((int)x1 - (int)x0)) {
184+
if (x0 > x1) {
185+
draw_line0(x1, y1, x0, y0);
186+
} else {
187+
draw_line0(x0, y0, x1, y1);
188+
}
189+
} else {
190+
if (y0 > y1) {
191+
draw_line1(x1, y1, x0, y0);
192+
} else {
193+
draw_line1(x0, y0, x1, y1);
194+
}
195+
}
196+
}

0 commit comments

Comments
 (0)