Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSD1306 i2c example #402

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 57 additions & 0 deletions examples/board_ssd1306_i2c/Makefile
@@ -0,0 +1,57 @@
#
# Copyright 2008-2011 Michel Pollet <buserror@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/>.

target= ssd1306demo
firm_src = ${wildcard at*${board}.c}
firmware = ${firm_src:.c=.axf}
simavr = ../../

IPATH = .
IPATH += ../shared
IPATH += ../parts
IPATH += ${simavr}/include
IPATH += ${simavr}/simavr/sim
IPATH += ${simavr}/simavr/sim
IPATH += ${simavr}/simavr/sim

VPATH = .
VPATH += ../shared
VPATH += ../parts

LDFLAGS += -lpthread

include ../Makefile.opengl

all: obj atmega32_ssd1306.axf ${target}

atmega32_ssd1306.axf: atmega32_ssd1306.c ssd1306.c images.c twimaster.c

include ${simavr}/Makefile.common

board = ${OBJ}/${target}.elf

${board} : ${OBJ}/ac_input.o
${board} : ${OBJ}/ssd1306_virt.o
${board} : ${OBJ}/ssd1306_glut.o
${board} : ${OBJ}/${target}.o

${target}: ${board}
@echo $@ done

clean: clean-${OBJ}
rm -rf *.hex *.a *.axf ${target} *.vcd .*.swo .*.swp .*.swm .*.swn
10 changes: 10 additions & 0 deletions examples/board_ssd1306_i2c/README
@@ -0,0 +1,10 @@
board_ssd1306

(C) 2014 Doug Szumski <d.s.szumski@gmail.com>

This sample code demonstrates the use of the SSD1306 OLED driver. It
runs identically in real life.

The part has been tested using only the supplied driver.

In the future it could be extended to allow parallel addressing.
172 changes: 172 additions & 0 deletions examples/board_ssd1306_i2c/atmega32_ssd1306.c
@@ -0,0 +1,172 @@
/*
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"
#include "../shared/twimaster.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)
{
//do while loop needed to iterate through full range of uint8_t
uint8_t contrast=0;
do {
ssd1306_set_contrast (contrast);
_delay_ms (DEFAULT_DELAY/200);
contrast++;
} while (contrast>0);
}

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 ()
{
i2c_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 ();
}

}
61 changes: 61 additions & 0 deletions examples/board_ssd1306_i2c/images.c
@@ -0,0 +1,61 @@
/*
images.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 "images.h"

/* 128x64 run length encoded simavr logo */
const uint8_t logo[] PROGMEM =
{
0x0, 0x0, 0xff, 0x0, 0x0, 0x7, 0xfe, 0xfe, 0x4, 0xe, 0x6, 0x6, 0x8, 0xe, 0xfe,
0xfe, 0x3, 0xfc, 0x0, 0x0, 0x5, 0x54, 0xfe, 0xfe, 0x2, 0x0, 0x0,
0x3, 0xfe, 0xfe, 0x2, 0xff, 0x6, 0x1e, 0x7e, 0xfc, 0xe0, 0xc0,
0x0, 0x80, 0xc0, 0xf0, 0x7c, 0x3e, 0xe, 0x5e, 0xff, 0xfe, 0xfe,
0x2, 0xff, 0xfe, 0xfe, 0x2, 0x0, 0x0, 0x6, 0xfe, 0xfe, 0x3, 0x6,
0x6, 0xa, 0x2e, 0xfe, 0xfe, 0x3, 0x0, 0x0, 0x2, 0xfe, 0xff,
0xba, 0x0, 0x0, 0x7, 0xf4, 0xff, 0xff, 0x5, 0xfe, 0x0, 0x0, 0x2,
0xfe, 0xff, 0xff, 0x2, 0xf, 0x7, 0x7, 0xa, 0xff, 0xff, 0x3, 0x0,
0x0, 0xf, 0x7f, 0x7f, 0x4, 0x70, 0x60, 0x60, 0x8, 0xe0, 0xe0,
0x2, 0xe1, 0xe1, 0x2, 0xc0, 0x0, 0x0, 0x2, 0x80, 0x80, 0x3,
0xe5, 0xff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0x3, 0xf, 0xf, 0x6, 0x0, 0x0, 0x3, 0x2, 0xf, 0xf, 0x6, 0x0,
0x0, 0x2, 0x7, 0xf, 0xf, 0x5, 0x7, 0x0, 0x0, 0xa, 0x3, 0xf, 0xf,
0x3, 0x0, 0x0, 0x6, 0x3, 0x7, 0xf, 0xe, 0xe, 0x2, 0xf, 0x7, 0x3,
0x3, 0x2, 0x1, 0x0, 0x0, 0x5, 0x7, 0xf, 0xf, 0x5, 0x7, 0x0, 0x0,
0x7, 0x7, 0xf, 0xf, 0x5, 0x7, 0x0, 0x0, 0xff, 0x0, 0x0, 0x6
};


30 changes: 30 additions & 0 deletions examples/board_ssd1306_i2c/images.h
@@ -0,0 +1,30 @@
/*
images.h

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/>.
*/

#ifndef IMAGES_H_
#define IMAGES_H_

#include <stdint.h>
#include <avr/pgmspace.h>

extern const uint8_t logo[] PROGMEM;

#endif /* IMAGES_H_ */