11package com .pi4j .drivers .display .graphics .ssd1306 ;
22
3+ import com .pi4j .drivers .display .graphics .GraphicsDisplay ;
34import com .pi4j .drivers .display .graphics .GraphicsDisplayDriver ;
45import com .pi4j .drivers .display .graphics .GraphicsDisplayInfo ;
56import com .pi4j .drivers .display .graphics .PixelFormat ;
6- import com .pi4j .io .i2c .I2C ;
7+ import com .pi4j .io .IODataWriter ;
8+ import com .pi4j .io .OnOffWrite ;
79
810import org .slf4j .Logger ;
911import org .slf4j .LoggerFactory ;
1012
13+ import java .io .Closeable ;
14+ import java .io .IOException ;
15+
1116public class Ssd1306Driver implements GraphicsDisplayDriver {
1217
1318 private static Logger log = LoggerFactory .getLogger (Ssd1306Driver .class );
@@ -28,14 +33,25 @@ public class Ssd1306Driver implements GraphicsDisplayDriver {
2833 private static final int WITH_ONE_COMMAND = 0x00 ;
2934 private static final int WITH_DATA_ONLY = 0x40 ;
3035
31- private final I2C i2c ;
36+ private final IODataWriter writer ;
3237 private final GraphicsDisplayInfo displayInfo ;
33- private byte [] page_buffer ;
38+ private final byte [] page_buffer ;
39+ private final OnOffWrite <?> dc ;
40+ private final byte [] commandBuffer = new byte [8 ];
41+
42+
43+ /** Constructor for I2c, using WITH_ONE_COMMAND / WITH_DATA instead of dc to switch between data and command mode. */
44+ public Ssd1306Driver (IODataWriter writer ) {
45+ this (writer , null );
46+ }
3447
35- public Ssd1306Driver (I2C i2c ) {
36- this .i2c = i2c ;
37- this .displayInfo = new GraphicsDisplayInfo (128 , 64 , PixelFormat .MONOCHROME );
48+ /** Constructor for SPI with a dedicated dc pin. */
49+ public Ssd1306Driver (IODataWriter writer , OnOffWrite <?> dc ) {
50+ this .writer = writer ;
51+ this .dc = dc ;
52+ this .displayInfo = new GraphicsDisplayInfo (128 , 64 , PixelFormat .MONOCHROME , 8 , GraphicsDisplay .Rotation .ROTATE_0 );
3853 page_buffer = new byte [128 * 8 ]; // 1024
54+ commandBuffer [0 ] = WITH_ONE_COMMAND ;
3955 init ();
4056 }
4157
@@ -92,32 +108,34 @@ public void clear() {
92108 sendBuffer ();
93109 }
94110
111+ private void writeCommand (int length ) {
112+ if (dc == null ) {
113+ writer .write (commandBuffer , 0 , length + 1 );
114+ } else {
115+ dc .off ();
116+ writer .write (commandBuffer , 1 , length );
117+ }
118+ }
119+
95120 private void command (int x ) {
96121 log .debug ("Command: {} {}" , x , String .format ("0x%08x " , x ));
97-
98- byte [] buf = new byte [2 ];
99- buf [0 ] = WITH_ONE_COMMAND ;
100- buf [1 ] = (byte ) x ;
101- i2c .write (buf );
122+ commandBuffer [1 ] = (byte ) x ;
123+ writeCommand (1 );
102124 }
103125
104126 private void command (int x1 , int x2 ) {
105127 log .debug ("Command: {} {} {} {}" , x1 , x2 , String .format ("0x%08x " , x1 ), String .format ("0x%08x " , x2 ));
106128
107- byte [] buf = new byte [3 ];
108- buf [0 ] = WITH_ONE_COMMAND ;
109- buf [1 ] = (byte ) x1 ;
110- buf [2 ] = (byte ) x2 ;
111- i2c .write (buf );
129+ commandBuffer [1 ] = (byte ) x1 ;
130+ commandBuffer [2 ] = (byte ) x2 ;
131+ writeCommand (2 );
112132 }
113133
114134 private void setColumnAddress (int start , int end ) {
115- byte [] buf = new byte [4 ];
116- buf [0 ] = WITH_ONE_COMMAND ;
117- buf [1 ] = COMMAND_SET_COLUMN_ADDRESS ;
118- buf [2 ] = (byte ) (start & 0x7F );
119- buf [3 ] = (byte ) (end & 0x7F );
120- this .i2c .write (buf );
135+ commandBuffer [1 ] = COMMAND_SET_COLUMN_ADDRESS ;
136+ commandBuffer [2 ] = (byte ) (start & 0x7F );
137+ commandBuffer [3 ] = (byte ) (end & 0x7F );
138+ writeCommand (3 );
121139 }
122140
123141 public void sendBuffer () {
@@ -130,7 +148,12 @@ public void sendBuffer() {
130148 buf [0 ] = WITH_DATA_ONLY ;
131149 System .arraycopy (page_buffer , 0 , buf , 1 , page_buffer .length );
132150
133- i2c .write (buf , buf .length );
151+ if (dc != null ) {
152+ dc .on ();
153+ writer .write (buf , 1 , buf .length - 1 );
154+ } else {
155+ writer .write (buf , buf .length );
156+ }
134157 }
135158
136159 public void setPixelOn (int x , int y ) {
@@ -192,6 +215,19 @@ public void setPixels(int x, int y, int width, int height, byte[] data) {
192215
193216 @ Override
194217 public void close () {
195- i2c .close ();
218+ if (dc instanceof Closeable ) {
219+ try {
220+ ((Closeable ) dc ).close ();
221+ } catch (IOException e ) {
222+ e .printStackTrace ();
223+ }
224+ }
225+ if (writer instanceof Closeable ) {
226+ try {
227+ ((Closeable ) writer ).close ();
228+ } catch (IOException e ) {
229+ e .printStackTrace ();
230+ }
231+ }
196232 }
197233}
0 commit comments