gobot.io/x/gobot@v1.16.0/drivers/i2c/ssd1306_driver_test.go (about)

     1  package i2c
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"image"
     7  	"reflect"
     8  	"strings"
     9  	"testing"
    10  
    11  	"gobot.io/x/gobot"
    12  	"gobot.io/x/gobot/gobottest"
    13  )
    14  
    15  var _ gobot.Driver = (*SSD1306Driver)(nil)
    16  
    17  func TestDisplayBuffer(t *testing.T) {
    18  	width := 128
    19  	height := 64
    20  	size := 1024 // (width*height) / 8
    21  	display := NewDisplayBuffer(width, height, 8)
    22  
    23  	if display.Size() != size {
    24  		t.Errorf("invalid Size() (%d, expected %d)",
    25  			display.Size(), size)
    26  	}
    27  	if len(display.buffer) != size {
    28  		t.Errorf("allocated buffer size invalid (%d, expected %d)",
    29  			len(display.buffer), size)
    30  	}
    31  
    32  	gobottest.Assert(t, display.buffer[0], byte(0))
    33  	gobottest.Assert(t, display.buffer[1], byte(0))
    34  
    35  	display.SetPixel(0, 0, 1)
    36  	display.SetPixel(1, 0, 1)
    37  	display.SetPixel(2, 0, 1)
    38  	display.SetPixel(0, 1, 1)
    39  	gobottest.Assert(t, display.buffer[0], byte(3))
    40  	gobottest.Assert(t, display.buffer[1], byte(1))
    41  
    42  	display.SetPixel(0, 1, 0)
    43  	gobottest.Assert(t, display.buffer[0], byte(1))
    44  	gobottest.Assert(t, display.buffer[1], byte(1))
    45  }
    46  
    47  // --------- HELPERS
    48  func initTestSSD1306Driver(width, height int, externalVCC bool) (driver *SSD1306Driver) {
    49  	driver, _ = initTestSSD1306DriverWithStubbedAdaptor(width, height, externalVCC)
    50  	return
    51  }
    52  
    53  func initTestSSD1306DriverWithStubbedAdaptor(width, height int, externalVCC bool) (*SSD1306Driver, *i2cTestAdaptor) {
    54  	adaptor := newI2cTestAdaptor()
    55  	return NewSSD1306Driver(adaptor, WithSSD1306DisplayWidth(width), WithSSD1306DisplayHeight(height), WithSSD1306ExternalVCC(externalVCC)), adaptor
    56  }
    57  
    58  // --------- TESTS
    59  
    60  func TestNewSSD1306Driver(t *testing.T) {
    61  	// Does it return a pointer to an instance of SSD1306Driver?
    62  	var bm interface{} = NewSSD1306Driver(newI2cTestAdaptor())
    63  	_, ok := bm.(*SSD1306Driver)
    64  	if !ok {
    65  		t.Errorf("new should have returned a *SSD1306Driver")
    66  	}
    67  
    68  	b := NewSSD1306Driver(newI2cTestAdaptor())
    69  	gobottest.Refute(t, b.Connection(), nil)
    70  }
    71  
    72  // Methods
    73  
    74  func TestSSD1306DriverStartDefaul(t *testing.T) {
    75  	s, _ := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
    76  	gobottest.Assert(t, s.Start(), nil)
    77  }
    78  
    79  func TestSSD1306DriverStart128x32(t *testing.T) {
    80  	s, _ := initTestSSD1306DriverWithStubbedAdaptor(128, 32, false)
    81  	gobottest.Assert(t, s.Start(), nil)
    82  }
    83  
    84  func TestSSD1306DriverStart96x16(t *testing.T) {
    85  	s, _ := initTestSSD1306DriverWithStubbedAdaptor(96, 16, false)
    86  	gobottest.Assert(t, s.Start(), nil)
    87  }
    88  
    89  func TestSSD1306DriverStartExternalVCC(t *testing.T) {
    90  	s, _ := initTestSSD1306DriverWithStubbedAdaptor(128, 32, true)
    91  	gobottest.Assert(t, s.Start(), nil)
    92  }
    93  
    94  func TestSSD1306StartConnectError(t *testing.T) {
    95  	d, adaptor := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
    96  	adaptor.Testi2cConnectErr(true)
    97  	gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
    98  }
    99  
   100  func TestSSD1306StartSizeError(t *testing.T) {
   101  	d, _ := initTestSSD1306DriverWithStubbedAdaptor(128, 54, false)
   102  	//adaptor.Testi2cConnectErr(false)
   103  	gobottest.Assert(t, d.Start(), errors.New("128x54 resolution is unsupported, supported resolutions: 128x64, 128x32, 96x16"))
   104  }
   105  
   106  func TestSSD1306DriverHalt(t *testing.T) {
   107  	s := initTestSSD1306Driver(128, 64, false)
   108  
   109  	gobottest.Assert(t, s.Halt(), nil)
   110  }
   111  
   112  // Test Name & SetName
   113  func TestSSD1306DriverName(t *testing.T) {
   114  	s := initTestSSD1306Driver(96, 16, false)
   115  
   116  	gobottest.Assert(t, strings.HasPrefix(s.Name(), "SSD1306"), true)
   117  	s.SetName("Ole Oled")
   118  	gobottest.Assert(t, s.Name(), "Ole Oled")
   119  }
   120  
   121  func TestSSD1306DriverOptions(t *testing.T) {
   122  	s := NewSSD1306Driver(newI2cTestAdaptor(), WithBus(2), WithSSD1306DisplayHeight(32), WithSSD1306DisplayWidth(128))
   123  	gobottest.Assert(t, s.GetBusOrDefault(1), 2)
   124  	gobottest.Assert(t, s.displayHeight, 32)
   125  	gobottest.Assert(t, s.displayWidth, 128)
   126  }
   127  
   128  func TestSSD1306DriverDisplay(t *testing.T) {
   129  	s, _ := initTestSSD1306DriverWithStubbedAdaptor(96, 16, false)
   130  	s.Start()
   131  	gobottest.Assert(t, s.Display(), nil)
   132  }
   133  
   134  func TestSSD1306DriverShowImage(t *testing.T) {
   135  	s, _ := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
   136  	s.Start()
   137  	img := image.NewRGBA(image.Rect(0, 0, 640, 480))
   138  	gobottest.Assert(t, s.ShowImage(img), errors.New("image must match display width and height: 128x64"))
   139  
   140  	img = image.NewRGBA(image.Rect(0, 0, 128, 64))
   141  	gobottest.Assert(t, s.ShowImage(img), nil)
   142  }
   143  
   144  func TestSSD1306DriverCommand(t *testing.T) {
   145  	s, adaptor := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
   146  	s.Start()
   147  
   148  	adaptor.i2cWriteImpl = func(got []byte) (int, error) {
   149  		expected := []byte{0x80, 0xFF}
   150  		if !reflect.DeepEqual(got, expected) {
   151  			t.Logf("sequence error, got %+v, expected %+v", got, expected)
   152  			return 0, fmt.Errorf("oops")
   153  		}
   154  		return 0, nil
   155  	}
   156  	err := s.command(0xFF)
   157  	gobottest.Assert(t, err, nil)
   158  }
   159  
   160  func TestSSD1306DriverCommands(t *testing.T) {
   161  	s, adaptor := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
   162  	s.Start()
   163  
   164  	adaptor.i2cWriteImpl = func(got []byte) (int, error) {
   165  		expected := []byte{0x80, 0x00, 0x80, 0xFF}
   166  		if !reflect.DeepEqual(got, expected) {
   167  			t.Logf("sequence error, got %+v, expected %+v", got, expected)
   168  			return 0, fmt.Errorf("oops")
   169  		}
   170  		return 0, nil
   171  	}
   172  	err := s.commands([]byte{0x00, 0xFF})
   173  	gobottest.Assert(t, err, nil)
   174  }
   175  
   176  func TestSSD1306DriverOn(t *testing.T) {
   177  	s, adaptor := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
   178  	s.Start()
   179  
   180  	adaptor.i2cWriteImpl = func(got []byte) (int, error) {
   181  		expected := []byte{0x80, ssd1306SetDisplayOn}
   182  		if !reflect.DeepEqual(got, expected) {
   183  			t.Logf("sequence error, got %+v, expected %+v", got, expected)
   184  			return 0, fmt.Errorf("oops")
   185  		}
   186  		return 0, nil
   187  	}
   188  	err := s.On()
   189  	gobottest.Assert(t, err, nil)
   190  }
   191  
   192  func TestSSD1306DriverOff(t *testing.T) {
   193  	s, adaptor := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
   194  	s.Start()
   195  
   196  	adaptor.i2cWriteImpl = func(got []byte) (int, error) {
   197  		expected := []byte{0x80, ssd1306SetDisplayOff}
   198  		if !reflect.DeepEqual(got, expected) {
   199  			t.Logf("sequence error, got %+v, expected %+v", got, expected)
   200  			return 0, fmt.Errorf("oops")
   201  		}
   202  		return 0, nil
   203  	}
   204  	err := s.Off()
   205  	gobottest.Assert(t, err, nil)
   206  }
   207  
   208  func TestSSD1306Reset(t *testing.T) {
   209  	s, adaptor := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
   210  	s.Start()
   211  
   212  	adaptor.i2cWriteImpl = func(got []byte) (int, error) {
   213  		expectedOff := []byte{0x80, ssd1306SetDisplayOff}
   214  		expectedOn := []byte{0x80, ssd1306SetDisplayOn}
   215  		if !reflect.DeepEqual(got, expectedOff) && !reflect.DeepEqual(got, expectedOn) {
   216  			t.Logf("sequence error, got %+v, expected: %+v or %+v", got, expectedOff, expectedOn)
   217  			return 0, fmt.Errorf("oops")
   218  		}
   219  		return 0, nil
   220  	}
   221  	err := s.Reset()
   222  	gobottest.Assert(t, err, nil)
   223  }
   224  
   225  // COMMANDS
   226  
   227  func TestSSD1306DriverCommandsDisplay(t *testing.T) {
   228  	s := initTestSSD1306Driver(128, 64, false)
   229  	s.Start()
   230  
   231  	result := s.Command("Display")(map[string]interface{}{})
   232  	gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
   233  }
   234  
   235  func TestSSD1306DriverCommandsOn(t *testing.T) {
   236  	s := initTestSSD1306Driver(128, 64, false)
   237  	s.Start()
   238  
   239  	result := s.Command("On")(map[string]interface{}{})
   240  	gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
   241  }
   242  
   243  func TestSSD1306DriverCommandsOff(t *testing.T) {
   244  	s := initTestSSD1306Driver(128, 64, false)
   245  	s.Start()
   246  
   247  	result := s.Command("Off")(map[string]interface{}{})
   248  	gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
   249  }
   250  
   251  func TestSSD1306DriverCommandsClear(t *testing.T) {
   252  	s := initTestSSD1306Driver(128, 64, false)
   253  	s.Start()
   254  
   255  	result := s.Command("Clear")(map[string]interface{}{})
   256  	gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
   257  }
   258  
   259  func TestSSD1306DriverCommandsSetContrast(t *testing.T) {
   260  	s, adaptor := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
   261  	s.Start()
   262  
   263  	adaptor.i2cWriteImpl = func(got []byte) (int, error) {
   264  		expected := []byte{0x80, ssd1306SetContrast, 0x80, 0x10}
   265  		if !reflect.DeepEqual(got, expected) {
   266  			t.Logf("sequence error, got %+v, expected %+v", got, expected)
   267  			return 0, fmt.Errorf("oops")
   268  		}
   269  		return 0, nil
   270  	}
   271  
   272  	result := s.Command("SetContrast")(map[string]interface{}{
   273  		"contrast": byte(0x10),
   274  	})
   275  	gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
   276  }
   277  
   278  func TestSSD1306DriverCommandsSet(t *testing.T) {
   279  	s := initTestSSD1306Driver(128, 64, false)
   280  	s.Start()
   281  
   282  	gobottest.Assert(t, s.buffer.buffer[0], byte(0))
   283  	s.Command("Set")(map[string]interface{}{
   284  		"x": int(0),
   285  		"y": int(0),
   286  		"c": int(1),
   287  	})
   288  	gobottest.Assert(t, s.buffer.buffer[0], byte(1))
   289  }