summaryrefslogtreecommitdiffstats
path: root/app/oled.c
blob: 63a47fc5a62716d8ab5caa717efacfe88ea19f16 (plain)
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
#include "project.h"


int
ssd1306_cmds (uint32_t i2c, uint8_t *buf, size_t len, int delay)
{

  int ret = -1;

  while (i2c_lock());

  do {

    if (i2cp_start_transaction (i2c, SSD1306_I2C_ADDRESS, I2C_WRITE))
      break;

    if (i2cp_send (i2c, SSD1306_COMMAND))
      break;

    while (len--)
      if (i2cp_send (i2c, * (buf++)))
        break;

    i2cp_stop (i2c);

    if (delay)
      delay_us (delay);

    ret = 0;
  } while (0);

  i2c_unlock();


  return ret;
}

int
ssd1306_cmd (uint32_t i2c, uint8_t cmd, int delay)
{
  return ssd1306_cmds (i2c, &cmd, 1, delay);
}



void
oled_generate_stream (uint8_t *vram)
{
  memset (vram, 0, DMA_BUF_SZ);
  vram[0] = SSD1306_DATA_CONTINUE;
}




void
oled_cls (uint8_t *vram)
{
  memset (vram + 1, 0, SSD1306_VRAM_SIZE);
}


void
oled_on (uint32_t i2c)
{
  ssd1306_cmd (i2c, SSD1306_DISPLAY_ON, 0);

}


void
oled_off (uint32_t i2c)
{
  ssd1306_cmd (i2c, SSD1306_DISPLAY_OFF, 0);
}



#if 0
void
oled_squirt (void)
{
  unsigned i;
  uint8_t cmds[] = {
    SSD1306_SET_PAGE_ADDR, 0, 0xff,
    SSD1306_SET_COLUMN_ADDR, 0, SSD1306_WIDTH - 1
  };

  ssd1306_cmds (cmds, sizeof (cmds), 0);


  while (i2c_lock());

  i2cp_start_transaction (SSD1306_I2C_ADDRESS, I2C_WRITE);

  for (i = 0; i < DMA_BUF_SZ; ++i)
    i2cp_send (update_buf[i]);

  i2cp_stop();

  i2c_unlock();
}
#endif

void
oled_reset (uint32_t i2c)
{
  uint8_t init[] = {
    SSD1306_DISPLAY_OFF,
    SSD1306_SET_DISPLAY_CLOCK_DIV_RATIO, 0x80,
    SSD1306_SET_MULTIPLEX_RATIO, SSD1306_HEIGHT - 1,
    SSD1306_SET_DISPLAY_OFFSET, 0x0,
    SSD1306_SET_START_LINE | 0x0,
    SSD1306_CHARGE_PUMP, 0x14,
    SSD1306_MEMORY_ADDR_MODE, 0x00,
    SSD1306_SET_SEGMENT_REMAP | 0x1,
    SSD1306_COM_SCAN_DIR_DEC,
    //SSD1306_SET_COM_PINS, 0x12,
    SSD1306_SET_COM_PINS, 0x02,
    //SSD1306_SET_CONTRAST_CONTROL, 0xCF,
    SSD1306_SET_CONTRAST_CONTROL, 0x8F,
    SSD1306_SET_PRECHARGE_PERIOD, 0xF1,
    SSD1306_SET_VCOM_DESELECT, 0x40,
    SSD1306_DISPLAY_ALL_ON_RESUME,
    SSD1306_NORMAL_DISPLAY,
    SSD1306_DISPLAY_ON,
  };

  ssd1306_cmds (i2c, init, sizeof (init), 0);
}


void
oled_const_strip (uint8_t *vram, int x, int xe, uint8_t and, uint8_t or)
{
  uint8_t r;

  for (x++; x <= xe; ++x) {
    r = vram[x] & and;
    vram[x] = r | or;
  }
}


void
oled_blit_strip (uint8_t *vram, int x, int xe, uint8_t mask, int shift,
                 uint8_t *src)
{
  uint8_t r, s;

  while (x < 0) {
    x++;
    src++;
  };

  if (xe < 0) xe = 0;

  if (x > SSD1306_VRAM_SIZE) xe = SSD1306_VRAM_SIZE;

  if (xe > SSD1306_VRAM_SIZE) xe = SSD1306_VRAM_SIZE;


  if (shift > 0) {
    for (x++; x <= xe; ++x) {
      s = * (src++);
      s <<= shift;
      s &= mask;
      r = vram[x] & ~mask;
      vram[x] = r | s;

    }
  } else if (shift < 0) {
    shift = -shift;

    for (x++; x <= xe; ++x) {
      s = * (src++);
      s >>= shift;
      s &= mask;
      r = vram[x] & ~mask;
      vram[x] = r | s;
    }
  } else {
    for (x++; x <= xe; ++x) {
      s = * (src++);
      s &= mask;
      r = vram[x] & ~mask;
      vram[x] = r | s;
    }
  }
}