![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
...
Fork of Xadow_Watch_RGBOLED by
main.cpp@0:99c0050a285c, 2014-04-01 (annotated)
- Committer:
- loovee
- Date:
- Tue Apr 01 07:02:59 2014 +0000
- Revision:
- 0:99c0050a285c
..
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
loovee | 0:99c0050a285c | 1 | #include "mbed.h" |
loovee | 0:99c0050a285c | 2 | //#include "USBSerial.h" |
loovee | 0:99c0050a285c | 3 | #include "Defs.h" |
loovee | 0:99c0050a285c | 4 | #include "pinmap.h" |
loovee | 0:99c0050a285c | 5 | #include "font_watch.h" |
loovee | 0:99c0050a285c | 6 | #include "DS1337.h" |
loovee | 0:99c0050a285c | 7 | |
loovee | 0:99c0050a285c | 8 | |
loovee | 0:99c0050a285c | 9 | #define HARD_SPI 1 |
loovee | 0:99c0050a285c | 10 | |
loovee | 0:99c0050a285c | 11 | //USBSerial pc; |
loovee | 0:99c0050a285c | 12 | |
loovee | 0:99c0050a285c | 13 | // functions |
loovee | 0:99c0050a285c | 14 | void spiWrite(unsigned char data); |
loovee | 0:99c0050a285c | 15 | void writeCommand(unsigned char cmd); |
loovee | 0:99c0050a285c | 16 | void writeData(unsigned char data); |
loovee | 0:99c0050a285c | 17 | void writeColor(unsigned int data); |
loovee | 0:99c0050a285c | 18 | void initOLED(void); |
loovee | 0:99c0050a285c | 19 | void clearOLED(void); |
loovee | 0:99c0050a285c | 20 | void fillRect(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, unsigned int outlineColor, unsigned int fillColor); |
loovee | 0:99c0050a285c | 21 | void drawLine(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, unsigned int color); |
loovee | 0:99c0050a285c | 22 | void fillOLED(unsigned int color); |
loovee | 0:99c0050a285c | 23 | |
loovee | 0:99c0050a285c | 24 | //DigitalOut blue(P0_20); |
loovee | 0:99c0050a285c | 25 | DigitalOut white(P0_23); |
loovee | 0:99c0050a285c | 26 | DigitalOut PowerON_OLED(P0_15); |
loovee | 0:99c0050a285c | 27 | DigitalOut DC_OLED(P0_11); |
loovee | 0:99c0050a285c | 28 | |
loovee | 0:99c0050a285c | 29 | DigitalOut CS_OLED(P0_2); |
loovee | 0:99c0050a285c | 30 | |
loovee | 0:99c0050a285c | 31 | |
loovee | 0:99c0050a285c | 32 | #if HARD_SPI |
loovee | 0:99c0050a285c | 33 | |
loovee | 0:99c0050a285c | 34 | SPI spi(P0_9, P0_20, P0_10); // mosi, miso, sclk |
loovee | 0:99c0050a285c | 35 | |
loovee | 0:99c0050a285c | 36 | #else |
loovee | 0:99c0050a285c | 37 | DigitalOut MOSI_OLED(P0_9); |
loovee | 0:99c0050a285c | 38 | DigitalOut SCK_OLED(P0_10); |
loovee | 0:99c0050a285c | 39 | |
loovee | 0:99c0050a285c | 40 | #endif |
loovee | 0:99c0050a285c | 41 | |
loovee | 0:99c0050a285c | 42 | |
loovee | 0:99c0050a285c | 43 | DS1337 RTC(P0_5, P0_4); |
loovee | 0:99c0050a285c | 44 | |
loovee | 0:99c0050a285c | 45 | |
loovee | 0:99c0050a285c | 46 | Serial uart(P0_19, P0_18); |
loovee | 0:99c0050a285c | 47 | |
loovee | 0:99c0050a285c | 48 | |
loovee | 0:99c0050a285c | 49 | typedef struct{ |
loovee | 0:99c0050a285c | 50 | |
loovee | 0:99c0050a285c | 51 | int hour; |
loovee | 0:99c0050a285c | 52 | int minutes; |
loovee | 0:99c0050a285c | 53 | int second; |
loovee | 0:99c0050a285c | 54 | int year; |
loovee | 0:99c0050a285c | 55 | int month; |
loovee | 0:99c0050a285c | 56 | int day; |
loovee | 0:99c0050a285c | 57 | int week; |
loovee | 0:99c0050a285c | 58 | |
loovee | 0:99c0050a285c | 59 | }TIME_S; |
loovee | 0:99c0050a285c | 60 | |
loovee | 0:99c0050a285c | 61 | TIME_S time_now, time_buf; |
loovee | 0:99c0050a285c | 62 | |
loovee | 0:99c0050a285c | 63 | void time_init() |
loovee | 0:99c0050a285c | 64 | { |
loovee | 0:99c0050a285c | 65 | RTC.readTime(); |
loovee | 0:99c0050a285c | 66 | time_now.hour = RTC.getHours(); |
loovee | 0:99c0050a285c | 67 | time_now.minutes = RTC.getMinutes(); |
loovee | 0:99c0050a285c | 68 | time_now.second = RTC.getSeconds(); |
loovee | 0:99c0050a285c | 69 | time_now.year = RTC.getYears(); |
loovee | 0:99c0050a285c | 70 | time_now.month = RTC.getMonths(); |
loovee | 0:99c0050a285c | 71 | time_now.day = RTC.getDays(); |
loovee | 0:99c0050a285c | 72 | time_now.week = RTC.getDayOfWeek(); |
loovee | 0:99c0050a285c | 73 | |
loovee | 0:99c0050a285c | 74 | time_buf.hour = RTC.getHours(); |
loovee | 0:99c0050a285c | 75 | time_buf.minutes = RTC.getMinutes(); |
loovee | 0:99c0050a285c | 76 | time_buf.second = RTC.getSeconds(); |
loovee | 0:99c0050a285c | 77 | time_buf.year = RTC.getYears(); |
loovee | 0:99c0050a285c | 78 | time_buf.month = RTC.getMonths(); |
loovee | 0:99c0050a285c | 79 | time_buf.day = RTC.getDays(); |
loovee | 0:99c0050a285c | 80 | time_buf.week = RTC.getDayOfWeek(); |
loovee | 0:99c0050a285c | 81 | } |
loovee | 0:99c0050a285c | 82 | |
loovee | 0:99c0050a285c | 83 | void time_refresh() |
loovee | 0:99c0050a285c | 84 | { |
loovee | 0:99c0050a285c | 85 | RTC.readTime(); |
loovee | 0:99c0050a285c | 86 | time_now.hour = RTC.getHours(); |
loovee | 0:99c0050a285c | 87 | time_now.minutes = RTC.getMinutes(); |
loovee | 0:99c0050a285c | 88 | time_now.second = RTC.getSeconds(); |
loovee | 0:99c0050a285c | 89 | time_now.year = RTC.getYears(); |
loovee | 0:99c0050a285c | 90 | time_now.month = RTC.getMonths(); |
loovee | 0:99c0050a285c | 91 | time_now.day = RTC.getDays(); |
loovee | 0:99c0050a285c | 92 | time_now.week = RTC.getDayOfWeek(); |
loovee | 0:99c0050a285c | 93 | } |
loovee | 0:99c0050a285c | 94 | |
loovee | 0:99c0050a285c | 95 | void setTime() |
loovee | 0:99c0050a285c | 96 | { |
loovee | 0:99c0050a285c | 97 | RTC.setSeconds(50); |
loovee | 0:99c0050a285c | 98 | RTC.setMinutes(33); |
loovee | 0:99c0050a285c | 99 | RTC.setHours(16); |
loovee | 0:99c0050a285c | 100 | RTC.setDays(18); |
loovee | 0:99c0050a285c | 101 | RTC.setDayOfWeek(2); |
loovee | 0:99c0050a285c | 102 | RTC.setMonths(3); |
loovee | 0:99c0050a285c | 103 | RTC.setYears(2014); |
loovee | 0:99c0050a285c | 104 | |
loovee | 0:99c0050a285c | 105 | RTC.setTime(); |
loovee | 0:99c0050a285c | 106 | } |
loovee | 0:99c0050a285c | 107 | |
loovee | 0:99c0050a285c | 108 | |
loovee | 0:99c0050a285c | 109 | void drawPix(int x, int y, unsigned int color) |
loovee | 0:99c0050a285c | 110 | { |
loovee | 0:99c0050a285c | 111 | //fillRect(x, y, x, y, color, color); |
loovee | 0:99c0050a285c | 112 | |
loovee | 0:99c0050a285c | 113 | int x1 = 95-y; |
loovee | 0:99c0050a285c | 114 | int y1 = x; |
loovee | 0:99c0050a285c | 115 | drawLine(x1, y1, x1, y1, color); |
loovee | 0:99c0050a285c | 116 | } |
loovee | 0:99c0050a285c | 117 | |
loovee | 0:99c0050a285c | 118 | void drawBuff(int x, int y, int buf_len, int buf_width, int color, const unsigned char *buff) |
loovee | 0:99c0050a285c | 119 | { |
loovee | 0:99c0050a285c | 120 | for(int i=0; i<(buf_width/8); i++) |
loovee | 0:99c0050a285c | 121 | { |
loovee | 0:99c0050a285c | 122 | for(int j=0; j<buf_len; j++) |
loovee | 0:99c0050a285c | 123 | { |
loovee | 0:99c0050a285c | 124 | for(int k=0; k<8; k++) |
loovee | 0:99c0050a285c | 125 | { |
loovee | 0:99c0050a285c | 126 | int clr_ = (buff[i*buf_len+j] & (0x01<<k)) ? color : 0; |
loovee | 0:99c0050a285c | 127 | drawPix(j+x, 8*i+k+y, clr_); |
loovee | 0:99c0050a285c | 128 | } |
loovee | 0:99c0050a285c | 129 | } |
loovee | 0:99c0050a285c | 130 | } |
loovee | 0:99c0050a285c | 131 | } |
loovee | 0:99c0050a285c | 132 | |
loovee | 0:99c0050a285c | 133 | void dispFont(int x, int y, int color, const unsigned char *font) |
loovee | 0:99c0050a285c | 134 | { |
loovee | 0:99c0050a285c | 135 | drawBuff(x, y, 11, 32, color, font); |
loovee | 0:99c0050a285c | 136 | } |
loovee | 0:99c0050a285c | 137 | |
loovee | 0:99c0050a285c | 138 | void dispFont_small(int x, int y, int color, const unsigned char *font) |
loovee | 0:99c0050a285c | 139 | { |
loovee | 0:99c0050a285c | 140 | drawBuff(x, y, 8, 16, color, font); |
loovee | 0:99c0050a285c | 141 | } |
loovee | 0:99c0050a285c | 142 | |
loovee | 0:99c0050a285c | 143 | void dispChar(char c, int x, int y, int color) |
loovee | 0:99c0050a285c | 144 | { |
loovee | 0:99c0050a285c | 145 | c = c-32; |
loovee | 0:99c0050a285c | 146 | drawBuff(x, y, 8, 16, color, font_ascii[c]); |
loovee | 0:99c0050a285c | 147 | } |
loovee | 0:99c0050a285c | 148 | |
loovee | 0:99c0050a285c | 149 | unsigned int make_color(unsigned char r, unsigned char g, unsigned char b) |
loovee | 0:99c0050a285c | 150 | { |
loovee | 0:99c0050a285c | 151 | unsigned int color_ = r; |
loovee | 0:99c0050a285c | 152 | |
loovee | 0:99c0050a285c | 153 | color_ = r>>3; // red |
loovee | 0:99c0050a285c | 154 | color_ = color_<<6 | (g>>2); // green |
loovee | 0:99c0050a285c | 155 | color_ = color_<<5 | (b>>3); // blue |
loovee | 0:99c0050a285c | 156 | |
loovee | 0:99c0050a285c | 157 | return color_; |
loovee | 0:99c0050a285c | 158 | } |
loovee | 0:99c0050a285c | 159 | |
loovee | 0:99c0050a285c | 160 | void dispString(char *str) |
loovee | 0:99c0050a285c | 161 | { |
loovee | 0:99c0050a285c | 162 | clearOLED(); |
loovee | 0:99c0050a285c | 163 | |
loovee | 0:99c0050a285c | 164 | int color = make_color(255,255, 255); |
loovee | 0:99c0050a285c | 165 | |
loovee | 0:99c0050a285c | 166 | int x=0, y=0; |
loovee | 0:99c0050a285c | 167 | |
loovee | 0:99c0050a285c | 168 | int pix = 0; |
loovee | 0:99c0050a285c | 169 | |
loovee | 0:99c0050a285c | 170 | while(*str) |
loovee | 0:99c0050a285c | 171 | { |
loovee | 0:99c0050a285c | 172 | dispChar(*str, x, y, make_color(255, 255, 255)); |
loovee | 0:99c0050a285c | 173 | str++; |
loovee | 0:99c0050a285c | 174 | x += 8; |
loovee | 0:99c0050a285c | 175 | pix++; |
loovee | 0:99c0050a285c | 176 | |
loovee | 0:99c0050a285c | 177 | if(pix == 8) |
loovee | 0:99c0050a285c | 178 | { |
loovee | 0:99c0050a285c | 179 | pix = 0; |
loovee | 0:99c0050a285c | 180 | x = 0; |
loovee | 0:99c0050a285c | 181 | y += 16; |
loovee | 0:99c0050a285c | 182 | } |
loovee | 0:99c0050a285c | 183 | } |
loovee | 0:99c0050a285c | 184 | } |
loovee | 0:99c0050a285c | 185 | |
loovee | 0:99c0050a285c | 186 | void dispDot(int x, int y, int color) |
loovee | 0:99c0050a285c | 187 | { |
loovee | 0:99c0050a285c | 188 | drawBuff(x, y, 7, 32, color, font_dot); |
loovee | 0:99c0050a285c | 189 | } |
loovee | 0:99c0050a285c | 190 | |
loovee | 0:99c0050a285c | 191 | void refresh_time() |
loovee | 0:99c0050a285c | 192 | { |
loovee | 0:99c0050a285c | 193 | int y = 60; |
loovee | 0:99c0050a285c | 194 | int color_ = make_color(255, 255, 255); |
loovee | 0:99c0050a285c | 195 | dispFont(0, y, color_, font_num[time_now.hour/10]); |
loovee | 0:99c0050a285c | 196 | dispFont(14, y, color_, font_num[time_now.hour%10]); |
loovee | 0:99c0050a285c | 197 | dispDot(28, y-2, color_); |
loovee | 0:99c0050a285c | 198 | dispFont(38, y, color_, font_num[time_now.minutes/10]); |
loovee | 0:99c0050a285c | 199 | dispFont(52, y, color_, font_num[time_now.minutes%10]); |
loovee | 0:99c0050a285c | 200 | } |
loovee | 0:99c0050a285c | 201 | |
loovee | 0:99c0050a285c | 202 | void refresh_day() |
loovee | 0:99c0050a285c | 203 | { |
loovee | 0:99c0050a285c | 204 | int color_ = make_color(125, 255, 62); |
loovee | 0:99c0050a285c | 205 | |
loovee | 0:99c0050a285c | 206 | dispChar('M', 0, 40, color_); |
loovee | 0:99c0050a285c | 207 | dispChar('a', 8, 40, color_); |
loovee | 0:99c0050a285c | 208 | dispChar('r', 16, 40, color_); |
loovee | 0:99c0050a285c | 209 | |
loovee | 0:99c0050a285c | 210 | // dispChar() |
loovee | 0:99c0050a285c | 211 | |
loovee | 0:99c0050a285c | 212 | dispFont_small(30, 40, color_, font_ascii[time_now.day/10+'0'-32]); |
loovee | 0:99c0050a285c | 213 | dispFont_small(38, 40, color_, font_ascii[time_now.day%10+'0'-32]); |
loovee | 0:99c0050a285c | 214 | } |
loovee | 0:99c0050a285c | 215 | |
loovee | 0:99c0050a285c | 216 | void show_time() |
loovee | 0:99c0050a285c | 217 | { |
loovee | 0:99c0050a285c | 218 | clearOLED(); |
loovee | 0:99c0050a285c | 219 | int color_ = make_color(255, 255, 255); |
loovee | 0:99c0050a285c | 220 | refresh_day(); |
loovee | 0:99c0050a285c | 221 | for(int i=0; i<63; i++) |
loovee | 0:99c0050a285c | 222 | { |
loovee | 0:99c0050a285c | 223 | drawPix(i, 56, color_); |
loovee | 0:99c0050a285c | 224 | drawPix(i, 57, color_); |
loovee | 0:99c0050a285c | 225 | } |
loovee | 0:99c0050a285c | 226 | refresh_time(); |
loovee | 0:99c0050a285c | 227 | } |
loovee | 0:99c0050a285c | 228 | |
loovee | 0:99c0050a285c | 229 | |
loovee | 0:99c0050a285c | 230 | int main() |
loovee | 0:99c0050a285c | 231 | { |
loovee | 0:99c0050a285c | 232 | uart.baud(38400); |
loovee | 0:99c0050a285c | 233 | |
loovee | 0:99c0050a285c | 234 | pin_function(P0_15,1); |
loovee | 0:99c0050a285c | 235 | pin_function(P0_10,1); |
loovee | 0:99c0050a285c | 236 | |
loovee | 0:99c0050a285c | 237 | //pin_mode(P0_15,OpenDrain); |
loovee | 0:99c0050a285c | 238 | PowerON_OLED = 0; // power on OLED module |
loovee | 0:99c0050a285c | 239 | wait_ms(5); |
loovee | 0:99c0050a285c | 240 | PowerON_OLED = 1; // power off OLED module |
loovee | 0:99c0050a285c | 241 | wait_ms(5); |
loovee | 0:99c0050a285c | 242 | PowerON_OLED = 0; // power on OLED module |
loovee | 0:99c0050a285c | 243 | wait_ms(5); |
loovee | 0:99c0050a285c | 244 | |
loovee | 0:99c0050a285c | 245 | #if HARD_SPI |
loovee | 0:99c0050a285c | 246 | spi.format(8,3); |
loovee | 0:99c0050a285c | 247 | spi.frequency(1000000); |
loovee | 0:99c0050a285c | 248 | #else |
loovee | 0:99c0050a285c | 249 | SCK_OLED = 1; |
loovee | 0:99c0050a285c | 250 | CS_OLED = 1; |
loovee | 0:99c0050a285c | 251 | #endif |
loovee | 0:99c0050a285c | 252 | initOLED(); |
loovee | 0:99c0050a285c | 253 | clearOLED(); |
loovee | 0:99c0050a285c | 254 | //unsigned int color = 0; |
loovee | 0:99c0050a285c | 255 | // blue = 0; |
loovee | 0:99c0050a285c | 256 | |
loovee | 0:99c0050a285c | 257 | |
loovee | 0:99c0050a285c | 258 | dispString("xadow smart watch"); |
loovee | 0:99c0050a285c | 259 | |
loovee | 0:99c0050a285c | 260 | wait(1); |
loovee | 0:99c0050a285c | 261 | //setTime(); |
loovee | 0:99c0050a285c | 262 | time_init(); |
loovee | 0:99c0050a285c | 263 | show_time(); |
loovee | 0:99c0050a285c | 264 | |
loovee | 0:99c0050a285c | 265 | char ble_str[40]; |
loovee | 0:99c0050a285c | 266 | int len_str = 0; |
loovee | 0:99c0050a285c | 267 | |
loovee | 0:99c0050a285c | 268 | |
loovee | 0:99c0050a285c | 269 | drawBuff(12, 0, 40, 40, make_color(153, 153, 255), font_ophw); |
loovee | 0:99c0050a285c | 270 | |
loovee | 0:99c0050a285c | 271 | for(;;) |
loovee | 0:99c0050a285c | 272 | { |
loovee | 0:99c0050a285c | 273 | time_refresh(); |
loovee | 0:99c0050a285c | 274 | |
loovee | 0:99c0050a285c | 275 | if(time_now.day != time_buf.day) |
loovee | 0:99c0050a285c | 276 | { |
loovee | 0:99c0050a285c | 277 | refresh_day(); |
loovee | 0:99c0050a285c | 278 | } |
loovee | 0:99c0050a285c | 279 | |
loovee | 0:99c0050a285c | 280 | if(time_now.minutes != time_buf.minutes) |
loovee | 0:99c0050a285c | 281 | { |
loovee | 0:99c0050a285c | 282 | refresh_time(); |
loovee | 0:99c0050a285c | 283 | } |
loovee | 0:99c0050a285c | 284 | |
loovee | 0:99c0050a285c | 285 | wait(0.1); |
loovee | 0:99c0050a285c | 286 | |
loovee | 0:99c0050a285c | 287 | |
loovee | 0:99c0050a285c | 288 | while (uart.readable()) |
loovee | 0:99c0050a285c | 289 | { |
loovee | 0:99c0050a285c | 290 | ble_str[len_str++] = uart.getc(); |
loovee | 0:99c0050a285c | 291 | } |
loovee | 0:99c0050a285c | 292 | |
loovee | 0:99c0050a285c | 293 | if(len_str == 1 && (ble_str[0] == 't' || ble_str[0] == 'T')) |
loovee | 0:99c0050a285c | 294 | { |
loovee | 0:99c0050a285c | 295 | time_refresh(); |
loovee | 0:99c0050a285c | 296 | uart.printf("%d/%d/%d\r\n", time_now.year, time_now.month, time_now.day); |
loovee | 0:99c0050a285c | 297 | uart.printf("%d:%d:%d\r\n", time_now.hour, time_now.minutes, time_now.second); |
loovee | 0:99c0050a285c | 298 | len_str = 0; |
loovee | 0:99c0050a285c | 299 | } |
loovee | 0:99c0050a285c | 300 | else if(len_str>0) |
loovee | 0:99c0050a285c | 301 | { |
loovee | 0:99c0050a285c | 302 | ble_str[len_str] = '\0'; |
loovee | 0:99c0050a285c | 303 | dispString(ble_str); |
loovee | 0:99c0050a285c | 304 | |
loovee | 0:99c0050a285c | 305 | wait(3); |
loovee | 0:99c0050a285c | 306 | show_time(); |
loovee | 0:99c0050a285c | 307 | len_str = 0; |
loovee | 0:99c0050a285c | 308 | |
loovee | 0:99c0050a285c | 309 | } |
loovee | 0:99c0050a285c | 310 | |
loovee | 0:99c0050a285c | 311 | } |
loovee | 0:99c0050a285c | 312 | } |
loovee | 0:99c0050a285c | 313 | |
loovee | 0:99c0050a285c | 314 | void spiWrite(unsigned char data) |
loovee | 0:99c0050a285c | 315 | { |
loovee | 0:99c0050a285c | 316 | |
loovee | 0:99c0050a285c | 317 | #if HARD_SPI |
loovee | 0:99c0050a285c | 318 | |
loovee | 0:99c0050a285c | 319 | spi.write(data); |
loovee | 0:99c0050a285c | 320 | #else |
loovee | 0:99c0050a285c | 321 | for(unsigned char i=0;i<8;i++) |
loovee | 0:99c0050a285c | 322 | { |
loovee | 0:99c0050a285c | 323 | SCK_OLED = 0; |
loovee | 0:99c0050a285c | 324 | if(data&0x80) |
loovee | 0:99c0050a285c | 325 | { |
loovee | 0:99c0050a285c | 326 | MOSI_OLED = 1; |
loovee | 0:99c0050a285c | 327 | } |
loovee | 0:99c0050a285c | 328 | else |
loovee | 0:99c0050a285c | 329 | { |
loovee | 0:99c0050a285c | 330 | MOSI_OLED = 0; |
loovee | 0:99c0050a285c | 331 | } |
loovee | 0:99c0050a285c | 332 | data<<=1; |
loovee | 0:99c0050a285c | 333 | wait_us(1); |
loovee | 0:99c0050a285c | 334 | SCK_OLED = 1; |
loovee | 0:99c0050a285c | 335 | wait_us(1); |
loovee | 0:99c0050a285c | 336 | } |
loovee | 0:99c0050a285c | 337 | #endif |
loovee | 0:99c0050a285c | 338 | } |
loovee | 0:99c0050a285c | 339 | |
loovee | 0:99c0050a285c | 340 | void writeCommand(unsigned char cmd) |
loovee | 0:99c0050a285c | 341 | { |
loovee | 0:99c0050a285c | 342 | DC_OLED = 0; |
loovee | 0:99c0050a285c | 343 | CS_OLED = 0; |
loovee | 0:99c0050a285c | 344 | spiWrite(cmd); |
loovee | 0:99c0050a285c | 345 | CS_OLED = 1; |
loovee | 0:99c0050a285c | 346 | } |
loovee | 0:99c0050a285c | 347 | |
loovee | 0:99c0050a285c | 348 | void writeData(unsigned char data) |
loovee | 0:99c0050a285c | 349 | { |
loovee | 0:99c0050a285c | 350 | DC_OLED = 1; |
loovee | 0:99c0050a285c | 351 | CS_OLED = 0; |
loovee | 0:99c0050a285c | 352 | spiWrite(data); |
loovee | 0:99c0050a285c | 353 | CS_OLED = 1; |
loovee | 0:99c0050a285c | 354 | } |
loovee | 0:99c0050a285c | 355 | |
loovee | 0:99c0050a285c | 356 | void writeColor(unsigned int data) |
loovee | 0:99c0050a285c | 357 | { |
loovee | 0:99c0050a285c | 358 | DC_OLED = 1; |
loovee | 0:99c0050a285c | 359 | CS_OLED = 0; |
loovee | 0:99c0050a285c | 360 | spiWrite(data>>8); |
loovee | 0:99c0050a285c | 361 | spiWrite(data); |
loovee | 0:99c0050a285c | 362 | CS_OLED = 1; |
loovee | 0:99c0050a285c | 363 | } |
loovee | 0:99c0050a285c | 364 | |
loovee | 0:99c0050a285c | 365 | void initOLED(void) |
loovee | 0:99c0050a285c | 366 | { |
loovee | 0:99c0050a285c | 367 | writeCommand(SSD1331_CMD_DISPLAYOFF); // 0xAE |
loovee | 0:99c0050a285c | 368 | writeCommand(SSD1331_CMD_SETREMAP); // 0xA0 |
loovee | 0:99c0050a285c | 369 | #if defined SSD1331_COLORORDER_RGB |
loovee | 0:99c0050a285c | 370 | writeCommand(0x72); // RGB Color |
loovee | 0:99c0050a285c | 371 | #else |
loovee | 0:99c0050a285c | 372 | writeCommand(0x76); // BGR Color |
loovee | 0:99c0050a285c | 373 | #endif |
loovee | 0:99c0050a285c | 374 | writeCommand(SSD1331_CMD_STARTLINE); // 0xA1 |
loovee | 0:99c0050a285c | 375 | writeCommand(0x0); |
loovee | 0:99c0050a285c | 376 | writeCommand(SSD1331_CMD_DISPLAYOFFSET); // 0xA2 |
loovee | 0:99c0050a285c | 377 | writeCommand(0x0); |
loovee | 0:99c0050a285c | 378 | writeCommand(SSD1331_CMD_NORMALDISPLAY); // 0xA4 |
loovee | 0:99c0050a285c | 379 | writeCommand(SSD1331_CMD_SETMULTIPLEX); // 0xA8 |
loovee | 0:99c0050a285c | 380 | writeCommand(0x3F); // 0x3F 1/64 duty |
loovee | 0:99c0050a285c | 381 | writeCommand(SSD1331_CMD_SETMASTER); // 0xAD |
loovee | 0:99c0050a285c | 382 | writeCommand(0x8E); |
loovee | 0:99c0050a285c | 383 | writeCommand(SSD1331_CMD_POWERMODE); // 0xB0 |
loovee | 0:99c0050a285c | 384 | writeCommand(0x0B); |
loovee | 0:99c0050a285c | 385 | writeCommand(SSD1331_CMD_PRECHARGE); // 0xB1 |
loovee | 0:99c0050a285c | 386 | writeCommand(0x31); |
loovee | 0:99c0050a285c | 387 | writeCommand(SSD1331_CMD_CLOCKDIV); // 0xB3 |
loovee | 0:99c0050a285c | 388 | writeCommand(0xF0); // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16) |
loovee | 0:99c0050a285c | 389 | writeCommand(SSD1331_CMD_PRECHARGEA); // 0x8A |
loovee | 0:99c0050a285c | 390 | writeCommand(0x64); |
loovee | 0:99c0050a285c | 391 | writeCommand(SSD1331_CMD_PRECHARGEB); // 0x8B |
loovee | 0:99c0050a285c | 392 | writeCommand(0x78); |
loovee | 0:99c0050a285c | 393 | writeCommand(SSD1331_CMD_PRECHARGEA); // 0x8C |
loovee | 0:99c0050a285c | 394 | writeCommand(0x64); |
loovee | 0:99c0050a285c | 395 | writeCommand(SSD1331_CMD_PRECHARGELEVEL); // 0xBB |
loovee | 0:99c0050a285c | 396 | writeCommand(0x3A); |
loovee | 0:99c0050a285c | 397 | writeCommand(SSD1331_CMD_VCOMH); // 0xBE |
loovee | 0:99c0050a285c | 398 | writeCommand(0x3E); |
loovee | 0:99c0050a285c | 399 | writeCommand(SSD1331_CMD_MASTERCURRENT); // 0x87 |
loovee | 0:99c0050a285c | 400 | writeCommand(0x06); |
loovee | 0:99c0050a285c | 401 | writeCommand(SSD1331_CMD_CONTRASTA); // 0x81 |
loovee | 0:99c0050a285c | 402 | writeCommand(0x91); |
loovee | 0:99c0050a285c | 403 | writeCommand(SSD1331_CMD_CONTRASTB); // 0x82 |
loovee | 0:99c0050a285c | 404 | writeCommand(0x50); |
loovee | 0:99c0050a285c | 405 | writeCommand(SSD1331_CMD_CONTRASTC); // 0x83 |
loovee | 0:99c0050a285c | 406 | writeCommand(0x7D); |
loovee | 0:99c0050a285c | 407 | writeCommand(SSD1331_CMD_DISPLAYON); //--turn on oled panel |
loovee | 0:99c0050a285c | 408 | |
loovee | 0:99c0050a285c | 409 | } |
loovee | 0:99c0050a285c | 410 | |
loovee | 0:99c0050a285c | 411 | void clearOLED(void) |
loovee | 0:99c0050a285c | 412 | { |
loovee | 0:99c0050a285c | 413 | fillRect(0,0,95,63,0,0); |
loovee | 0:99c0050a285c | 414 | } |
loovee | 0:99c0050a285c | 415 | |
loovee | 0:99c0050a285c | 416 | void fillOLED(unsigned int color) |
loovee | 0:99c0050a285c | 417 | { |
loovee | 0:99c0050a285c | 418 | fillRect(0,0,95,63,color,color); |
loovee | 0:99c0050a285c | 419 | } |
loovee | 0:99c0050a285c | 420 | |
loovee | 0:99c0050a285c | 421 | void fillRect(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, unsigned int outlineColor, unsigned int fillColor) |
loovee | 0:99c0050a285c | 422 | { |
loovee | 0:99c0050a285c | 423 | writeCommand(SSD1331_CMD_FILL); |
loovee | 0:99c0050a285c | 424 | writeCommand(0x01); |
loovee | 0:99c0050a285c | 425 | |
loovee | 0:99c0050a285c | 426 | writeCommand(SSD1331_CMD_DRAWRECT); |
loovee | 0:99c0050a285c | 427 | writeCommand(x0); // Starting column |
loovee | 0:99c0050a285c | 428 | writeCommand(y0); // Starting row |
loovee | 0:99c0050a285c | 429 | writeCommand(x1); // End column |
loovee | 0:99c0050a285c | 430 | writeCommand(y1); // End row |
loovee | 0:99c0050a285c | 431 | |
loovee | 0:99c0050a285c | 432 | // Outline color |
loovee | 0:99c0050a285c | 433 | writeCommand((uint8_t)((outlineColor >> 11) << 1)); |
loovee | 0:99c0050a285c | 434 | writeCommand((uint8_t)((outlineColor >> 5) & 0x3F)); |
loovee | 0:99c0050a285c | 435 | writeCommand((uint8_t)((outlineColor << 1) & 0x3F)); |
loovee | 0:99c0050a285c | 436 | // Fill color |
loovee | 0:99c0050a285c | 437 | writeCommand((uint8_t)((fillColor >> 11) << 1)); |
loovee | 0:99c0050a285c | 438 | writeCommand((uint8_t)((fillColor >> 5) & 0x3F)); |
loovee | 0:99c0050a285c | 439 | writeCommand((uint8_t)((fillColor << 1) & 0x3F)); |
loovee | 0:99c0050a285c | 440 | |
loovee | 0:99c0050a285c | 441 | // Delay while the fill completes |
loovee | 0:99c0050a285c | 442 | wait_ms(SSD1331_DELAYS_HWFILL); |
loovee | 0:99c0050a285c | 443 | } |
loovee | 0:99c0050a285c | 444 | |
loovee | 0:99c0050a285c | 445 | void drawLine(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, unsigned int color) |
loovee | 0:99c0050a285c | 446 | { |
loovee | 0:99c0050a285c | 447 | writeCommand(SSD1331_CMD_DRAWLINE); |
loovee | 0:99c0050a285c | 448 | writeCommand(x0); |
loovee | 0:99c0050a285c | 449 | writeCommand(y0); |
loovee | 0:99c0050a285c | 450 | writeCommand(x1); |
loovee | 0:99c0050a285c | 451 | writeCommand(y1); |
loovee | 0:99c0050a285c | 452 | //wait_ms(SSD1331_DELAYS_HWLINE); |
loovee | 0:99c0050a285c | 453 | writeCommand((uint8_t)((color >> 11) << 1)); |
loovee | 0:99c0050a285c | 454 | writeCommand((uint8_t)((color >> 5) & 0x3F)); |
loovee | 0:99c0050a285c | 455 | writeCommand((uint8_t)((color << 1) & 0x3F)); |
loovee | 0:99c0050a285c | 456 | // wait_ms(SSD1331_DELAYS_HWLINE); |
loovee | 0:99c0050a285c | 457 | } |