You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • You could try this:

    var exports = {};
    
    var C = {
     OLED_CMD                   : 0x80,
     OLED_WIDTH                 : 128,
     OLED_HEIGHT                : 64
    };
    
    // export
    exports.connect = function(spi, dc, callback) {
     var oled = Graphics.createArrayBuffer(128,64,1,{verĀ­tical_byte : true});
     var w = function(c,d) {
       digitalWrite(dc,0);
       spi.write(c);
       digitalWrite(dc,1);
       spi.write(c);
     };
     var extVcc=false;
     
     // configure the OLED
     digitalWrite(dc,0);// command
     spi.write([ 0xAe, // disp off
                 0xD5, // clk div
                 0x80, // suggested ratio
                 0xA8, C.OLED_HEIGHT-1, // set multiplex
                 0xD3,0x0, // display offset
                 0x40, // start line
                 0x8D,extVcc?0x10:0x14, // charge pump
                 0x20,0x0, // memory mode
                 0xA1, // seg remap 1
                 0xC8, // comscandec
                 0xDA,0x12, // set compins
                 0x81,extVcc?0x9F:0xCF, // set contrast
                 0xD9,extVcc?0x22:0xF1, // set precharge
                 0xDb,0x40, // set vcom detect
                 0xA4, // display all on
                 0xA6, // display normal (non-inverted)
                 0xAf // disp on
                ]);
       digitalWrite(dc,1);// data
     
     // if there is a callback, call it now(ish)
     if (callback !== undefined) setTimeout(callback, 10);
      
     // write to the screen
     oled.flip = function() { 
       // set how the data is to be sent (whole screen)
       digitalWrite(dc,0);// command
       spi.write([0x21, // columns
         0, C.OLED_WIDTH-1,
         0x22, // rows
         0, 7]);
       digitalWrite(dc,1);// data
       spi.write(this.buffer);
      };
     
     // return graphics
     return oled;
    };
    
    
    
    B3.reset(); // GND
    B4.set(); // VCC
    
    digitalPulse(B7,0,10); // Reset
    
    setTimeout(function() {
      var s = new SPI();
      s.setup({mosi: B6 /* D1 */, sck:B5 /* D0 */});
      var g = exports.connect(s, A8 /* DC */, function(){
       // write some text
       g.drawString("Hello World!",2,2);
       // write to the screen
       g.flip(); 
      }
     );
    },50);
    

    Just gave it a go and it seems to work. I'll have a go at tidying it up into a general purpose I2C+SPI module - I shamelessly copied the magic initialisation incantation from Adafruit and it seems to work - looks like some oleds needed more setup than I was giving them

About

Avatar for Gordon @Gordon started