Ecran OLED du projet SmartCampus
Dependencies: SSD1308_128x64_I2C
main.cpp@5:dff410a009c5, 2012-07-21 (annotated)
- Committer:
- wim
- Date:
- Sat Jul 21 12:50:23 2012 +0000
- Revision:
- 5:dff410a009c5
- Parent:
- 4:83b4e905be5b
- Child:
- 6:44256b0b5d18
Added comments, Added progressbar
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 1:00053cb70ac5 | 1 | /* mbed Seeed 128x64 OLED Test |
wim | 1:00053cb70ac5 | 2 | * http://www.seeedstudio.com/depot/grove-oled-display-12864-p-781.html?cPath=163_167 |
wim | 1:00053cb70ac5 | 3 | * |
wim | 1:00053cb70ac5 | 4 | * Copyright (c) 2012 Wim Huiskamp |
wim | 1:00053cb70ac5 | 5 | * Released under the MIT License: http://mbed.org/license/mit |
wim | 1:00053cb70ac5 | 6 | * |
wim | 1:00053cb70ac5 | 7 | * version 0.2 Initial Release |
wim | 1:00053cb70ac5 | 8 | */ |
wim | 1:00053cb70ac5 | 9 | #include "mbed.h" |
wim | 1:00053cb70ac5 | 10 | #include "mbed_logo.h" |
wim | 1:00053cb70ac5 | 11 | #include "SSD1308.h" |
wim | 1:00053cb70ac5 | 12 | |
wim | 1:00053cb70ac5 | 13 | |
wim | 1:00053cb70ac5 | 14 | //Pin Defines for I2C Bus |
wim | 1:00053cb70ac5 | 15 | //#define D_SDA p9 |
wim | 1:00053cb70ac5 | 16 | //#define D_SCL p10 |
wim | 1:00053cb70ac5 | 17 | #define D_SDA p28 |
wim | 1:00053cb70ac5 | 18 | #define D_SCL p27 |
wim | 1:00053cb70ac5 | 19 | I2C i2c(D_SDA, D_SCL); |
wim | 1:00053cb70ac5 | 20 | |
wim | 1:00053cb70ac5 | 21 | //Host PC Baudrate (Virtual Com Port on USB) |
wim | 1:00053cb70ac5 | 22 | #define D_BAUDRATE 9600 |
wim | 1:00053cb70ac5 | 23 | //#define D_BAUDRATE 57600 |
wim | 1:00053cb70ac5 | 24 | |
wim | 1:00053cb70ac5 | 25 | // mbed Interface Hardware definitions |
wim | 1:00053cb70ac5 | 26 | DigitalOut myled1(LED1); |
wim | 1:00053cb70ac5 | 27 | DigitalOut myled2(LED2); |
wim | 1:00053cb70ac5 | 28 | DigitalOut myled3(LED3); |
wim | 1:00053cb70ac5 | 29 | DigitalOut heartbeatLED(LED4); |
wim | 1:00053cb70ac5 | 30 | |
wim | 1:00053cb70ac5 | 31 | // Host PC Communication channels |
wim | 1:00053cb70ac5 | 32 | Serial pc(USBTX, USBRX); // tx, rx |
wim | 1:00053cb70ac5 | 33 | |
wim | 1:00053cb70ac5 | 34 | // Instantiate OLED |
wim | 1:00053cb70ac5 | 35 | SSD1308 oled = SSD1308(i2c, SSD1308_SA0); |
wim | 1:00053cb70ac5 | 36 | |
wim | 1:00053cb70ac5 | 37 | void show_menu() { |
wim | 3:1337e3d65ed0 | 38 | pc.printf("0: Exit\r"); |
wim | 3:1337e3d65ed0 | 39 | pc.printf("1: Show Menu\r"); |
wim | 3:1337e3d65ed0 | 40 | pc.printf("2: Send Message\r"); |
wim | 3:1337e3d65ed0 | 41 | pc.printf("3: Fill Display\r"); |
wim | 3:1337e3d65ed0 | 42 | pc.printf("4: Display cleared\r"); |
wim | 3:1337e3d65ed0 | 43 | pc.printf("5: Display off\r"); |
wim | 3:1337e3d65ed0 | 44 | pc.printf("6: Display on\r"); |
wim | 3:1337e3d65ed0 | 45 | pc.printf("7: Display Invert\r"); |
wim | 3:1337e3d65ed0 | 46 | pc.printf("8: Display Normal\r"); |
wim | 3:1337e3d65ed0 | 47 | pc.printf("9: Display Bitmap\r"); |
wim | 3:1337e3d65ed0 | 48 | pc.printf("A: Brightness Ramp\r"); |
wim | 3:1337e3d65ed0 | 49 | pc.printf("B: Send Inverted Message\r"); |
wim | 3:1337e3d65ed0 | 50 | pc.printf("C: Flip and Mirror\r"); |
wim | 3:1337e3d65ed0 | 51 | pc.printf("D: Blink\r"); |
wim | 3:1337e3d65ed0 | 52 | pc.printf("E: Big Chars\r"); |
wim | 5:dff410a009c5 | 53 | pc.printf("F: Progress Scale\r"); |
wim | 1:00053cb70ac5 | 54 | // pc.printf("\n\r"); |
wim | 1:00053cb70ac5 | 55 | |
wim | 1:00053cb70ac5 | 56 | } |
wim | 1:00053cb70ac5 | 57 | |
wim | 1:00053cb70ac5 | 58 | |
wim | 1:00053cb70ac5 | 59 | void oled_Test() { |
wim | 1:00053cb70ac5 | 60 | pc.printf("OLED test start\r"); |
wim | 1:00053cb70ac5 | 61 | |
wim | 1:00053cb70ac5 | 62 | oled.writeString(0, 0, 13, "Hello World !"); |
wim | 1:00053cb70ac5 | 63 | pc.printf("Printed something\r\n"); |
wim | 1:00053cb70ac5 | 64 | wait(3); |
wim | 1:00053cb70ac5 | 65 | |
wim | 1:00053cb70ac5 | 66 | oled.writeString(1, 0, 8, "baz quux"); |
wim | 1:00053cb70ac5 | 67 | pc.printf("Printed something\r\n"); |
wim | 1:00053cb70ac5 | 68 | wait(3); |
wim | 1:00053cb70ac5 | 69 | |
wim | 1:00053cb70ac5 | 70 | // oled.writeString(4, 0, 272, "a long, rather lengthy, extended string passage thing, eh, that just goes on, and on, and on, and on, and on, and on, and on, yes, further, continuing, extending, expanding beyond all reason or sanity!!!!! and yet, there's more! so much more! for ever and ever, oh yeah"); |
wim | 1:00053cb70ac5 | 71 | // pc.printf("Printed something\r\n"); |
wim | 1:00053cb70ac5 | 72 | // wait(3); |
wim | 1:00053cb70ac5 | 73 | |
wim | 1:00053cb70ac5 | 74 | oled.fillDisplay(0xAA); |
wim | 1:00053cb70ac5 | 75 | pc.printf("Display filled\r\n"); |
wim | 1:00053cb70ac5 | 76 | wait(3); |
wim | 1:00053cb70ac5 | 77 | |
wim | 1:00053cb70ac5 | 78 | oled.setDisplayOff(); |
wim | 1:00053cb70ac5 | 79 | pc.printf("Display off\r\n"); |
wim | 1:00053cb70ac5 | 80 | wait(0.5); |
wim | 1:00053cb70ac5 | 81 | |
wim | 1:00053cb70ac5 | 82 | oled.setDisplayOn(); |
wim | 1:00053cb70ac5 | 83 | pc.printf("Display on\r\n"); |
wim | 1:00053cb70ac5 | 84 | wait(0.5); |
wim | 1:00053cb70ac5 | 85 | |
wim | 1:00053cb70ac5 | 86 | oled.clearDisplay(); |
wim | 1:00053cb70ac5 | 87 | pc.printf("Display cleared\r\n"); |
wim | 1:00053cb70ac5 | 88 | wait(0.5); |
wim | 1:00053cb70ac5 | 89 | |
wim | 1:00053cb70ac5 | 90 | oled.writeString(0, 0, 11, "Bye World !"); |
wim | 1:00053cb70ac5 | 91 | pc.printf("Printed something\r\n"); |
wim | 1:00053cb70ac5 | 92 | wait(3); |
wim | 1:00053cb70ac5 | 93 | |
wim | 1:00053cb70ac5 | 94 | pc.printf("OLED test done\r\n"); |
wim | 1:00053cb70ac5 | 95 | } |
wim | 1:00053cb70ac5 | 96 | |
wim | 1:00053cb70ac5 | 97 | // Variables for Heartbeat and Status monitoring |
wim | 1:00053cb70ac5 | 98 | Ticker heartbeat; |
wim | 1:00053cb70ac5 | 99 | bool heartbeatflag=false; |
wim | 1:00053cb70ac5 | 100 | |
wim | 1:00053cb70ac5 | 101 | // Local functions |
wim | 1:00053cb70ac5 | 102 | void clear_screen() { |
wim | 1:00053cb70ac5 | 103 | //ANSI Terminal Commands |
wim | 1:00053cb70ac5 | 104 | pc.printf("\x1B[2J"); |
wim | 1:00053cb70ac5 | 105 | pc.printf("\x1B[H"); |
wim | 1:00053cb70ac5 | 106 | } |
wim | 1:00053cb70ac5 | 107 | |
wim | 1:00053cb70ac5 | 108 | |
wim | 1:00053cb70ac5 | 109 | void init_interfaces() { |
wim | 1:00053cb70ac5 | 110 | // Init Host PC communication, default is 9600 |
wim | 1:00053cb70ac5 | 111 | pc.baud(D_BAUDRATE); |
wim | 1:00053cb70ac5 | 112 | |
wim | 1:00053cb70ac5 | 113 | // Init I/F hardware |
wim | 1:00053cb70ac5 | 114 | // i2c.frequency(100000); |
wim | 1:00053cb70ac5 | 115 | i2c.frequency(400000); // according to the spec the max bitrate for the SSD1308 is 400 kbit/s |
wim | 1:00053cb70ac5 | 116 | } |
wim | 1:00053cb70ac5 | 117 | |
wim | 1:00053cb70ac5 | 118 | |
wim | 1:00053cb70ac5 | 119 | // Heartbeat monitor |
wim | 1:00053cb70ac5 | 120 | void pulse() { |
wim | 1:00053cb70ac5 | 121 | heartbeatLED = !heartbeatLED; |
wim | 1:00053cb70ac5 | 122 | } |
wim | 1:00053cb70ac5 | 123 | |
wim | 1:00053cb70ac5 | 124 | void heartbeat_start() { |
wim | 1:00053cb70ac5 | 125 | heartbeat.attach(&pulse, 0.5); |
wim | 1:00053cb70ac5 | 126 | } |
wim | 1:00053cb70ac5 | 127 | |
wim | 1:00053cb70ac5 | 128 | void heartbeat_stop() { |
wim | 1:00053cb70ac5 | 129 | heartbeat.detach(); |
wim | 1:00053cb70ac5 | 130 | } |
wim | 1:00053cb70ac5 | 131 | |
wim | 1:00053cb70ac5 | 132 | |
wim | 1:00053cb70ac5 | 133 | int main() { |
wim | 1:00053cb70ac5 | 134 | bool running=true; |
wim | 2:d86478c0f5da | 135 | bool left = true; |
wim | 2:d86478c0f5da | 136 | bool down = true; |
wim | 2:d86478c0f5da | 137 | |
wim | 1:00053cb70ac5 | 138 | char command; |
wim | 1:00053cb70ac5 | 139 | |
wim | 1:00053cb70ac5 | 140 | init_interfaces(); |
wim | 1:00053cb70ac5 | 141 | |
wim | 1:00053cb70ac5 | 142 | heartbeat_start(); |
wim | 1:00053cb70ac5 | 143 | |
wim | 1:00053cb70ac5 | 144 | clear_screen(); |
wim | 1:00053cb70ac5 | 145 | |
wim | 1:00053cb70ac5 | 146 | pc.printf("Hello World!\n\r"); |
wim | 1:00053cb70ac5 | 147 | |
wim | 1:00053cb70ac5 | 148 | #if(0) |
wim | 1:00053cb70ac5 | 149 | // Quick test |
wim | 1:00053cb70ac5 | 150 | oled_Test(); |
wim | 1:00053cb70ac5 | 151 | |
wim | 1:00053cb70ac5 | 152 | while(1) { |
wim | 1:00053cb70ac5 | 153 | myled1 = 1; |
wim | 1:00053cb70ac5 | 154 | wait(0.2); |
wim | 1:00053cb70ac5 | 155 | |
wim | 1:00053cb70ac5 | 156 | myled1 = 0; |
wim | 1:00053cb70ac5 | 157 | wait(0.2); |
wim | 1:00053cb70ac5 | 158 | pc.printf("*"); |
wim | 1:00053cb70ac5 | 159 | } |
wim | 1:00053cb70ac5 | 160 | #else |
wim | 1:00053cb70ac5 | 161 | // Interactive Test |
wim | 1:00053cb70ac5 | 162 | show_menu(); |
wim | 1:00053cb70ac5 | 163 | |
wim | 1:00053cb70ac5 | 164 | while(running) { |
wim | 1:00053cb70ac5 | 165 | |
wim | 1:00053cb70ac5 | 166 | if(pc.readable()) { |
wim | 1:00053cb70ac5 | 167 | command = pc.getc(); |
wim | 3:1337e3d65ed0 | 168 | //pc.printf("command= %c \n\r", command); |
wim | 3:1337e3d65ed0 | 169 | pc.printf("\r"); |
wim | 3:1337e3d65ed0 | 170 | |
wim | 1:00053cb70ac5 | 171 | switch (command) { |
wim | 1:00053cb70ac5 | 172 | case '0' : |
wim | 1:00053cb70ac5 | 173 | pc.printf("Done\n\r"); |
wim | 1:00053cb70ac5 | 174 | running = false; |
wim | 1:00053cb70ac5 | 175 | break; |
wim | 1:00053cb70ac5 | 176 | |
wim | 1:00053cb70ac5 | 177 | case '1' : |
wim | 1:00053cb70ac5 | 178 | show_menu(); |
wim | 1:00053cb70ac5 | 179 | break; |
wim | 1:00053cb70ac5 | 180 | |
wim | 1:00053cb70ac5 | 181 | case '2' : |
wim | 1:00053cb70ac5 | 182 | pc.printf("Hello World!\n\r"); |
wim | 1:00053cb70ac5 | 183 | oled.writeString(0, 0, 13, "Hello World !"); |
wim | 1:00053cb70ac5 | 184 | break; |
wim | 1:00053cb70ac5 | 185 | |
wim | 1:00053cb70ac5 | 186 | case '3' : |
wim | 3:1337e3d65ed0 | 187 | pc.printf("Fill part of Display 0xA5\n\r"); |
wim | 3:1337e3d65ed0 | 188 | // oled.fillDisplay(0xA5); |
wim | 3:1337e3d65ed0 | 189 | |
wim | 3:1337e3d65ed0 | 190 | oled.fillDisplay(0xA5, 2, 5, 0, 63); |
wim | 1:00053cb70ac5 | 191 | break; |
wim | 1:00053cb70ac5 | 192 | |
wim | 1:00053cb70ac5 | 193 | case '4' : |
wim | 1:00053cb70ac5 | 194 | pc.printf("Display cleared\r\n"); |
wim | 1:00053cb70ac5 | 195 | oled.clearDisplay(); |
wim | 1:00053cb70ac5 | 196 | break; |
wim | 1:00053cb70ac5 | 197 | |
wim | 1:00053cb70ac5 | 198 | case '5' : |
wim | 1:00053cb70ac5 | 199 | pc.printf("Display off\r\n"); |
wim | 1:00053cb70ac5 | 200 | oled.setDisplayOff(); |
wim | 1:00053cb70ac5 | 201 | break; |
wim | 1:00053cb70ac5 | 202 | case '6' : |
wim | 1:00053cb70ac5 | 203 | pc.printf("Display on\r\n"); |
wim | 1:00053cb70ac5 | 204 | oled.setDisplayOn(); |
wim | 1:00053cb70ac5 | 205 | break; |
wim | 1:00053cb70ac5 | 206 | |
wim | 1:00053cb70ac5 | 207 | case '7' : |
wim | 1:00053cb70ac5 | 208 | pc.printf("Display Invert\r\n"); |
wim | 1:00053cb70ac5 | 209 | oled.setDisplayInverse(); |
wim | 1:00053cb70ac5 | 210 | break; |
wim | 1:00053cb70ac5 | 211 | |
wim | 1:00053cb70ac5 | 212 | case '8' : |
wim | 1:00053cb70ac5 | 213 | pc.printf("Display Normal\r\n"); |
wim | 1:00053cb70ac5 | 214 | oled.setDisplayNormal(); |
wim | 1:00053cb70ac5 | 215 | break; |
wim | 1:00053cb70ac5 | 216 | |
wim | 1:00053cb70ac5 | 217 | case '9' : |
wim | 1:00053cb70ac5 | 218 | pc.printf("Display bitmap\r\n"); |
wim | 3:1337e3d65ed0 | 219 | oled.writeBitmap((uint8_t*) mbed_logo); |
wim | 3:1337e3d65ed0 | 220 | |
wim | 1:00053cb70ac5 | 221 | break; |
wim | 1:00053cb70ac5 | 222 | |
wim | 1:00053cb70ac5 | 223 | case 'A' : |
wim | 3:1337e3d65ed0 | 224 | |
wim | 2:d86478c0f5da | 225 | pc.printf("Brightness Ramp Down\r\n"); |
wim | 3:1337e3d65ed0 | 226 | for (int contrast=0x7F; contrast >= 0x10; contrast--) { |
wim | 2:d86478c0f5da | 227 | oled.setContrastControl(contrast); |
wim | 2:d86478c0f5da | 228 | wait(0.05); |
wim | 2:d86478c0f5da | 229 | } |
wim | 2:d86478c0f5da | 230 | |
wim | 2:d86478c0f5da | 231 | wait(1); |
wim | 3:1337e3d65ed0 | 232 | |
wim | 2:d86478c0f5da | 233 | pc.printf("Brightness Ramp Up\r\n"); |
wim | 3:1337e3d65ed0 | 234 | for (int contrast=0x10; contrast <= 0x7F; contrast++) { |
wim | 2:d86478c0f5da | 235 | oled.setContrastControl(contrast); |
wim | 2:d86478c0f5da | 236 | wait(0.05); |
wim | 2:d86478c0f5da | 237 | } |
wim | 2:d86478c0f5da | 238 | |
wim | 3:1337e3d65ed0 | 239 | pc.printf("Brightness Ramp Done\r\n"); |
wim | 3:1337e3d65ed0 | 240 | |
wim | 1:00053cb70ac5 | 241 | break; |
wim | 1:00053cb70ac5 | 242 | |
wim | 1:00053cb70ac5 | 243 | case 'B' : |
wim | 2:d86478c0f5da | 244 | pc.printf("Send Inverted Message\n\r"); |
wim | 3:1337e3d65ed0 | 245 | oled.setInverted(true); |
wim | 3:1337e3d65ed0 | 246 | oled.writeString(0, 0, 13, "Hello World !"); |
wim | 3:1337e3d65ed0 | 247 | oled.setInverted(false); |
wim | 3:1337e3d65ed0 | 248 | |
wim | 3:1337e3d65ed0 | 249 | oled.printf(" Result is %d", 12345); |
wim | 3:1337e3d65ed0 | 250 | |
wim | 2:d86478c0f5da | 251 | break; |
wim | 2:d86478c0f5da | 252 | |
wim | 2:d86478c0f5da | 253 | case 'C' : |
wim | 2:d86478c0f5da | 254 | pc.printf("Flip and Mirror (Rewrite display to show horizontal effect)\n\r"); |
wim | 2:d86478c0f5da | 255 | left = !left; |
wim | 2:d86478c0f5da | 256 | down = !down; |
wim | 2:d86478c0f5da | 257 | oled.setDisplayFlip(left, down); |
wim | 3:1337e3d65ed0 | 258 | break; |
wim | 3:1337e3d65ed0 | 259 | |
wim | 3:1337e3d65ed0 | 260 | case 'D' : |
wim | 3:1337e3d65ed0 | 261 | pc.printf("D: Blink and Fade (not supported) \n\r"); |
wim | 3:1337e3d65ed0 | 262 | |
wim | 3:1337e3d65ed0 | 263 | // oled.setDisplayBlink(true); |
wim | 3:1337e3d65ed0 | 264 | // wait(4); |
wim | 3:1337e3d65ed0 | 265 | // oled.setDisplayBlink(false); |
wim | 3:1337e3d65ed0 | 266 | |
wim | 3:1337e3d65ed0 | 267 | oled.setDisplayFade(true); |
wim | 3:1337e3d65ed0 | 268 | wait(4); |
wim | 3:1337e3d65ed0 | 269 | oled.setDisplayFade(false); |
wim | 3:1337e3d65ed0 | 270 | |
wim | 3:1337e3d65ed0 | 271 | pc.printf("D: Blink done\n\r"); |
wim | 3:1337e3d65ed0 | 272 | break; |
wim | 3:1337e3d65ed0 | 273 | |
wim | 3:1337e3d65ed0 | 274 | |
wim | 3:1337e3d65ed0 | 275 | case 'E' : |
wim | 3:1337e3d65ed0 | 276 | pc.printf("E: Big Chars\n\r"); |
wim | 3:1337e3d65ed0 | 277 | |
wim | 3:1337e3d65ed0 | 278 | oled.writeBigChar(0, 0, '+'); |
wim | 3:1337e3d65ed0 | 279 | oled.writeBigChar(0, 16, '7'); |
wim | 3:1337e3d65ed0 | 280 | oled.writeBigChar(0, 32, '8'); |
wim | 3:1337e3d65ed0 | 281 | oled.writeBigChar(0, 48, '9'); |
wim | 3:1337e3d65ed0 | 282 | |
wim | 3:1337e3d65ed0 | 283 | pc.printf("E: Big Chars done\n\r"); |
wim | 3:1337e3d65ed0 | 284 | break; |
wim | 5:dff410a009c5 | 285 | |
wim | 5:dff410a009c5 | 286 | case 'F' : |
wim | 5:dff410a009c5 | 287 | pc.printf("F: Progress Scale\n\r"); |
wim | 5:dff410a009c5 | 288 | |
wim | 5:dff410a009c5 | 289 | for (int percentage=0; percentage <= 100; percentage++) { |
wim | 5:dff410a009c5 | 290 | oled.writeProgressBar(2, 0, percentage); |
wim | 5:dff410a009c5 | 291 | oled.printf(" %3d %%", percentage); |
wim | 5:dff410a009c5 | 292 | |
wim | 5:dff410a009c5 | 293 | oled.writeProgressBar(4, 0, 100 - percentage); |
wim | 5:dff410a009c5 | 294 | oled.printf(" %3d %%", 100 - percentage); |
wim | 5:dff410a009c5 | 295 | wait(0.05); |
wim | 5:dff410a009c5 | 296 | } |
wim | 5:dff410a009c5 | 297 | |
wim | 5:dff410a009c5 | 298 | |
wim | 5:dff410a009c5 | 299 | pc.printf("F: Progress Scale done\n\r"); |
wim | 5:dff410a009c5 | 300 | break; |
wim | 5:dff410a009c5 | 301 | |
wim | 5:dff410a009c5 | 302 | |
wim | 3:1337e3d65ed0 | 303 | |
wim | 1:00053cb70ac5 | 304 | } //switch |
wim | 1:00053cb70ac5 | 305 | }//if |
wim | 1:00053cb70ac5 | 306 | }//while |
wim | 1:00053cb70ac5 | 307 | #endif |
wim | 1:00053cb70ac5 | 308 | |
wim | 1:00053cb70ac5 | 309 | pc.printf("Bye World!\n\r"); |
wim | 1:00053cb70ac5 | 310 | }//main |
wim | 1:00053cb70ac5 | 311 |