OLED
Dependents: nucled_L476RG_OLED_Display
Fork of L152RE_OLED_SSD1306 by
SSD1306/ssd1306.cpp@0:761e771590e5, 2014-06-26 (annotated)
- Committer:
- charlesfhda
- Date:
- Thu Jun 26 22:43:10 2014 +0000
- Revision:
- 0:761e771590e5
Moved program files to new account.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charlesfhda | 0:761e771590e5 | 1 | |
charlesfhda | 0:761e771590e5 | 2 | #include "mbed.h" |
charlesfhda | 0:761e771590e5 | 3 | #include "ssd1306.h" |
charlesfhda | 0:761e771590e5 | 4 | |
charlesfhda | 0:761e771590e5 | 5 | #include <stdarg.h> |
charlesfhda | 0:761e771590e5 | 6 | |
charlesfhda | 0:761e771590e5 | 7 | #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) |
charlesfhda | 0:761e771590e5 | 8 | |
charlesfhda | 0:761e771590e5 | 9 | SSD1306::SSD1306(PinName cs, PinName rs, PinName dc, PinName clk, PinName data) |
charlesfhda | 0:761e771590e5 | 10 | : _spi(data, NC, clk), |
charlesfhda | 0:761e771590e5 | 11 | _cs(cs), |
charlesfhda | 0:761e771590e5 | 12 | _reset(rs), |
charlesfhda | 0:761e771590e5 | 13 | _dc(dc), |
charlesfhda | 0:761e771590e5 | 14 | _cursor_x(0), |
charlesfhda | 0:761e771590e5 | 15 | _cursor_y(0) |
charlesfhda | 0:761e771590e5 | 16 | { |
charlesfhda | 0:761e771590e5 | 17 | } |
charlesfhda | 0:761e771590e5 | 18 | |
charlesfhda | 0:761e771590e5 | 19 | void SSD1306::off() |
charlesfhda | 0:761e771590e5 | 20 | { |
charlesfhda | 0:761e771590e5 | 21 | _send_command(0xAE); |
charlesfhda | 0:761e771590e5 | 22 | } |
charlesfhda | 0:761e771590e5 | 23 | |
charlesfhda | 0:761e771590e5 | 24 | void SSD1306::on() |
charlesfhda | 0:761e771590e5 | 25 | { |
charlesfhda | 0:761e771590e5 | 26 | _send_command(0xAF); |
charlesfhda | 0:761e771590e5 | 27 | } |
charlesfhda | 0:761e771590e5 | 28 | |
charlesfhda | 0:761e771590e5 | 29 | void SSD1306::sleep() |
charlesfhda | 0:761e771590e5 | 30 | { |
charlesfhda | 0:761e771590e5 | 31 | _send_command(0xAE); |
charlesfhda | 0:761e771590e5 | 32 | } |
charlesfhda | 0:761e771590e5 | 33 | |
charlesfhda | 0:761e771590e5 | 34 | void SSD1306::wake() |
charlesfhda | 0:761e771590e5 | 35 | { |
charlesfhda | 0:761e771590e5 | 36 | _send_command(0xAF); |
charlesfhda | 0:761e771590e5 | 37 | } |
charlesfhda | 0:761e771590e5 | 38 | |
charlesfhda | 0:761e771590e5 | 39 | void SSD1306::set_inverse(unsigned char value) |
charlesfhda | 0:761e771590e5 | 40 | { |
charlesfhda | 0:761e771590e5 | 41 | _send_command(value ? 0xA7 : 0xA6); |
charlesfhda | 0:761e771590e5 | 42 | } |
charlesfhda | 0:761e771590e5 | 43 | |
charlesfhda | 0:761e771590e5 | 44 | void SSD1306::set_display_offset(unsigned char value) |
charlesfhda | 0:761e771590e5 | 45 | { |
charlesfhda | 0:761e771590e5 | 46 | _send_command(0xD3); |
charlesfhda | 0:761e771590e5 | 47 | _send_command(value & 0x3F); |
charlesfhda | 0:761e771590e5 | 48 | } |
charlesfhda | 0:761e771590e5 | 49 | |
charlesfhda | 0:761e771590e5 | 50 | void SSD1306::set_contrast(unsigned char value) |
charlesfhda | 0:761e771590e5 | 51 | { |
charlesfhda | 0:761e771590e5 | 52 | _send_command(0x81); |
charlesfhda | 0:761e771590e5 | 53 | _send_command(value); |
charlesfhda | 0:761e771590e5 | 54 | } |
charlesfhda | 0:761e771590e5 | 55 | |
charlesfhda | 0:761e771590e5 | 56 | void SSD1306::set_display_start_line(unsigned char value) |
charlesfhda | 0:761e771590e5 | 57 | { |
charlesfhda | 0:761e771590e5 | 58 | _send_command(0x40 | value); |
charlesfhda | 0:761e771590e5 | 59 | } |
charlesfhda | 0:761e771590e5 | 60 | |
charlesfhda | 0:761e771590e5 | 61 | void SSD1306::set_segment_remap(unsigned char value) |
charlesfhda | 0:761e771590e5 | 62 | { |
charlesfhda | 0:761e771590e5 | 63 | _send_command(value ? 0xA1 : 0xA0); |
charlesfhda | 0:761e771590e5 | 64 | } |
charlesfhda | 0:761e771590e5 | 65 | |
charlesfhda | 0:761e771590e5 | 66 | void SSD1306::set_multiplex_ratio(unsigned char value) |
charlesfhda | 0:761e771590e5 | 67 | { |
charlesfhda | 0:761e771590e5 | 68 | _send_command(0xA8); |
charlesfhda | 0:761e771590e5 | 69 | _send_command(value & 0x3F); |
charlesfhda | 0:761e771590e5 | 70 | } |
charlesfhda | 0:761e771590e5 | 71 | |
charlesfhda | 0:761e771590e5 | 72 | void SSD1306::set_com_output_scan_direction(unsigned char value) |
charlesfhda | 0:761e771590e5 | 73 | { |
charlesfhda | 0:761e771590e5 | 74 | _send_command(value ? 0xC8 : 0xC0); |
charlesfhda | 0:761e771590e5 | 75 | } |
charlesfhda | 0:761e771590e5 | 76 | |
charlesfhda | 0:761e771590e5 | 77 | void SSD1306::set_com_pins_hardware_configuration(unsigned char sequential, unsigned char lr_remap) |
charlesfhda | 0:761e771590e5 | 78 | { |
charlesfhda | 0:761e771590e5 | 79 | _send_command(0xDA); |
charlesfhda | 0:761e771590e5 | 80 | _send_command(0x02 | ((sequential & 1) << 4) | ((lr_remap & 1) << 5)); |
charlesfhda | 0:761e771590e5 | 81 | } |
charlesfhda | 0:761e771590e5 | 82 | |
charlesfhda | 0:761e771590e5 | 83 | void SSD1306::start_horizontal_scroll(unsigned char direction, unsigned char start, unsigned char end, unsigned char interval) |
charlesfhda | 0:761e771590e5 | 84 | { |
charlesfhda | 0:761e771590e5 | 85 | _send_command(direction ? 0x27 : 0x26); |
charlesfhda | 0:761e771590e5 | 86 | _send_command(0x00); |
charlesfhda | 0:761e771590e5 | 87 | _send_command(start & 0x07); |
charlesfhda | 0:761e771590e5 | 88 | switch (interval) { |
charlesfhda | 0:761e771590e5 | 89 | case 2: _send_command(0x07); break; // 111b |
charlesfhda | 0:761e771590e5 | 90 | case 3: _send_command(0x04); break; // 100b |
charlesfhda | 0:761e771590e5 | 91 | case 4: _send_command(0x05); break; // 101b |
charlesfhda | 0:761e771590e5 | 92 | case 5: _send_command(0x00); break; // 000b |
charlesfhda | 0:761e771590e5 | 93 | case 25: _send_command(0x06); break; // 110b |
charlesfhda | 0:761e771590e5 | 94 | case 64: _send_command(0x01); break; // 001b |
charlesfhda | 0:761e771590e5 | 95 | case 128: _send_command(0x02); break; // 010b |
charlesfhda | 0:761e771590e5 | 96 | case 256: _send_command(0x03); break; // 011b |
charlesfhda | 0:761e771590e5 | 97 | default: |
charlesfhda | 0:761e771590e5 | 98 | // default to 2 frame interval |
charlesfhda | 0:761e771590e5 | 99 | _send_command(0x07); break; |
charlesfhda | 0:761e771590e5 | 100 | } |
charlesfhda | 0:761e771590e5 | 101 | _send_command(end & 0x07); |
charlesfhda | 0:761e771590e5 | 102 | _send_command(0x00); |
charlesfhda | 0:761e771590e5 | 103 | _send_command(0xFF); |
charlesfhda | 0:761e771590e5 | 104 | |
charlesfhda | 0:761e771590e5 | 105 | // activate scroll |
charlesfhda | 0:761e771590e5 | 106 | _send_command(0x2F); |
charlesfhda | 0:761e771590e5 | 107 | } |
charlesfhda | 0:761e771590e5 | 108 | |
charlesfhda | 0:761e771590e5 | 109 | void SSD1306::start_vertical_and_horizontal_scroll(unsigned char direction, unsigned char start, unsigned char end, unsigned char interval, unsigned char vertical_offset) |
charlesfhda | 0:761e771590e5 | 110 | { |
charlesfhda | 0:761e771590e5 | 111 | _send_command(direction ? 0x2A : 0x29); |
charlesfhda | 0:761e771590e5 | 112 | _send_command(0x00); |
charlesfhda | 0:761e771590e5 | 113 | _send_command(start & 0x07); |
charlesfhda | 0:761e771590e5 | 114 | switch (interval) { |
charlesfhda | 0:761e771590e5 | 115 | case 2: _send_command(0x07); break; // 111b |
charlesfhda | 0:761e771590e5 | 116 | case 3: _send_command(0x04); break; // 100b |
charlesfhda | 0:761e771590e5 | 117 | case 4: _send_command(0x05); break; // 101b |
charlesfhda | 0:761e771590e5 | 118 | case 5: _send_command(0x00); break; // 000b |
charlesfhda | 0:761e771590e5 | 119 | case 25: _send_command(0x06); break; // 110b |
charlesfhda | 0:761e771590e5 | 120 | case 64: _send_command(0x01); break; // 001b |
charlesfhda | 0:761e771590e5 | 121 | case 128: _send_command(0x02); break; // 010b |
charlesfhda | 0:761e771590e5 | 122 | case 256: _send_command(0x03); break; // 011b |
charlesfhda | 0:761e771590e5 | 123 | default: |
charlesfhda | 0:761e771590e5 | 124 | // default to 2 frame interval |
charlesfhda | 0:761e771590e5 | 125 | _send_command(0x07); break; |
charlesfhda | 0:761e771590e5 | 126 | } |
charlesfhda | 0:761e771590e5 | 127 | _send_command(end & 0x07); |
charlesfhda | 0:761e771590e5 | 128 | _send_command(vertical_offset); |
charlesfhda | 0:761e771590e5 | 129 | |
charlesfhda | 0:761e771590e5 | 130 | // activate scroll |
charlesfhda | 0:761e771590e5 | 131 | _send_command(0x2F); |
charlesfhda | 0:761e771590e5 | 132 | } |
charlesfhda | 0:761e771590e5 | 133 | |
charlesfhda | 0:761e771590e5 | 134 | void SSD1306::stop_scroll() |
charlesfhda | 0:761e771590e5 | 135 | { |
charlesfhda | 0:761e771590e5 | 136 | // all scroll configurations are removed from the display when executing this command. |
charlesfhda | 0:761e771590e5 | 137 | _send_command(0x2E); |
charlesfhda | 0:761e771590e5 | 138 | } |
charlesfhda | 0:761e771590e5 | 139 | |
charlesfhda | 0:761e771590e5 | 140 | void SSD1306::pam_set_start_address(unsigned char address) |
charlesfhda | 0:761e771590e5 | 141 | { |
charlesfhda | 0:761e771590e5 | 142 | // "Set Lower Column Start Address for Page Addressing Mode" |
charlesfhda | 0:761e771590e5 | 143 | _send_command(address & 0x0F); |
charlesfhda | 0:761e771590e5 | 144 | |
charlesfhda | 0:761e771590e5 | 145 | // "Set Higher Column Start Address for Page Addressing Mode" |
charlesfhda | 0:761e771590e5 | 146 | _send_command((address << 4) & 0x0F); |
charlesfhda | 0:761e771590e5 | 147 | } |
charlesfhda | 0:761e771590e5 | 148 | |
charlesfhda | 0:761e771590e5 | 149 | void SSD1306::set_memory_addressing_mode(unsigned char mode) |
charlesfhda | 0:761e771590e5 | 150 | { |
charlesfhda | 0:761e771590e5 | 151 | _send_command(0x20); |
charlesfhda | 0:761e771590e5 | 152 | _send_command(mode & 0x3); |
charlesfhda | 0:761e771590e5 | 153 | } |
charlesfhda | 0:761e771590e5 | 154 | |
charlesfhda | 0:761e771590e5 | 155 | void SSD1306::hv_set_column_address(unsigned char start, unsigned char end) |
charlesfhda | 0:761e771590e5 | 156 | { |
charlesfhda | 0:761e771590e5 | 157 | _send_command(0x21); |
charlesfhda | 0:761e771590e5 | 158 | _send_command(start & 0x7F); |
charlesfhda | 0:761e771590e5 | 159 | _send_command(end & 0x7F); |
charlesfhda | 0:761e771590e5 | 160 | } |
charlesfhda | 0:761e771590e5 | 161 | |
charlesfhda | 0:761e771590e5 | 162 | void SSD1306::hv_set_page_address(unsigned char start, unsigned char end) |
charlesfhda | 0:761e771590e5 | 163 | { |
charlesfhda | 0:761e771590e5 | 164 | _send_command(0x22); |
charlesfhda | 0:761e771590e5 | 165 | _send_command(start & 0x07); |
charlesfhda | 0:761e771590e5 | 166 | _send_command(end & 0x07); |
charlesfhda | 0:761e771590e5 | 167 | } |
charlesfhda | 0:761e771590e5 | 168 | |
charlesfhda | 0:761e771590e5 | 169 | void SSD1306::pam_set_page_start(unsigned char address) |
charlesfhda | 0:761e771590e5 | 170 | { |
charlesfhda | 0:761e771590e5 | 171 | _send_command(0xB0 | (address & 0x07)); |
charlesfhda | 0:761e771590e5 | 172 | } |
charlesfhda | 0:761e771590e5 | 173 | |
charlesfhda | 0:761e771590e5 | 174 | void SSD1306::set_display_clock_ratio_and_frequency(unsigned char ratio, unsigned char frequency) |
charlesfhda | 0:761e771590e5 | 175 | { |
charlesfhda | 0:761e771590e5 | 176 | _send_command(0xD5); |
charlesfhda | 0:761e771590e5 | 177 | _send_command((ratio & 0x0F) | ((frequency & 0x0F) << 4)); |
charlesfhda | 0:761e771590e5 | 178 | } |
charlesfhda | 0:761e771590e5 | 179 | |
charlesfhda | 0:761e771590e5 | 180 | void SSD1306::set_precharge_period(unsigned char phase1, unsigned char phase2) |
charlesfhda | 0:761e771590e5 | 181 | { |
charlesfhda | 0:761e771590e5 | 182 | _send_command(0xD9); |
charlesfhda | 0:761e771590e5 | 183 | _send_command((phase1 & 0x0F) | ((phase2 & 0x0F ) << 4)); |
charlesfhda | 0:761e771590e5 | 184 | } |
charlesfhda | 0:761e771590e5 | 185 | |
charlesfhda | 0:761e771590e5 | 186 | void SSD1306::set_vcomh_deselect_level(unsigned char level) |
charlesfhda | 0:761e771590e5 | 187 | { |
charlesfhda | 0:761e771590e5 | 188 | _send_command(0xDB); |
charlesfhda | 0:761e771590e5 | 189 | _send_command((level & 0x03) << 4); |
charlesfhda | 0:761e771590e5 | 190 | } |
charlesfhda | 0:761e771590e5 | 191 | |
charlesfhda | 0:761e771590e5 | 192 | void SSD1306::nop() |
charlesfhda | 0:761e771590e5 | 193 | { |
charlesfhda | 0:761e771590e5 | 194 | _send_command(0xE3); |
charlesfhda | 0:761e771590e5 | 195 | } |
charlesfhda | 0:761e771590e5 | 196 | |
charlesfhda | 0:761e771590e5 | 197 | void SSD1306::set_charge_pump_enable(unsigned char enable) |
charlesfhda | 0:761e771590e5 | 198 | { |
charlesfhda | 0:761e771590e5 | 199 | _send_command(0x8D); |
charlesfhda | 0:761e771590e5 | 200 | _send_command(enable ? 0x14 : 0x10); |
charlesfhda | 0:761e771590e5 | 201 | } |
charlesfhda | 0:761e771590e5 | 202 | |
charlesfhda | 0:761e771590e5 | 203 | void SSD1306::initialise() |
charlesfhda | 0:761e771590e5 | 204 | { |
charlesfhda | 0:761e771590e5 | 205 | // Init |
charlesfhda | 0:761e771590e5 | 206 | _reset = 1; |
charlesfhda | 0:761e771590e5 | 207 | wait(0.01); |
charlesfhda | 0:761e771590e5 | 208 | _reset = 0; |
charlesfhda | 0:761e771590e5 | 209 | wait(0.10); |
charlesfhda | 0:761e771590e5 | 210 | _reset = 1; |
charlesfhda | 0:761e771590e5 | 211 | |
charlesfhda | 0:761e771590e5 | 212 | off(); |
charlesfhda | 0:761e771590e5 | 213 | |
charlesfhda | 0:761e771590e5 | 214 | set_display_clock_ratio_and_frequency(0, 8); |
charlesfhda | 0:761e771590e5 | 215 | set_multiplex_ratio(0x3F); // 1/64 duty |
charlesfhda | 0:761e771590e5 | 216 | set_precharge_period(0xF, 0x01); |
charlesfhda | 0:761e771590e5 | 217 | set_display_offset(0); |
charlesfhda | 0:761e771590e5 | 218 | set_display_start_line(0); |
charlesfhda | 0:761e771590e5 | 219 | set_charge_pump_enable(1); |
charlesfhda | 0:761e771590e5 | 220 | set_memory_addressing_mode(0); // horizontal addressing mode; across then down |
charlesfhda | 0:761e771590e5 | 221 | set_segment_remap(1); |
charlesfhda | 0:761e771590e5 | 222 | set_com_output_scan_direction(1); |
charlesfhda | 0:761e771590e5 | 223 | set_com_pins_hardware_configuration(1, 0); |
charlesfhda | 0:761e771590e5 | 224 | set_contrast(0xFF); |
charlesfhda | 0:761e771590e5 | 225 | set_vcomh_deselect_level(1); |
charlesfhda | 0:761e771590e5 | 226 | |
charlesfhda | 0:761e771590e5 | 227 | wake(); |
charlesfhda | 0:761e771590e5 | 228 | set_inverse(0); |
charlesfhda | 0:761e771590e5 | 229 | |
charlesfhda | 0:761e771590e5 | 230 | hv_set_column_address(0, 127); |
charlesfhda | 0:761e771590e5 | 231 | hv_set_page_address(0, 7); |
charlesfhda | 0:761e771590e5 | 232 | |
charlesfhda | 0:761e771590e5 | 233 | pam_set_start_address(0); |
charlesfhda | 0:761e771590e5 | 234 | pam_set_page_start(0); |
charlesfhda | 0:761e771590e5 | 235 | |
charlesfhda | 0:761e771590e5 | 236 | // set_precharge_period(2, 2); |
charlesfhda | 0:761e771590e5 | 237 | } |
charlesfhda | 0:761e771590e5 | 238 | |
charlesfhda | 0:761e771590e5 | 239 | void SSD1306::update() |
charlesfhda | 0:761e771590e5 | 240 | { |
charlesfhda | 0:761e771590e5 | 241 | hv_set_column_address(0, 127); |
charlesfhda | 0:761e771590e5 | 242 | hv_set_page_address(0, 7); |
charlesfhda | 0:761e771590e5 | 243 | |
charlesfhda | 0:761e771590e5 | 244 | for (int i = 0; i < 1024; i++) |
charlesfhda | 0:761e771590e5 | 245 | _send_data(_screen[i]); |
charlesfhda | 0:761e771590e5 | 246 | } |
charlesfhda | 0:761e771590e5 | 247 | |
charlesfhda | 0:761e771590e5 | 248 | void SSD1306::drawBitmap(int x, int y, |
charlesfhda | 0:761e771590e5 | 249 | const unsigned char *bitmap, int w, int h, int color) { |
charlesfhda | 0:761e771590e5 | 250 | int16_t i, j, byteWidth = (w + 7) / 8; |
charlesfhda | 0:761e771590e5 | 251 | |
charlesfhda | 0:761e771590e5 | 252 | for(j=0; j<h; j++) { |
charlesfhda | 0:761e771590e5 | 253 | for(i=0; i<w; i++ ) { |
charlesfhda | 0:761e771590e5 | 254 | if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) { |
charlesfhda | 0:761e771590e5 | 255 | color? set_pixel(x+i, y+j): clear_pixel(x+i, y+j); |
charlesfhda | 0:761e771590e5 | 256 | } |
charlesfhda | 0:761e771590e5 | 257 | } |
charlesfhda | 0:761e771590e5 | 258 | } |
charlesfhda | 0:761e771590e5 | 259 | } |
charlesfhda | 0:761e771590e5 | 260 | |
charlesfhda | 0:761e771590e5 | 261 | void SSD1306::set_pixel(int x, int y) |
charlesfhda | 0:761e771590e5 | 262 | { |
charlesfhda | 0:761e771590e5 | 263 | if (x >= SSD1306_LCDWIDTH || y >= SSD1306_LCDHEIGHT) return; |
charlesfhda | 0:761e771590e5 | 264 | |
charlesfhda | 0:761e771590e5 | 265 | _screen[x + (y / 8) * 128] |= 1 << (y % 8); |
charlesfhda | 0:761e771590e5 | 266 | } |
charlesfhda | 0:761e771590e5 | 267 | |
charlesfhda | 0:761e771590e5 | 268 | void SSD1306::clear_pixel(int x, int y) |
charlesfhda | 0:761e771590e5 | 269 | { |
charlesfhda | 0:761e771590e5 | 270 | if (x >= SSD1306_LCDWIDTH || y >= SSD1306_LCDHEIGHT) return; |
charlesfhda | 0:761e771590e5 | 271 | |
charlesfhda | 0:761e771590e5 | 272 | _screen[x + (y / 8) * 128] &= ~(1 << (y % 8)); |
charlesfhda | 0:761e771590e5 | 273 | } |
charlesfhda | 0:761e771590e5 | 274 | |
charlesfhda | 0:761e771590e5 | 275 | void SSD1306::line(int x0, int y0, int x1, int y1) { |
charlesfhda | 0:761e771590e5 | 276 | int steep = abs(y1 - y0) > abs(x1 - x0); |
charlesfhda | 0:761e771590e5 | 277 | int t; |
charlesfhda | 0:761e771590e5 | 278 | |
charlesfhda | 0:761e771590e5 | 279 | if (steep) { |
charlesfhda | 0:761e771590e5 | 280 | t = x0; x0 = y0; y0 = t; |
charlesfhda | 0:761e771590e5 | 281 | t = x1; x1 = y1; y1 = t; |
charlesfhda | 0:761e771590e5 | 282 | } |
charlesfhda | 0:761e771590e5 | 283 | |
charlesfhda | 0:761e771590e5 | 284 | if (x0 > x1) { |
charlesfhda | 0:761e771590e5 | 285 | t = x0; x0 = x1; x1 = t; |
charlesfhda | 0:761e771590e5 | 286 | t = y0; y0 = y1; y1 = t; |
charlesfhda | 0:761e771590e5 | 287 | } |
charlesfhda | 0:761e771590e5 | 288 | |
charlesfhda | 0:761e771590e5 | 289 | int dx, dy; |
charlesfhda | 0:761e771590e5 | 290 | |
charlesfhda | 0:761e771590e5 | 291 | dx = x1 - x0; |
charlesfhda | 0:761e771590e5 | 292 | dy = abs(y1 - y0); |
charlesfhda | 0:761e771590e5 | 293 | |
charlesfhda | 0:761e771590e5 | 294 | int err = dx / 2; |
charlesfhda | 0:761e771590e5 | 295 | int ystep; |
charlesfhda | 0:761e771590e5 | 296 | |
charlesfhda | 0:761e771590e5 | 297 | if (y0 < y1) { |
charlesfhda | 0:761e771590e5 | 298 | ystep = 1; |
charlesfhda | 0:761e771590e5 | 299 | } else { |
charlesfhda | 0:761e771590e5 | 300 | ystep = -1;} |
charlesfhda | 0:761e771590e5 | 301 | |
charlesfhda | 0:761e771590e5 | 302 | for (; x0<x1; x0++) { |
charlesfhda | 0:761e771590e5 | 303 | if (steep) { |
charlesfhda | 0:761e771590e5 | 304 | set_pixel(y0, x0); |
charlesfhda | 0:761e771590e5 | 305 | } else { |
charlesfhda | 0:761e771590e5 | 306 | set_pixel(x0, y0); |
charlesfhda | 0:761e771590e5 | 307 | } |
charlesfhda | 0:761e771590e5 | 308 | err -= dy; |
charlesfhda | 0:761e771590e5 | 309 | if (err < 0) { |
charlesfhda | 0:761e771590e5 | 310 | y0 += ystep; |
charlesfhda | 0:761e771590e5 | 311 | err += dx; |
charlesfhda | 0:761e771590e5 | 312 | } |
charlesfhda | 0:761e771590e5 | 313 | } |
charlesfhda | 0:761e771590e5 | 314 | } |
charlesfhda | 0:761e771590e5 | 315 | |
charlesfhda | 0:761e771590e5 | 316 | void SSD1306::set_font(unsigned char *font, unsigned int width) |
charlesfhda | 0:761e771590e5 | 317 | { |
charlesfhda | 0:761e771590e5 | 318 | _console_font_data = font; |
charlesfhda | 0:761e771590e5 | 319 | _console_font_width = width; |
charlesfhda | 0:761e771590e5 | 320 | } |
charlesfhda | 0:761e771590e5 | 321 | |
charlesfhda | 0:761e771590e5 | 322 | void SSD1306::set_double_height_text(unsigned int double_height) |
charlesfhda | 0:761e771590e5 | 323 | { |
charlesfhda | 0:761e771590e5 | 324 | _double_height_text = double_height; |
charlesfhda | 0:761e771590e5 | 325 | } |
charlesfhda | 0:761e771590e5 | 326 | |
charlesfhda | 0:761e771590e5 | 327 | void SSD1306::putc(unsigned char c) |
charlesfhda | 0:761e771590e5 | 328 | { |
charlesfhda | 0:761e771590e5 | 329 | while (_cursor_x >= (128 / _console_font_width)) |
charlesfhda | 0:761e771590e5 | 330 | { |
charlesfhda | 0:761e771590e5 | 331 | _cursor_x -= (128 / _console_font_width); |
charlesfhda | 0:761e771590e5 | 332 | _cursor_y++; |
charlesfhda | 0:761e771590e5 | 333 | } |
charlesfhda | 0:761e771590e5 | 334 | |
charlesfhda | 0:761e771590e5 | 335 | while (_cursor_y > 7) |
charlesfhda | 0:761e771590e5 | 336 | { |
charlesfhda | 0:761e771590e5 | 337 | scroll_up(); |
charlesfhda | 0:761e771590e5 | 338 | } |
charlesfhda | 0:761e771590e5 | 339 | |
charlesfhda | 0:761e771590e5 | 340 | switch (c) |
charlesfhda | 0:761e771590e5 | 341 | { |
charlesfhda | 0:761e771590e5 | 342 | case '\n': |
charlesfhda | 0:761e771590e5 | 343 | _cursor_y++; |
charlesfhda | 0:761e771590e5 | 344 | break; |
charlesfhda | 0:761e771590e5 | 345 | |
charlesfhda | 0:761e771590e5 | 346 | case '\r': |
charlesfhda | 0:761e771590e5 | 347 | _cursor_x = 0; |
charlesfhda | 0:761e771590e5 | 348 | break; |
charlesfhda | 0:761e771590e5 | 349 | |
charlesfhda | 0:761e771590e5 | 350 | case '\t': |
charlesfhda | 0:761e771590e5 | 351 | _cursor_x = (_cursor_x + 4) % 4; |
charlesfhda | 0:761e771590e5 | 352 | break; |
charlesfhda | 0:761e771590e5 | 353 | |
charlesfhda | 0:761e771590e5 | 354 | default: |
charlesfhda | 0:761e771590e5 | 355 | for (int b = 0; b < _console_font_width; b++) |
charlesfhda | 0:761e771590e5 | 356 | { |
charlesfhda | 0:761e771590e5 | 357 | _screen[_cursor_x * _console_font_width + _cursor_y * 128 + b] = _console_font_data[(c - FONT_START) * _console_font_width + b]; |
charlesfhda | 0:761e771590e5 | 358 | } |
charlesfhda | 0:761e771590e5 | 359 | |
charlesfhda | 0:761e771590e5 | 360 | _cursor_x++; |
charlesfhda | 0:761e771590e5 | 361 | } |
charlesfhda | 0:761e771590e5 | 362 | } |
charlesfhda | 0:761e771590e5 | 363 | |
charlesfhda | 0:761e771590e5 | 364 | void SSD1306::scroll_up() |
charlesfhda | 0:761e771590e5 | 365 | { |
charlesfhda | 0:761e771590e5 | 366 | for (int y = 1; y <= 7; y++) |
charlesfhda | 0:761e771590e5 | 367 | { |
charlesfhda | 0:761e771590e5 | 368 | for (int x = 0; x < 128; x++) |
charlesfhda | 0:761e771590e5 | 369 | { |
charlesfhda | 0:761e771590e5 | 370 | _screen[x + 128 * (y - 1)] = _screen[x + 128 * y]; |
charlesfhda | 0:761e771590e5 | 371 | } |
charlesfhda | 0:761e771590e5 | 372 | } |
charlesfhda | 0:761e771590e5 | 373 | |
charlesfhda | 0:761e771590e5 | 374 | for (int x = 0; x < 128; x++) |
charlesfhda | 0:761e771590e5 | 375 | { |
charlesfhda | 0:761e771590e5 | 376 | _screen[x + 128 * 7] = 0; |
charlesfhda | 0:761e771590e5 | 377 | } |
charlesfhda | 0:761e771590e5 | 378 | |
charlesfhda | 0:761e771590e5 | 379 | _cursor_y--; |
charlesfhda | 0:761e771590e5 | 380 | } |
charlesfhda | 0:761e771590e5 | 381 | |
charlesfhda | 0:761e771590e5 | 382 | void SSD1306::printf(const char *format, ...) |
charlesfhda | 0:761e771590e5 | 383 | { |
charlesfhda | 0:761e771590e5 | 384 | static char buffer[128]; |
charlesfhda | 0:761e771590e5 | 385 | |
charlesfhda | 0:761e771590e5 | 386 | va_list args; |
charlesfhda | 0:761e771590e5 | 387 | va_start(args, format); |
charlesfhda | 0:761e771590e5 | 388 | vsprintf(buffer, format, args); |
charlesfhda | 0:761e771590e5 | 389 | va_end(args); |
charlesfhda | 0:761e771590e5 | 390 | |
charlesfhda | 0:761e771590e5 | 391 | char *c = (char *)&buffer; |
charlesfhda | 0:761e771590e5 | 392 | while (*c != 0) |
charlesfhda | 0:761e771590e5 | 393 | { |
charlesfhda | 0:761e771590e5 | 394 | putc(*c++); |
charlesfhda | 0:761e771590e5 | 395 | } |
charlesfhda | 0:761e771590e5 | 396 | } |
charlesfhda | 0:761e771590e5 | 397 | |
charlesfhda | 0:761e771590e5 | 398 | void SSD1306::clear() |
charlesfhda | 0:761e771590e5 | 399 | { |
charlesfhda | 0:761e771590e5 | 400 | for (int i = 0; i < 1024; i++) |
charlesfhda | 0:761e771590e5 | 401 | _screen[i] = 0; |
charlesfhda | 0:761e771590e5 | 402 | |
charlesfhda | 0:761e771590e5 | 403 | _cursor_x = 0; |
charlesfhda | 0:761e771590e5 | 404 | _cursor_y = 0; |
charlesfhda | 0:761e771590e5 | 405 | } |
charlesfhda | 0:761e771590e5 | 406 | |
charlesfhda | 0:761e771590e5 | 407 | void SSD1306::_send_command(unsigned char code) |
charlesfhda | 0:761e771590e5 | 408 | { |
charlesfhda | 0:761e771590e5 | 409 | _cs = 1; |
charlesfhda | 0:761e771590e5 | 410 | _dc = 0; |
charlesfhda | 0:761e771590e5 | 411 | _cs = 0; |
charlesfhda | 0:761e771590e5 | 412 | _spi.write(code); |
charlesfhda | 0:761e771590e5 | 413 | _cs = 1; |
charlesfhda | 0:761e771590e5 | 414 | } |
charlesfhda | 0:761e771590e5 | 415 | |
charlesfhda | 0:761e771590e5 | 416 | void SSD1306::_send_data(unsigned char value) |
charlesfhda | 0:761e771590e5 | 417 | { |
charlesfhda | 0:761e771590e5 | 418 | _cs = 1; |
charlesfhda | 0:761e771590e5 | 419 | _dc = 1; |
charlesfhda | 0:761e771590e5 | 420 | _cs = 0; |
charlesfhda | 0:761e771590e5 | 421 | _spi.write(value); |
charlesfhda | 0:761e771590e5 | 422 | _cs = 1; |
charlesfhda | 0:761e771590e5 | 423 | } |