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