Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created October 23, 2017 10:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adam-buckley/f591515be832b355b2d0ccb751782a40 to your computer and use it in GitHub Desktop.
Save adam-buckley/f591515be832b355b2d0ccb751782a40 to your computer and use it in GitHub Desktop.
Onion Omega2 - I2C OLED Bouncing Dot using C
* A small demo of a dot bouncing inside a 128x64 OLED screen, using the Onion Omega 2. Note this isn't using the
* OLED expansion, but a cheapo screen I bought from China, using the SSD1306 (as far as I could tell) driver.
* This was only a personal exercise to see how easy it is to setup, it might be handly to someone else learning!
* To compile:
* - install gcc and make (See:
* - install git (Follow this:
* - install python-dev
* - git clone the i2c exp driver (
* - (At the time of writing) Alter the makefile according to this bug report (
* - go into the "i2c-exp-driver" project and compile with `make`
* - copy bin, include, lib directories to /usr
* - compile gist below with `gcc bouncing_dot.c -loniondebug -lonioni2c -lonionoledexp`
* - Run!
* @author Adam Buckley <>
// #include <stdio.h>
#include <unistd.h>
#include <oled-exp.h>
int main() {
// Init the OLED
int status = oledDriverInit();
status |= oledSetDisplayPower(1);
int x = 0, y = 0;
int delta_x = 1, delta_y = 1;
// Run forever
while(1) {
x += delta_x;
y += delta_y;
// When X hits a boundary on the OLED, flip the delta position
if (x == 0 || x == 63) {
delta_x *= -1;
// Do the same for the y coordinate
if (y == 0 || y == 127) {
delta_y *= -1;
// Set cursor to correct position
// The way this works is that while the height of the OLED is 64px, there
// is 8 logical rows, each with 8 addressable pixels. So the code below is just
// working out where a pixel (e.g x = 57) should be placed
// See for more info
status |= oledSetCursorByPixel(((int) x / 8), y);
switch(x % 8) {
case 0:
status |= oledWriteByte(0x01);
case 1:
status |= oledWriteByte(0x02);
case 2:
status |= oledWriteByte(0x04);
case 3:
status |= oledWriteByte(0x08);
case 4:
status |= oledWriteByte(0x10);
case 5:
status |= oledWriteByte(0x20);
case 6:
status |= oledWriteByte(0x40);
case 7:
status |= oledWriteByte(0x80);
// A larger usleep value will make the dot move slower
// Clear the current dot on the screen
status |= oledSetCursorByPixel(((int) x / 8), y);
status |= oledWriteByte(0x00);
// Clear below for some reason will cause the screen to not print anything
// status |= oledClear();
return 0;
Copy link

I wrote the code above on the Omega itself, nano has its limits, please let me know if you have any issues.

Copy link

Awesome project, love it!
If you take a peek at /usr/lib on the Omega, you'll see that it already has the shared objects for liboniondebug, libonioni2c, and libonionoledexp.
Haven't tried it myself, but it should work if you just grab the header files from the i2c-exp-driver repo!

Copy link

Thanks @greenbreakfast!

I had a feeling that was the case regarding libonionoledexp, my knowledge of C and Omega internals are pretty limited so I just followed the tutorial. I'll fire up my other Omega when I get home and see if I can do this without having to compile external libraries.

Copy link

A third way to do it: cross-compile your program. We just put out a guide:

Copy link

Thanks for the very clear compilation instructions.

However, I just noticed that the OLED Expansion Python Module commands to setCursor stopped working after I followed your compilation steps (gcc, make, git, python-dev installs).

In a Python shell, the OLED command "oledExp.setCursor(0,0)" (for example) now returns: "IOError: I2C transaction failed." Same for setCursorByPixel(...)." I tried the Sierpinski triangle example in the i2c-exp-driver Github repo, and it fails as well, at the same point. Other commands still work (driverInit, clear, write, wWriteByte, etc.).

Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment