Zumo32U4 library
Zumo32U4OLED.h
1 // Copyright (C) Pololu Corporation. See www.pololu.com for details.
2 
3 #pragma once
4 
5 #include <Arduino.h>
6 #include <FastGPIO.h>
7 #include <USBPause.h>
8 #include <PololuSH1106Main.h>
9 #include <util/delay.h>
10 
11 // This asm inside the macro below is an optimized version of
12 // FastGPIO::Pin<mosPin>::setOutputValue(d >> b & 1);
13 // It prevents the compiler from using slow branches to implement the
14 // conditional logic, and also ensures that the writing speed doesn't depend
15 // on the data.
16 #define _P3PP_OLED_SEND_BIT(b) \
17  FastGPIO::Pin<clkPin>::setOutputValueLow(); \
18  asm volatile( \
19  "sbrc %2, %3\n" "sbi %0, %1\n" \
20  "sbrs %2, %3\n" "cbi %0, %1\n" \
21  : : \
22  "I" (FastGPIO::pinStructs[mosPin].portAddr - __SFR_OFFSET), \
23  "I" (FastGPIO::pinStructs[mosPin].bit), \
24  "r" (d), \
25  "I" (b)); \
26  FastGPIO::Pin<clkPin>::setOutputValueHigh();
27 
31 {
32  // Pin assignments
33  static const uint8_t clkPin = 1, mosPin = IO_D5, resPin = 0, dcPin = 17;
34 
35 public:
36  void initPins()
37  {
38  FastGPIO::Pin<clkPin>::setOutputLow();
39  }
40 
41  void reset()
42  {
43  FastGPIO::Pin<resPin>::setOutputLow();
44  _delay_us(10);
45  FastGPIO::Pin<resPin>::setOutputHigh();
46  _delay_us(10);
47  }
48 
49  void sh1106TransferStart()
50  {
51  // From https://github.com/pololu/usb-pause-arduino/blob/master/USBPause.h:
52  // Disables USB interrupts because the Arduino USB interrupts use some of
53  // the OLED pins.
54  savedUDIEN = UDIEN;
55  UDIEN = 0;
56  savedUENUM = UENUM;
57  UENUM = 0;
58  savedUEIENX0 = UEIENX;
59  UEIENX = 0;
60 
61  savedStateMosi = FastGPIO::Pin<mosPin>::getState();
62  savedStateDc = FastGPIO::Pin<dcPin>::getState();
63 
64  FastGPIO::Pin<mosPin>::setOutputLow();
65  }
66 
67  void sh1106TransferEnd()
68  {
69  FastGPIO::Pin<mosPin>::setState(savedStateMosi);
70  FastGPIO::Pin<dcPin>::setState(savedStateDc);
71 
72  // From https://github.com/pololu/usb-pause-arduino/blob/master/USBPause.h
73  UENUM = 0;
74  UEIENX = savedUEIENX0;
75  UENUM = savedUENUM;
76  UDIEN = savedUDIEN;
77  }
78 
79  void sh1106CommandMode()
80  {
81  FastGPIO::Pin<dcPin>::setOutputLow();
82  }
83 
84  void sh1106DataMode()
85  {
86  FastGPIO::Pin<dcPin>::setOutputHigh();
87  }
88 
89  void sh1106Write(uint8_t d)
90  {
91  _P3PP_OLED_SEND_BIT(7);
92  _P3PP_OLED_SEND_BIT(6);
93  _P3PP_OLED_SEND_BIT(5);
94  _P3PP_OLED_SEND_BIT(4);
95  _P3PP_OLED_SEND_BIT(3);
96  _P3PP_OLED_SEND_BIT(2);
97  _P3PP_OLED_SEND_BIT(1);
98  _P3PP_OLED_SEND_BIT(0);
99  }
100 
101 private:
102  uint8_t savedStateMosi, savedStateDc;
103  uint8_t savedUDIEN, savedUENUM, savedUEIENX0;
104 };
105 
119 class Zumo32U4OLED : public PololuSH1106Main<Zumo32U4OLEDCore>
120 {
121 };
Low-level functions for writing data to the SH1106 OLED on the Pololu Zumo 32U4 OLED robot.
Definition: Zumo32U4OLED.h:31
Makes it easy to show text and graphics on the SH1106 OLED of the Pololu Zumo 32U4 OLED robot.
Definition: Zumo32U4OLED.h:120