Revision 3e2454a3 main.c

View differences:

main.c
36 36
static inline void drawEverything(void);
37 37
inline void printLineBasic(int16_t line, uint8_t round, uint8_t color);
38 38

  
39
inline void setPixelAtPos(uint8_t x, uint8_t y){
40
	lines[y] |= (1<<x);
41
}
42

  
43
// yes, this is right, we need 16bit input and output 8bit, "it fits"(tm)
44
static inline int16_t i16abs(int16_t x) { return x < 0 ? -x : x; }
45

  
46
// draw a line from the first coordinate to the second one
47
void drawLine(int16_t x_from, int16_t y_from,
48
		int16_t x_to  , int16_t y_to) {
49
	int16_t dx = i16abs((int16_t)x_to - (int16_t)x_from);
50
	int16_t dy = i16abs((int16_t)y_to - (int16_t)y_from);
51

  
52
	int16_t sx = x_from < x_to ? 1 : -1;
53
	int16_t sy = y_from < y_to ? 1 : -1;
54

  
55
	int16_t err = dx - dy;
56

  
57
	setPixelAtPos(x_from, y_from);
58

  
59
	while(x_from != x_to || y_from != y_to) {
60
		int16_t e2 = 2 * err;
61

  
62
		if(e2 > -dy) {
63
			err -= dy;
64
			x_from += sx;
65
		}
66

  
67
		if(e2 < dx) {
68
			err += dx;
69
			y_from += sy;
70
		}
71

  
72
		setPixelAtPos(x_from, y_from);
73
	}
74
}
75

  
76
void clearBuffer(){
77
	for(uint8_t i = 0; i < 90; i++){
78
		lines[i] = 0;
79
	}
80
}
81

  
39 82
int main(){
40 83
	// hardware init
41 84
	// clock, data and output enable
42 85
	DDR(WALLBOARD_PORT) |= (1<<CLOCK_PIN) | (1<<DATA_RED_PIN) | (1<<DATA_GREEN_PIN) | (1<<OUTPUT_ENABLE_PIN);
43 86
	// address lines:
44 87
	DDR(ADDRESS_PORT) |= (1<<A3) | (1<<A2) | (1<<A1) | (1<<A0);
45

  
88
	
89
	drawLine(0,0, 15, 89);
90
	uint8_t x1 = 0;
91
	uint8_t y1 = 0;
92
	uint8_t x2 = 15;
93
	uint8_t y2 = 89;
46 94
	while(1){
47 95
		drawEverything();
96
		clearBuffer();
97
		drawLine(x1, y1, x2, y2);
98
		if(x1 < 15 && y2 == 89){
99
			x1++;
100
			x2--;
101
		}
102
		else if(y1 != 89){
103
			y1++;
104
			y2--;
105
		}
106
		else{
107
			x1 = 0;
108
			y1 = 0;
109
			x2 = 15;
110
			y2 = 89;
111
		}
48 112
	}
49 113
}
50 114

  

Also available in: Unified diff