File: atmega32_ssd1306.c

package info (click to toggle)
simavr 1.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 20,268 kB
  • sloc: ansic: 362,806; makefile: 622; ruby: 70; python: 63
file content (170 lines) | stat: -rw-r--r-- 3,518 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 atmega32_ssd1306.c

 Copyright 2014 Doug Szumski <d.s.szumski@gmail.com>

 This file is part of simavr.

 simavr is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 simavr is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with simavr.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>
#include <avr/io.h>
#include <util/delay.h>

#undef F_CPU
#define F_CPU 7380000

#include "avr_mcu_section.h"
AVR_MCU(F_CPU, "atmega32");

#include "ssd1306.h"
#include "images.h"

#define DEFAULT_DELAY 10000

void
spi_init (void)
{
	DDRB |= (1 << PB4) | (1 << PB5) | (1 << PB7) | (1 << PB3) | (1 << PB1);
	SPCR |= (1 << SPE) | (1 << MSTR);
	// Double the speed
	SPSR |= (1 << SPI2X);
}

void
demo_set_contrast (void)
{
	for (uint16_t contrast = 0; contrast <= 255; contrast++)
	{
		PORTB ^= (1 << PB0);
		ssd1306_set_contrast (contrast);
		_delay_ms (DEFAULT_DELAY/200);
	}
}

void
demo_show_image (void)
{
	ssd1306_write_image_fb (logo);
	ssd1306_display_fb ();
	_delay_ms (DEFAULT_DELAY);
}

/* Draw some dots by writing directly to the VRAM */
void
demo_set_byte_direct (void)
{
	ssd1306_clear_screen ();
	uint8_t x = 0;
	for (uint8_t page = 0; page < SSD1306_PIXEL_PAGES; ++page)
	{
		for (x = 0; x < SSD1306_X_PIXELS; x += 2)
		{
			ssd1306_write_byte (x, page, 0xAA);
		}
	}
	_delay_ms (DEFAULT_DELAY);
}

/* Draw some stripes by setting individual pixels on the frame buffer */
void
demo_set_pixels (void)
{
	ssd1306_clear_fb ();
	uint8_t x = 0;
	for (uint8_t y = 0; y < SSD1306_Y_PIXELS; ++y)
	{
		for (x = 0; x < SSD1306_X_PIXELS; x += 2)
		{
			ssd1306_set_pixel_fb (x, y, PIXEL_STATE_ON);
		}
	}
	ssd1306_display_fb ();
	_delay_ms (DEFAULT_DELAY);
}

void
demo_clear_screen (void)
{
	// Turn all pixels on
	memset (ssd1306_frame_buffer_g, 0xFF, SSD1306_PIXEL_BYTES);
	ssd1306_display_fb ();
	_delay_ms (DEFAULT_DELAY);

	// Clear screen
	ssd1306_clear_screen ();
	_delay_ms (DEFAULT_DELAY);
}

void
demo_set_power_state (void)
{
	ssd1306_set_power_state (POWER_STATE_SLEEP);
	_delay_ms (DEFAULT_DELAY);
	ssd1306_set_power_state (POWER_STATE_ON);
	_delay_ms (DEFAULT_DELAY);
}

void
demo_rotate_display (void)
{
	for (uint8_t i = DISP_ORIENT_NORMAL;
	                i <= DISP_ORIENT_UPSIDE_DOWN_MIRRORED; ++i)
	{
		ssd1306_set_display_orientation (i);
		ssd1306_write_image_fb (logo);
		ssd1306_display_fb ();
		_delay_ms (DEFAULT_DELAY);
	}
}

void
demo_invert_image ()
{
	ssd1306_set_display_orientation (DISP_ORIENT_NORMAL);
	for (uint8_t i = DISPLAY_MODE_NORMAL; i <= DISPLAY_MODE_INVERTED; ++i)
	{
		ssd1306_set_display_mode (i);
		ssd1306_write_image_fb (logo);
		ssd1306_display_fb ();
		// Check inverted contrast works
		demo_set_contrast ();
	}
}

int
main ()
{
	spi_init ();

	/*
	 * Demonstrate the virtual part functionality. Runs approx 10 times
	 * faster on an i7-3740QM CPU @ 2.70GHz than in real life.
	 */
	for (;;)
	{
		ssd1306_init_display ();

		demo_show_image ();
		demo_set_power_state ();
		demo_set_contrast ();
		demo_set_byte_direct ();
		demo_set_pixels ();
		demo_clear_screen ();
		demo_rotate_display ();
		demo_invert_image ();
	}

}