Adafruit PiOLEDΒΆ

Controlling the Adafruit PiOLED with C. Note that I2C must be enabled (the simplest way to do this is with raspi-config and a reboot).

Click to dowlnoad PiOled.cc or view the code:

  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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Use some C++ constructs but mostly C
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <assert.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <unistd.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
// build with: gcc PiOled.cc -lwiringPi -lm -o PiOled
////////////////////////////////////////////////////////////////////////////////////////////////////
#define USE_WRITE 1                 // Can use fd write (ssd1306 doesn't support reads over i2c)
#define USE_BLOCK (0 && !USE_WRITE) // If not fd write, can use block transfers
////////////////////////////////////////////////////////////////////////////////////////////////////
#define TIMING 0 // Print time from start of frame write to end (not quite inverse framerate)
////////////////////////////////////////////////////////////////////////////////////////////////////
#define OLED_WD 128
#define OLED_HT  32
////////////////////////////////////////////////////////////////////////////////////////////////////
// Boilerplate - things I'm used to...
typedef unsigned char      u8;
typedef unsigned short     u16;
typedef unsigned long      u32;
typedef unsigned long long u64;
typedef float              f32;
typedef double             f64;
static_assert(sizeof(u8)  == 1);
static_assert(sizeof(u16) == 2);
static_assert(sizeof(u32) == 4);
static_assert(sizeof(u64) == 8);
static_assert(sizeof(f32) == 4);
static_assert(sizeof(f64) == 8);
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace ssd1306
{
  // These are commands written to register 0x00; parameters are written to the same register.
  // Data for the display is written to register 0x40.
  const u8 display_off           = 0xAE;
  const u8 display_on            = 0xAF;
  const u8 set_contrast          = 0x81;
  const u8 display_from_ram      = 0xA4;
  const u8 normal_display        = 0xA6; // as opposed to inverse
  const u8 inverse_display       = 0xA7; // as opposed to normal
  const u8 setdisplayoffset      = 0xD3;
  const u8 setcompins            = 0xDA;
  const u8 setvcomdetect         = 0xDB;
  const u8 setdisplayclockdiv    = 0xD5;
  const u8 setprecharge          = 0xD9;
  const u8 setmultiplex          = 0xA8;
  const u8 setlowcolumn          = 0x00;
  const u8 sethighcolumn         = 0x10;
  const u8 setstartline          = 0x40;
  const u8 memorymode            = 0x20;
  const u8 columnaddr            = 0x21;
  const u8 pageaddr              = 0x22;
  const u8 comscaninc            = 0xC0;
  const u8 comscandec            = 0xC8;
  const u8 segment_remap_none    = 0xA0;
  const u8 segment_remap_reverse = 0xA1;
  const u8 chargepump            = 0x8D;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// Random number in [0,1) range
f64 rand01()
{
  return rand() / (f64(RAND_MAX) + 1.0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
u64 nanoseconds()
{
  struct timespec ts;
  clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
  return u64(ts.tv_sec) * 1000000000ULL + u64(ts.tv_nsec);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
struct PiOled
{
  int fd;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void PiOledCommand(PiOled* po, u8 cmd)
{
#if USE_WRITE
  u8 buf[2] = { 0x00, cmd };
  write( po->fd, buf, 2 );
#else
  wiringPiI2CWriteReg8(po->fd, 0x00, cmd);
#endif
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void PiOledCommand(PiOled* po, u8 cmd, u8 val)
{
  PiOledCommand(po, cmd);
  PiOledCommand(po, val);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void PiOledCommand(PiOled* po, u8 cmd, u8 v0, u8 v1)
{
  PiOledCommand(po, cmd);
  PiOledCommand(po, v0);
  PiOledCommand(po, v1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void PiOledNativeImage(PiOled* po, u8* data)
{
#if TIMING
  u64 start = nanoseconds();
#endif

#if USE_WRITE
  u8 buf[OLED_WD * OLED_HT / 8 + 1];
  buf[0] = 0x40; // Write data
  memcpy(&buf[1], data, OLED_WD * OLED_HT / 8);
  write(po->fd, buf, OLED_WD * OLED_HT / 8 + 1);
#elif USE_BLOCK
  // wiringPi doesn't support this call
  i2c_smbus_data i2c_data;
  i2c_data.block[0] = 32;           // 32 bytes per ioctl call is the max

  i2c_smbus_ioctl_data i2c_ioctl_data =
  {
    I2C_SMBUS_WRITE,
    0x40, // Writing data 
    I2C_SMBUS_I2C_BLOCK_DATA, // Block data but without length sent on wire 
    &i2c_data,
  };

  for(u32 i = 0; i < OLED_WD * OLED_HT / 8; i += 32)
  {
    for(u32 j = 0; j < 32; ++ j)
      i2c_data.block[j + 1] = data[i + j];
    ioctl( po->fd, I2C_SMBUS, &i2c_ioctl_data);
  }
#else
  for(u32 i = 0; i < OLED_WD * OLED_HT / 8; ++ i)
    wiringPiI2CWriteReg8(po->fd, 0x40, data[i]);
#endif
  
#if TIMING
  u64 end = nanoseconds();
  printf("%f ms\n", int(end - start) / 1000.0 / 1000.0);
#endif
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void PiOledClear(PiOled* po)
{
  u8 data[OLED_WD * OLED_HT / 8];
  bzero(data, OLED_WD * OLED_HT / 8); 
  PiOledNativeImage(po, data);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void PiOledOff(PiOled* po)
{
  // Put display to sleep
  PiOledCommand(po,ssd1306::display_off);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline bool PiOledInlz(PiOled* po)
{
  // Check i2c address
  int fd = wiringPiI2CSetup(0x3C);
  if (fd < 0)
    return false;

  po->fd = fd;

  // Startup sequence found in Adafruit's python sample
  // https://github.com/adafruit/Adafruit_Python_SSD1306/blob/master/Adafruit_SSD1306/SSD1306.py
  PiOledCommand(po,ssd1306::display_off);
  PiOledCommand(po,ssd1306::setdisplayclockdiv, 0x80);
  PiOledCommand(po,ssd1306::setmultiplex, 0x1F);
  PiOledCommand(po,ssd1306::setdisplayoffset, 0x0);
  PiOledCommand(po,ssd1306::setstartline);
  PiOledCommand(po,ssd1306::chargepump, 0x14);
  PiOledCommand(po,ssd1306::memorymode, 0x00);
  PiOledCommand(po,ssd1306::segment_remap_reverse); // Headers at top, bits start left
  PiOledCommand(po,ssd1306::comscandec);
  PiOledCommand(po,ssd1306::setcompins, 0x02);
  PiOledCommand(po,ssd1306::set_contrast, 0x8F);
  PiOledCommand(po,ssd1306::setprecharge, 0xF1);
  PiOledCommand(po,ssd1306::setvcomdetect, 0x40);
  PiOledCommand(po,ssd1306::display_from_ram);
  PiOledCommand(po,ssd1306::normal_display);
  // Just done once - updating entire frames seems to repeat just fine.
  PiOledCommand(po, ssd1306::columnaddr, 0, 128 - 1);
  PiOledCommand(po, ssd1306::pageaddr, 0, 32 / 8 - 1);
  PiOledClear(po);
  PiOledCommand(po,ssd1306::display_on);

  return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void PiOledSetBitNative(u8* data, u8 x, u8 y, bool b)
{
  u32 addr   = x + (y >> 3) * 128;
  u8  mask   = ~(1 << (y & 7));
  u8  bit    = b << (y & 7);
  data[addr] = (data[addr] & mask) | bit;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
PiOled po;
////////////////////////////////////////////////////////////////////////////////////////////////////
void clear_atexit()
{
  PiOledOff(&po);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void sigint_handler(int dummy)
{
  // Make sure the atext clear gets run
  exit(1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Particles
const int n_ps = 64;

struct p
{
  f32 x;
  f32 y;
  f32 vx;
  f32 vy;
};

p ps[n_ps];
////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char** arg)
{
  // Catch control-C or other exit and put display to sleep
  atexit(clear_atexit);
  signal(SIGINT, sigint_handler);
  
  if(!PiOledInlz(&po))
  {
    printf("Error in setup\n");
    return -1;
  }

  u8 data[128 * 32 / 8];

  srand( u32((nanoseconds() >> 10) ^ nanoseconds()) );

  for(u32 i = 0; i < n_ps; ++ i)
  {
    ps[i].x = OLED_WD * rand01();  
    ps[i].y = OLED_HT * rand01();  

    // Biases toward diagonal velocities
    ps[i].vx = rand01() - 0.5f;  
    ps[i].vy = rand01() - 0.5f;  
  }
  
  while(1)
  {
    bzero(data, OLED_WD * OLED_HT / 8);

    for(u32 i = 0; i < n_ps; ++ i)
    {
      ps[i].x += ps[i].vx;
      ps[i].y += ps[i].vy;
      
      if(ps[i].x > OLED_WD - 1.0f)
      {
        ps[i].x = OLED_WD - 1.0f;
        ps[i].vx = -ps[i].vx;
      }
      if(ps[i].x < 0.0f)
      {
        ps[i].x = 0.0f;
        ps[i].vx = -ps[i].vx;
      }
      if(ps[i].y > OLED_HT - 1.0f)
      {
        ps[i].y = OLED_HT - 1.0f;
        ps[i].vy = -ps[i].vy;
      }
      if(ps[i].y < 0.0f)
      {
        ps[i].y = 0.0f;
        ps[i].vy = -ps[i].vy;
      }

      PiOledSetBitNative(data, ps[i].x, ps[i].y, 1);
    }
    
    PiOledNativeImage(&po, data);
  }
  
  // Any parameter on the command line will clear through atexit
  if(argc > 1)
    exit(0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////