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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
| #define U8X8\_HAVE\_HW\_SPI #include <Arduino.h> #include <U8g2lib.h>
#ifdef U8X8\_HAVE\_HW\_SPI #include <SPI.h> #endif #ifdef U8X8\_HAVE\_HW\_I2C #include <Wire.h> #endif
U8G2_ST7920_128X64_1_SW_SPI u8g2(U8G2_R0, /\* clock=\*/ 13, /\* data=\*/ 11, /\* CS=\*/ 10, /\* reset=\*/ 8);
void u8g2\_prepare(void) { u8g2.setFont(u8g2_font_6x10_tf); u8g2.setFontRefHeightExtendedText(); u8g2.setDrawColor(1); u8g2.setFontPosTop(); u8g2.setFontDirection(0); }
void u8g2\_box\_title(uint8\_t a) { u8g2.drawStr( 10+a\*2, 5, "U8g2"); u8g2.drawStr( 10, 20, "GraphicsTest"); u8g2.drawFrame(0,0,u8g2.getDisplayWidth(),u8g2.getDisplayHeight() ); }
void u8g2\_box\_frame(uint8\_t a) { u8g2.drawStr( 0, 0, "drawBox"); u8g2.drawBox(5,10,20,10); u8g2.drawBox(10+a,15,30,7); u8g2.drawStr( 0, 30, "drawFrame"); u8g2.drawFrame(5,10+30,20,10); u8g2.drawFrame(10+a,15+30,30,7); }
void u8g2\_disc\_circle(uint8\_t a) { u8g2.drawStr( 0, 0, "drawDisc"); u8g2.drawDisc(10,18,9); u8g2.drawDisc(24+a,16,7); u8g2.drawStr( 0, 30, "drawCircle"); u8g2.drawCircle(10,18+30,9); u8g2.drawCircle(24+a,16+30,7); }
void u8g2\_r\_frame(uint8\_t a) { u8g2.drawStr( 0, 0, "drawRFrame/Box"); u8g2.drawRFrame(5, 10,40,30, a+1); u8g2.drawRBox(50, 10,25,40, a+1); }
void u8g2\_string(uint8\_t a) { u8g2.setFontDirection(0); u8g2.drawStr(30+a,31, " 0"); u8g2.setFontDirection(1); u8g2.drawStr(30,31+a, " 90"); u8g2.setFontDirection(2); u8g2.drawStr(30-a,31, " 180"); u8g2.setFontDirection(3); u8g2.drawStr(30,31-a, " 270"); }
void u8g2\_line(uint8\_t a) { u8g2.drawStr( 0, 0, "drawLine"); u8g2.drawLine(7+a, 10, 40, 55); u8g2.drawLine(7+a\*2, 10, 60, 55); u8g2.drawLine(7+a\*3, 10, 80, 55); u8g2.drawLine(7+a\*4, 10, 100, 55); }
void u8g2\_triangle(uint8\_t a) { uint16\_t offset = a; u8g2.drawStr( 0, 0, "drawTriangle"); u8g2.drawTriangle(14,7, 45,30, 10,40); u8g2.drawTriangle(14+offset,7-offset, 45+offset,30-offset, 57+offset,10-offset); u8g2.drawTriangle(57+offset\*2,10, 45+offset\*2,30, 86+offset\*2,53); u8g2.drawTriangle(10+offset,40+offset, 45+offset,30+offset, 86+offset,53+offset); }
void u8g2\_ascii\_1() { char s[2] = " "; uint8\_t x, y; u8g2.drawStr( 0, 0, "ASCII page 1"); for( y = 0; y < 6; y++ ) { for( x = 0; x < 16; x++ ) { s[0] = y\*16 + x + 32; u8g2.drawStr(x\*7, y\*10+10, s); } } }
void u8g2\_ascii\_2() { char s[2] = " "; uint8\_t x, y; u8g2.drawStr( 0, 0, "ASCII page 2"); for( y = 0; y < 6; y++ ) { for( x = 0; x < 16; x++ ) { s[0] = y\*16 + x + 160; u8g2.drawStr(x\*7, y\*10+10, s); } } }
void u8g2\_extra\_page(uint8\_t a) { u8g2.drawStr( 0, 0, "Unicode"); u8g2.setFont(u8g2_font_unifont_t_symbols); u8g2.setFontPosTop(); u8g2.drawUTF8(0, 24, "☀ ☁"); switch(a) { case 0: case 1: case 2: case 3: u8g2.drawUTF8(a\*3, 36, "☂"); break; case 4: case 5: case 6: case 7: u8g2.drawUTF8(a\*3, 36, "☔"); break; } }
void u8g2\_xor(uint8\_t a) { uint8\_t i; u8g2.drawStr( 0, 0, "XOR"); u8g2.setFontMode(1); u8g2.setDrawColor(2); for( i = 0; i < 5; i++) { u8g2.drawBox(10+i\*16, 18 + (i&1)\*4, 21,31); } u8g2.drawStr( 5+a, 19, "XOR XOR XOR XOR"); u8g2.setDrawColor(0); u8g2.drawStr( 5+a, 29, "CLR CLR CLR CLR"); u8g2.setDrawColor(1); u8g2.drawStr( 5+a, 39, "SET SET SET SET"); u8g2.setFontMode(0); }
#define cross\_width 24 #define cross\_height 24 static const unsigned char cross_bits[] U8X8_PROGMEM = { 0x00, 0x18, 0x00, 0x00, 0x24, 0x00, 0x00, 0x24, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x81, 0x00, 0x00, 0x81, 0x00, 0xC0, 0x00, 0x03, 0x38, 0x3C, 0x1C, 0x06, 0x42, 0x60, 0x01, 0x42, 0x80, 0x01, 0x42, 0x80, 0x06, 0x42, 0x60, 0x38, 0x3C, 0x1C, 0xC0, 0x00, 0x03, 0x00, 0x81, 0x00, 0x00, 0x81, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x24, 0x00, 0x00, 0x24, 0x00, 0x00, 0x18, 0x00, };
#define cross\_fill\_width 24 #define cross\_fill\_height 24 static const unsigned char cross_fill_bits[] U8X8_PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x18, 0x64, 0x00, 0x26, 0x84, 0x00, 0x21, 0x08, 0x81, 0x10, 0x08, 0x42, 0x10, 0x10, 0x3C, 0x08, 0x20, 0x00, 0x04, 0x40, 0x00, 0x02, 0x80, 0x00, 0x01, 0x80, 0x18, 0x01, 0x80, 0x18, 0x01, 0x80, 0x00, 0x01, 0x40, 0x00, 0x02, 0x20, 0x00, 0x04, 0x10, 0x3C, 0x08, 0x08, 0x42, 0x10, 0x08, 0x81, 0x10, 0x84, 0x00, 0x21, 0x64, 0x00, 0x26, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
#define cross\_block\_width 14 #define cross\_block\_height 14 static const unsigned char cross_block_bits[] U8X8_PROGMEM = { 0xFF, 0x3F, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0xC1, 0x20, 0xC1, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0xFF, 0x3F, };
void u8g2\_bitmap\_overlay(uint8\_t a) { uint8\_t frame_size = 28;
u8g2.drawStr(0, 0, "Bitmap overlay");
u8g2.drawStr(0, frame_size + 12, "Solid / transparent"); u8g2.setBitmapMode(false /\* solid \*/); u8g2.drawFrame(0, 10, frame_size, frame_size); u8g2.drawXBMP(2, 12, cross_width, cross_height, cross_bits); if(a & 4) u8g2.drawXBMP(7, 17, cross_block_width, cross_block_height, cross_block_bits);
u8g2.setBitmapMode(true /\* transparent\*/); u8g2.drawFrame(frame_size + 5, 10, frame_size, frame_size); u8g2.drawXBMP(frame_size + 7, 12, cross_width, cross_height, cross_bits); if(a & 4) u8g2.drawXBMP(frame_size + 12, 17, cross_block_width, cross_block_height, cross_block_bits); }
void u8g2\_bitmap\_modes(uint8\_t transparent) { const uint8\_t frame_size = 24;
u8g2.drawBox(0, frame_size \* 0.5, frame_size \* 5, frame_size); u8g2.drawStr(frame_size \* 0.5, 50, "Black"); u8g2.drawStr(frame_size \* 2, 50, "White"); u8g2.drawStr(frame_size \* 3.5, 50, "XOR"); if(!transparent) { u8g2.setBitmapMode(false /\* solid \*/); u8g2.drawStr(0, 0, "Solid bitmap"); } else { u8g2.setBitmapMode(true /\* transparent\*/); u8g2.drawStr(0, 0, "Transparent bitmap"); } u8g2.setDrawColor(0);// Black u8g2.drawXBMP(frame_size \* 0.5, 24, cross_width, cross_height, cross_bits); u8g2.setDrawColor(1); // White u8g2.drawXBMP(frame_size \* 2, 24, cross_width, cross_height, cross_bits); u8g2.setDrawColor(2); // XOR u8g2.drawXBMP(frame_size \* 3.5, 24, cross_width, cross_height, cross_bits); }
uint8\_t draw_state = 0;
void draw(void) { u8g2\_prepare(); switch(draw_state >> 3) { case 0: u8g2\_box\_title(draw_state&7); break; case 1: u8g2\_box\_frame(draw_state&7); break; case 2: u8g2\_disc\_circle(draw_state&7); break; case 3: u8g2\_r\_frame(draw_state&7); break; case 4: u8g2\_string(draw_state&7); break; case 5: u8g2\_line(draw_state&7); break; case 6: u8g2\_triangle(draw_state&7); break; case 7: u8g2\_ascii\_1(); break; case 8: u8g2\_ascii\_2(); break; case 9: u8g2\_extra\_page(draw_state&7); break; case 10: u8g2\_xor(draw_state&7); break; case 11: u8g2\_bitmap\_modes(0); break; case 12: u8g2\_bitmap\_modes(1); break; case 13: u8g2\_bitmap\_overlay(draw_state&7); break; } }
void setup(void) { u8g2.begin(); }
void loop(void) { // picture loop u8g2.firstPage(); do { draw(); } while( u8g2.nextPage() ); // increase the state draw_state++; if ( draw_state >= 14\*8 ) draw_state = 0;
// delay between each page delay(150);
}
|