SWE-350 TOTP Generator Milestone 5
The DE-10 board has six 7-segment displays, this can be used to display and generate a time based one-time pin (TOTP).
Loading...
Searching...
No Matches
lcd_graphic.c
Go to the documentation of this file.
1// ============================================================================
2// Copyright (c) 2013 by Terasic Technologies Inc.
3// ============================================================================
4//
5// Permission:
6//
7// Terasic grants permission to use and modify this code for use
8// in synthesis for all Terasic Development Boards and Altera Development
9// Kits made by Terasic. Other use of this code, including the selling
10// ,duplication, or modification of any portion is strictly prohibited.
11//
12// Disclaimer:
13//
14// This VHDL/Verilog or C/C++ source code is intended as a design reference
15// which illustrates how these types of functions can be implemented.
16// It is the user's responsibility to verify their design for
17// consistency and functionality through the use of formal
18// verification methods. Terasic provides no warranty regarding the use
19// or functionality of this code.
20//
21// ============================================================================
22//
23// Terasic Technologies Inc
24// 9F., No.176, Sec.2, Gongdao 5th Rd, East Dist, Hsinchu City, 30070. Taiwan
25//
26//
27// web: http://www.terasic.com/
28// email: support@terasic.com
29//
30// ============================================================================
31
32#include <stdio.h>
33#include <unistd.h>
34#include <fcntl.h>
35#include <sys/mman.h>
36#include <math.h>
37#include <stdlib.h>
38#include <string.h>
39#include "hwlib.h"
40#include "lcd_graphic.h"
41#include "LCD_Lib.h"
42#include "font.h"
43
45// lowest level API
46void DRAW_Pixel(LCD_CANVAS *pCanvas, int X, int Y, int Color){
47 int nLine;
48 uint8_t *pFrame, Mask;
49
50 nLine = Y >> 3; //Y/8;
51 Mask = 0x01 << (Y % 8);
52 pFrame = pCanvas->pFrame + pCanvas->Width*nLine + X;
53 if (Color == 0x00)
54 *pFrame &= ~Mask;
55 else
56 *pFrame |= Mask;
57}
58
59
61// high-level API for developer
62
63
64// !!!! noe. this fucntion is LCD hardware depentdent
65void DRAW_Refresh(LCD_CANVAS *pCanvas){
66 LCD_FrameCopy(pCanvas->pFrame);
67}
68
69
70
71
72void DRAW_Line(LCD_CANVAS *pCanvas, int X1, int Y1, int X2, int Y2, int Color){
73 int X_Start, X_End;
74 int Y_Start, Y_End;
75 int x,y, acc=0, inc, x_delta, y_delta;
76
77 if (X1 == X2){
78 if (Y1 <= Y2){
79 Y_Start = Y1;
80 Y_End = Y2;
81 }else{
82 Y_Start = Y2;
83 Y_End = Y1;
84 }
85 for(y=Y_Start;y<Y_End;y++)
86 DRAW_Pixel(pCanvas, X1, y, Color);
87 }else if (Y1 == Y2){
88 if (X1 <= X2){
89 X_Start = X1;
90 X_End = X2;
91 }else{
92 X_Start = X2;
93 X_End = X1;
94 }
95 for(x=X_Start;x<X_End;x++){
96 DRAW_Pixel(pCanvas, x, Y1, Color);
97 }
98 }else if (abs(X1-X2) >= abs(Y1-Y2)){
99 if (X1 <= X2){
100 X_Start = X1;
101 Y_Start = Y1;
102 X_End = X2;
103 Y_End = Y2;
104 }else{
105 X_Start = X2;
106 Y_Start = Y2;
107 X_End = X1;
108 Y_End = Y1;
109 }
110
111 x_delta = X_End - X_Start;
112 y_delta = Y_End - Y_Start;
113 inc = (y_delta >= 0)?1:-1;
114 y_delta = abs(y_delta);
115 acc = x_delta/2;
116 y = Y_Start;
117
118 for(x=X_Start;x<X_End;x++){
119 DRAW_Pixel(pCanvas, x, y, Color);
120 acc += y_delta;
121 if (acc >= x_delta){
122 y += inc;
123 acc -= x_delta;
124 }
125 }
126 }else{
127 if (Y1 <= Y2){
128 X_Start = X1;
129 Y_Start = Y1;
130 X_End = X2;
131 Y_End = Y2;
132 }else{
133 X_Start = X2;
134 Y_Start = Y2;
135 X_End = X1;
136 Y_End = Y1;
137 }
138
139 y_delta = Y_End - Y_Start;
140 x_delta = X_End - X_Start;
141 inc = (x_delta >= 0)?1:-1;
142 x_delta = abs(x_delta);
143 acc = y_delta/2;
144 x = X_Start;
145
146 for(y=Y_Start;y<Y_End;y++){
147 DRAW_Pixel(pCanvas, x, y, Color);
148 acc += x_delta;
149 if (acc >= y_delta){
150 x += inc;
151 acc -= y_delta;
152 }
153 }
154
155 }
156
157
158}
159
160void DRAW_Rect(LCD_CANVAS *pCanvas, int X1, int Y1, int X2, int Y2, int Color){
161 DRAW_Line(pCanvas, X1, Y1, X2, Y1, Color);
162 DRAW_Line(pCanvas, X2, Y1, X2, Y2, Color);
163 DRAW_Line(pCanvas, X2, Y2, X1, Y2, Color);
164 DRAW_Line(pCanvas, X1, Y2, X1, Y1, Color);
165}
166
167void DRAW_Circle(LCD_CANVAS *pCanvas, int x0, int y0, int Radius, int Color){
168 int x = Radius, y = 0;
169 int radiusError = 1-x;
170
171 while(x >= y)
172 {
173 DRAW_Pixel(pCanvas,x + x0, y + y0, Color);
174 DRAW_Pixel(pCanvas,y + x0, x + y0, Color);
175 DRAW_Pixel(pCanvas,-x + x0, y + y0, Color);
176 DRAW_Pixel(pCanvas,-y + x0, x + y0, Color);
177 DRAW_Pixel(pCanvas,-x + x0, -y + y0, Color);
178 DRAW_Pixel(pCanvas,-y + x0, -x + y0, Color);
179 DRAW_Pixel(pCanvas,x + x0, -y + y0, Color);
180 DRAW_Pixel(pCanvas,y + x0, -x + y0, Color);
181
182 y++;
183 if(radiusError<0)
184 radiusError+=2*y+1;
185 else
186 {
187 x--;
188 radiusError+=2*(y-x)+1;
189 }
190 }
191}
192
193
194void DRAW_Clear(LCD_CANVAS *pCanvas, int nValue){
195#if 1
196 int y,x;
197 for(y=0;y<pCanvas->Height;y++){
198 for(x=0;x<pCanvas->Width;x++){
199 DRAW_Pixel(pCanvas, x, y, nValue);
200 }
201 }
202#else
203 int y,x;
204 uint8_t *pFrame, Mask;
205
206
207 pFrame = pCanvas->pFrame;
208 memset(pFrame, 0x00, pCanvas->FrameSize);
209 for(y=0;y<pCanvas->Height;y++){
210 Mask = 0x80;
211 for(x=0;x<pCanvas->Width;x++){
212 if (nValue != 0x00){
213 *pFrame |= Mask;
214 }
215 Mask >>= 1;
216 if (Mask == 0x00){
217 Mask = 0x80;
218 pFrame++;
219 }
220
221 }
222 }
223#endif
224
225
226}
227
228
229#ifdef SUPPORT_LCD_FONT
233
234void DRAW_PrintChar(LCD_CANVAS *pCanvas, int X0, int Y0, char Text, int Color, FONT_TABLE *font_table){
235 unsigned char *pFont;
236 uint8_t Mask;
237 int x, y, p;
238
239 for(y=0;y<2;y++){
240 Mask = 0x01;
241 for(p=0;p<8;p++){
242 pFont = font_table->pBitmap[(unsigned char)Text][y];
243
244// pFont = font_table[y];
245// pFont = (unsigned char *)&(font_table[Text][y][0]);
246// pFont = &(font_courier_new_16x16[(unsigned char)Text][y][0]);
247 for(x=0;x<16;x++){
248 if (Mask & *pFont)
249 DRAW_Pixel(pCanvas, X0+x, Y0+y*8+p, Color);
250 pFont++;
251 }
252 Mask <<= 1;
253 }
254 }
255
256}
257
258void DRAW_PrintString(LCD_CANVAS *pCanvas, int X0, int Y0, char* pText, int Color, FONT_TABLE *font_table){
259
260 int nLen, i;
261
262 nLen = strlen(pText);
263
264 for(i=0;i<nLen;i++){
265 DRAW_PrintChar(pCanvas, X0+i*font_table->FontWidth, Y0, *(pText+i), Color, font_table);
266 }
267
268}
269
270
271#endif //SUPPORT_LCD_FONT
void LCD_FrameCopy(uint8_t *Data)
Definition LCD_Lib.c:101
void DRAW_Circle(LCD_CANVAS *pCanvas, int x0, int y0, int Radius, int Color)
void DRAW_PrintString(LCD_CANVAS *pCanvas, int X0, int Y0, char *pText, int Color, FONT_TABLE *font_table)
void DRAW_Rect(LCD_CANVAS *pCanvas, int X1, int Y1, int X2, int Y2, int Color)
void DRAW_Clear(LCD_CANVAS *pCanvas, int nValue)
void DRAW_PrintChar(LCD_CANVAS *pCanvas, int X0, int Y0, char Text, int Color, FONT_TABLE *font_table)
FONT API ///////////////////////////////////.
void DRAW_Pixel(LCD_CANVAS *pCanvas, int X, int Y, int Color)
Definition lcd_graphic.c:46
void DRAW_Line(LCD_CANVAS *pCanvas, int X1, int Y1, int X2, int Y2, int Color)
Definition lcd_graphic.c:72
void DRAW_Refresh(LCD_CANVAS *pCanvas)
Definition lcd_graphic.c:65
int FontWidth
Definition font.h:26
FONT_BITMAP * pBitmap
Definition font.h:33
uint8_t * pFrame
Definition lcd_graphic.h:16