Adafruit ssd1331 OLED 96x64 SPI model
Fork of OledLib by
spioled96x64.cpp@3:eab8d193745a, 2011-01-12 (annotated)
- Committer:
- TETSUYA
- Date:
- Wed Jan 12 07:54:58 2011 +0000
- Revision:
- 3:eab8d193745a
- Parent:
- 2:e331851128c1
- Child:
- 4:b68843c0269c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TETSUYA | 2:e331851128c1 | 1 | // ALO-095BWNN-J9 test program |
TETSUYA | 2:e331851128c1 | 2 | // See also "http://www.aitendo.co.jp/product/2099" |
TETSUYA | 3:eab8d193745a | 3 | // Ver1.00 初版完成 |
TETSUYA | 3:eab8d193745a | 4 | // Ver1.01 フォントをk6x8フォントに変更。定義Hファイルも全角とは分離 |
TETSUYA | 3:eab8d193745a | 5 | // フォント描画ルーチンが1ドットすくなく動くのを修正 |
TETSUYA | 3:eab8d193745a | 6 | // 任意カラーパターンを描画できるように修正 |
TETSUYA | 2:e331851128c1 | 7 | |
TETSUYA | 2:e331851128c1 | 8 | |
TETSUYA | 2:e331851128c1 | 9 | #include "mbed.h" |
TETSUYA | 2:e331851128c1 | 10 | #include "spioled96x64.h" |
TETSUYA | 2:e331851128c1 | 11 | |
TETSUYA | 2:e331851128c1 | 12 | //Serial pc(USBTX, USBRX); |
TETSUYA | 2:e331851128c1 | 13 | #define countof(x) ( sizeof(x) / sizeof(x[0]) ) |
TETSUYA | 2:e331851128c1 | 14 | |
TETSUYA | 2:e331851128c1 | 15 | |
TETSUYA | 3:eab8d193745a | 16 | // コンストラクタ |
TETSUYA | 2:e331851128c1 | 17 | SPIOLED96x64::SPIOLED96x64(PinName cs_pin, PinName rst_pin, PinName a0_pin, PinName mosi_pin, PinName miso_pin, PinName sclk_pin) |
TETSUYA | 2:e331851128c1 | 18 | : CS(cs_pin), RES(rst_pin), DC(a0_pin), spi(mosi_pin, miso_pin, sclk_pin) |
TETSUYA | 2:e331851128c1 | 19 | { |
TETSUYA | 2:e331851128c1 | 20 | Init(); |
TETSUYA | 2:e331851128c1 | 21 | } |
TETSUYA | 2:e331851128c1 | 22 | |
TETSUYA | 3:eab8d193745a | 23 | // イニシャライザ |
TETSUYA | 2:e331851128c1 | 24 | void SPIOLED96x64::Init(void) |
TETSUYA | 2:e331851128c1 | 25 | { |
TETSUYA | 2:e331851128c1 | 26 | |
TETSUYA | 2:e331851128c1 | 27 | spi.format(8,3); //8bit frame and POL=1 /PHA=1(UpEdge Sampled) |
TETSUYA | 2:e331851128c1 | 28 | spi.frequency(25000000); // modify later |
TETSUYA | 2:e331851128c1 | 29 | //spi.frequency(42000000); // modify later |
TETSUYA | 2:e331851128c1 | 30 | |
TETSUYA | 2:e331851128c1 | 31 | // reset |
TETSUYA | 2:e331851128c1 | 32 | wait_ms(200); |
TETSUYA | 2:e331851128c1 | 33 | RES = 0; //Reset active |
TETSUYA | 2:e331851128c1 | 34 | wait_ms(200); |
TETSUYA | 2:e331851128c1 | 35 | RES = 1; |
TETSUYA | 2:e331851128c1 | 36 | |
TETSUYA | 2:e331851128c1 | 37 | |
TETSUYA | 2:e331851128c1 | 38 | // initialize sequence |
TETSUYA | 2:e331851128c1 | 39 | RegWrite(0xae); //OLED display OFF |
TETSUYA | 2:e331851128c1 | 40 | |
TETSUYA | 2:e331851128c1 | 41 | //Row Address |
TETSUYA | 2:e331851128c1 | 42 | RegWrite(0x75); /* Set Row Address */ |
TETSUYA | 2:e331851128c1 | 43 | RegWrite(0x00); /* Start = 0 */ |
TETSUYA | 2:e331851128c1 | 44 | RegWrite(0x3f); /* End = 63 */ |
TETSUYA | 2:e331851128c1 | 45 | RegWrite(0x15); /* Set Column Address */ |
TETSUYA | 2:e331851128c1 | 46 | RegWrite(0x00); /* Start = 0 */ |
TETSUYA | 2:e331851128c1 | 47 | RegWrite(0x5F); /* End = 95 */ |
TETSUYA | 2:e331851128c1 | 48 | |
TETSUYA | 2:e331851128c1 | 49 | //Contrast |
TETSUYA | 2:e331851128c1 | 50 | RegWrite(0xa0); //Set remap & data format 0111 0000 |
TETSUYA | 2:e331851128c1 | 51 | RegWrite(0x74); // 65k Color/8bit buswidth/format1 |
TETSUYA | 2:e331851128c1 | 52 | RegWrite(0xa1); //set display start row RAM |
TETSUYA | 2:e331851128c1 | 53 | RegWrite(0x00); |
TETSUYA | 2:e331851128c1 | 54 | RegWrite(0xa2); //set dispaly offset |
TETSUYA | 2:e331851128c1 | 55 | RegWrite(0x00); |
TETSUYA | 2:e331851128c1 | 56 | RegWrite(0xa4); //Set Display Mode |
TETSUYA | 2:e331851128c1 | 57 | RegWrite(0xa8); //Set Multiplex Ratio |
TETSUYA | 2:e331851128c1 | 58 | RegWrite(0x3f); |
TETSUYA | 2:e331851128c1 | 59 | RegWrite(0xad); //Set Master Configuration |
TETSUYA | 2:e331851128c1 | 60 | RegWrite(0x8f); //(External VCC Supply Selected) |
TETSUYA | 2:e331851128c1 | 61 | RegWrite(0xB0); //Set Power Saving Mode |
TETSUYA | 2:e331851128c1 | 62 | RegWrite(0x1a); |
TETSUYA | 2:e331851128c1 | 63 | RegWrite(0xB1); //Set Phase 1 & 2 Period Adjustment |
TETSUYA | 2:e331851128c1 | 64 | RegWrite(0x74); |
TETSUYA | 2:e331851128c1 | 65 | RegWrite(0xb3); //Set Display Clock Divide Ratio / Oscillator Frequency |
TETSUYA | 2:e331851128c1 | 66 | RegWrite(0xd0); |
TETSUYA | 2:e331851128c1 | 67 | RegWrite(0x8A); //Set Second Pre-charge Speed of Color A |
TETSUYA | 2:e331851128c1 | 68 | RegWrite(0x81); |
TETSUYA | 2:e331851128c1 | 69 | RegWrite(0x8B); //Set Second Pre-charge Speed of Color B |
TETSUYA | 2:e331851128c1 | 70 | RegWrite(0x82); |
TETSUYA | 2:e331851128c1 | 71 | RegWrite(0x8C); //Set Second Pre-charge Speed of Color C |
TETSUYA | 2:e331851128c1 | 72 | RegWrite(0x83); |
TETSUYA | 2:e331851128c1 | 73 | RegWrite(0xBB); //Set Pre-charge Level |
TETSUYA | 2:e331851128c1 | 74 | RegWrite(0x3e); |
TETSUYA | 2:e331851128c1 | 75 | RegWrite(0xBE); //Set VCOMH |
TETSUYA | 2:e331851128c1 | 76 | RegWrite(0x3e); |
TETSUYA | 2:e331851128c1 | 77 | RegWrite(0x87); //Set Master Current Control |
TETSUYA | 2:e331851128c1 | 78 | RegWrite(0x0f); |
TETSUYA | 2:e331851128c1 | 79 | RegWrite(0x81); //Set Contrast Control for Color gAh |
TETSUYA | 2:e331851128c1 | 80 | RegWrite(0x80); |
TETSUYA | 2:e331851128c1 | 81 | RegWrite(0x82); //Set Contrast Control for Color gBh |
TETSUYA | 2:e331851128c1 | 82 | RegWrite(0x80); |
TETSUYA | 2:e331851128c1 | 83 | RegWrite(0x83); //Set Contrast Control for Color gCh |
TETSUYA | 2:e331851128c1 | 84 | RegWrite(0x80); |
TETSUYA | 2:e331851128c1 | 85 | RegWrite(0xaf); //display ON |
TETSUYA | 2:e331851128c1 | 86 | |
TETSUYA | 2:e331851128c1 | 87 | x_locate = 0; |
TETSUYA | 2:e331851128c1 | 88 | y_locate = 0; |
TETSUYA | 2:e331851128c1 | 89 | chr_size = CS_NORMAL; |
TETSUYA | 2:e331851128c1 | 90 | Fill_Screen(0); |
TETSUYA | 2:e331851128c1 | 91 | } |
TETSUYA | 2:e331851128c1 | 92 | |
TETSUYA | 3:eab8d193745a | 93 | // レジスタライト |
TETSUYA | 2:e331851128c1 | 94 | void SPIOLED96x64::RegWrite(unsigned char Command) |
TETSUYA | 2:e331851128c1 | 95 | { |
TETSUYA | 2:e331851128c1 | 96 | DC = 0; // Command |
TETSUYA | 2:e331851128c1 | 97 | CS = 0; // CS active(toggle OLED) |
TETSUYA | 2:e331851128c1 | 98 | spi.write(Command); |
TETSUYA | 2:e331851128c1 | 99 | CS = 1; |
TETSUYA | 2:e331851128c1 | 100 | } |
TETSUYA | 3:eab8d193745a | 101 | // マルチバイト レジスタ設定 |
TETSUYA | 2:e331851128c1 | 102 | void SPIOLED96x64::RegWriteM(unsigned char *Command, int count) |
TETSUYA | 2:e331851128c1 | 103 | { |
TETSUYA | 2:e331851128c1 | 104 | int i; |
TETSUYA | 2:e331851128c1 | 105 | DC = 0; // Command |
TETSUYA | 2:e331851128c1 | 106 | CS = 0; // CS active(toggle OLED) |
TETSUYA | 2:e331851128c1 | 107 | for( i=0;i<count;i++){ |
TETSUYA | 2:e331851128c1 | 108 | spi.write(*Command++); |
TETSUYA | 2:e331851128c1 | 109 | } |
TETSUYA | 2:e331851128c1 | 110 | CS = 1; |
TETSUYA | 2:e331851128c1 | 111 | } |
TETSUYA | 2:e331851128c1 | 112 | |
TETSUYA | 2:e331851128c1 | 113 | |
TETSUYA | 3:eab8d193745a | 114 | // データライト |
TETSUYA | 2:e331851128c1 | 115 | void SPIOLED96x64::DataWrite(unsigned char c) |
TETSUYA | 2:e331851128c1 | 116 | { |
TETSUYA | 2:e331851128c1 | 117 | DC = 1; // DATA |
TETSUYA | 2:e331851128c1 | 118 | CS = 0; // CS active(toggle OLED) |
TETSUYA | 2:e331851128c1 | 119 | spi.write(c); |
TETSUYA | 2:e331851128c1 | 120 | CS = 1; |
TETSUYA | 2:e331851128c1 | 121 | } |
TETSUYA | 2:e331851128c1 | 122 | |
TETSUYA | 3:eab8d193745a | 123 | // 16ビット書き込み |
TETSUYA | 2:e331851128c1 | 124 | void SPIOLED96x64::DataWrite_to(unsigned int Dat) |
TETSUYA | 2:e331851128c1 | 125 | { |
TETSUYA | 2:e331851128c1 | 126 | DC = 1; // DATA |
TETSUYA | 2:e331851128c1 | 127 | CS = 0; // CS active(toggle OLED) |
TETSUYA | 2:e331851128c1 | 128 | spi.write((unsigned char)((Dat >> 8)& 0x00ff)); |
TETSUYA | 2:e331851128c1 | 129 | spi.write((unsigned char)(Dat & 0x00ff)); |
TETSUYA | 2:e331851128c1 | 130 | CS = 1; |
TETSUYA | 2:e331851128c1 | 131 | /* |
TETSUYA | 2:e331851128c1 | 132 | DataWrite((unsigned char)((Dat >> 8)& 0x00ff)); |
TETSUYA | 2:e331851128c1 | 133 | DataWrite((unsigned char)(Dat & 0x00ff)); |
TETSUYA | 2:e331851128c1 | 134 | */ |
TETSUYA | 2:e331851128c1 | 135 | } |
TETSUYA | 2:e331851128c1 | 136 | |
TETSUYA | 2:e331851128c1 | 137 | |
TETSUYA | 2:e331851128c1 | 138 | |
TETSUYA | 3:eab8d193745a | 139 | // 指定座標に指定色でドット描画 |
TETSUYA | 2:e331851128c1 | 140 | void SPIOLED96x64::Draw_Dot(int x,int y,unsigned int Color) |
TETSUYA | 2:e331851128c1 | 141 | { |
TETSUYA | 2:e331851128c1 | 142 | unsigned char cmd[7]={0x15,0x00,0x00,0x75,0x00,0x00,0x00}; |
TETSUYA | 2:e331851128c1 | 143 | if ( ( x > Dis_X_MAX ) |
TETSUYA | 2:e331851128c1 | 144 | ||( y > Dis_Y_MAX ) ) return ; |
TETSUYA | 2:e331851128c1 | 145 | /* |
TETSUYA | 2:e331851128c1 | 146 | RegWrite(0x15); |
TETSUYA | 2:e331851128c1 | 147 | RegWrite((unsigned char)x); // Column Start Address |
TETSUYA | 2:e331851128c1 | 148 | RegWrite((unsigned char)x); // Column End Address |
TETSUYA | 2:e331851128c1 | 149 | RegWrite(0x75); |
TETSUYA | 2:e331851128c1 | 150 | RegWrite((unsigned char)y); // Row Start Address |
TETSUYA | 2:e331851128c1 | 151 | RegWrite((unsigned char)y); // Row End Address |
TETSUYA | 2:e331851128c1 | 152 | */ |
TETSUYA | 2:e331851128c1 | 153 | cmd[1] = (unsigned char)x; |
TETSUYA | 2:e331851128c1 | 154 | cmd[2] = (unsigned char)x; |
TETSUYA | 2:e331851128c1 | 155 | cmd[4] = (unsigned char)y; |
TETSUYA | 2:e331851128c1 | 156 | cmd[5] = (unsigned char)y; |
TETSUYA | 2:e331851128c1 | 157 | RegWriteM(cmd, 6); |
TETSUYA | 2:e331851128c1 | 158 | DataWrite_to(Color); |
TETSUYA | 2:e331851128c1 | 159 | //DataWrite_to(Color); |
TETSUYA | 2:e331851128c1 | 160 | } |
TETSUYA | 2:e331851128c1 | 161 | |
TETSUYA | 2:e331851128c1 | 162 | |
TETSUYA | 2:e331851128c1 | 163 | |
TETSUYA | 2:e331851128c1 | 164 | void SPIOLED96x64::ChangeFontColor(unsigned int color) |
TETSUYA | 2:e331851128c1 | 165 | { |
TETSUYA | 2:e331851128c1 | 166 | Char_Color = color; |
TETSUYA | 2:e331851128c1 | 167 | } |
TETSUYA | 2:e331851128c1 | 168 | void SPIOLED96x64::ChangeBGColor(unsigned int color) |
TETSUYA | 2:e331851128c1 | 169 | { |
TETSUYA | 2:e331851128c1 | 170 | BGround_Color = color; |
TETSUYA | 2:e331851128c1 | 171 | } |
TETSUYA | 2:e331851128c1 | 172 | |
TETSUYA | 2:e331851128c1 | 173 | |
TETSUYA | 2:e331851128c1 | 174 | // CS_NORMAL/CS_WIDE/CS_HIGH/CS_WH |
TETSUYA | 2:e331851128c1 | 175 | void SPIOLED96x64::SetFontSize(int Csize) |
TETSUYA | 2:e331851128c1 | 176 | { |
TETSUYA | 2:e331851128c1 | 177 | chr_size = Csize; |
TETSUYA | 2:e331851128c1 | 178 | } |
TETSUYA | 3:eab8d193745a | 179 | // 縦横の倍率変化量を返す |
TETSUYA | 2:e331851128c1 | 180 | void SPIOLED96x64::CS_Conv(int *lpx,int *lpy) |
TETSUYA | 2:e331851128c1 | 181 | { |
TETSUYA | 2:e331851128c1 | 182 | switch( chr_size ){ |
TETSUYA | 2:e331851128c1 | 183 | case CS_WIDE: |
TETSUYA | 2:e331851128c1 | 184 | *lpx=2; |
TETSUYA | 2:e331851128c1 | 185 | *lpy=1; |
TETSUYA | 2:e331851128c1 | 186 | break; |
TETSUYA | 2:e331851128c1 | 187 | case CS_HIGH: |
TETSUYA | 2:e331851128c1 | 188 | *lpx=1; |
TETSUYA | 2:e331851128c1 | 189 | *lpy=2; |
TETSUYA | 2:e331851128c1 | 190 | break; |
TETSUYA | 2:e331851128c1 | 191 | case CS_WH : |
TETSUYA | 2:e331851128c1 | 192 | *lpx=2; |
TETSUYA | 2:e331851128c1 | 193 | *lpy=2; |
TETSUYA | 2:e331851128c1 | 194 | break; |
TETSUYA | 2:e331851128c1 | 195 | case CS_WHx36 : |
TETSUYA | 2:e331851128c1 | 196 | *lpx=6; |
TETSUYA | 2:e331851128c1 | 197 | *lpy=6; |
TETSUYA | 2:e331851128c1 | 198 | break; |
TETSUYA | 2:e331851128c1 | 199 | case CS_NORMAL: |
TETSUYA | 2:e331851128c1 | 200 | default: |
TETSUYA | 2:e331851128c1 | 201 | *lpx=1; |
TETSUYA | 2:e331851128c1 | 202 | *lpy=1; |
TETSUYA | 2:e331851128c1 | 203 | break; |
TETSUYA | 2:e331851128c1 | 204 | } |
TETSUYA | 2:e331851128c1 | 205 | |
TETSUYA | 2:e331851128c1 | 206 | } |
TETSUYA | 2:e331851128c1 | 207 | |
TETSUYA | 3:eab8d193745a | 208 | // 全角キャラクタの描画処理 |
TETSUYA | 3:eab8d193745a | 209 | // アスキーキャラクタの位置指定表示(5x7dotフォント) |
TETSUYA | 3:eab8d193745a | 210 | // フォントの特性上、最上位ビットから順に↑方向に記述される |
TETSUYA | 2:e331851128c1 | 211 | void SPIOLED96x64::PutChar(int x,int y,unsigned int a) |
TETSUYA | 2:e331851128c1 | 212 | { |
TETSUYA | 2:e331851128c1 | 213 | int i,j; |
TETSUYA | 2:e331851128c1 | 214 | unsigned char Temp=0; |
TETSUYA | 2:e331851128c1 | 215 | int lpx, lpy, k,l; |
TETSUYA | 2:e331851128c1 | 216 | int xw; |
TETSUYA | 2:e331851128c1 | 217 | j = 0; |
TETSUYA | 2:e331851128c1 | 218 | i = 0; |
TETSUYA | 2:e331851128c1 | 219 | |
TETSUYA | 2:e331851128c1 | 220 | CS_Conv(&lpx, &lpy); |
TETSUYA | 2:e331851128c1 | 221 | if ( a < 0x80 ) { xw = X_WitchASC; if(a < 32)a=32; } // ASCII |
TETSUYA | 2:e331851128c1 | 222 | else xw = X_Witch; // Kanji |
TETSUYA | 2:e331851128c1 | 223 | |
TETSUYA | 2:e331851128c1 | 224 | for(i=0; i<xw; i++){ |
TETSUYA | 2:e331851128c1 | 225 | for ( l=0;l<lpx;l++){ |
TETSUYA | 2:e331851128c1 | 226 | if ( a < 0x80 ) Temp = FontLookup_ABC[a-32][i]; // ASCII(0x20~0x7f)==> 0x00~0x5f |
TETSUYA | 2:e331851128c1 | 227 | else Temp = FontLookup[a][i]; // Kanji |
TETSUYA | 2:e331851128c1 | 228 | |
TETSUYA | 2:e331851128c1 | 229 | for(j=Y_Witch-1;j>=0;j--){ |
TETSUYA | 2:e331851128c1 | 230 | for ( k=0;k<lpy;k++){ |
TETSUYA | 2:e331851128c1 | 231 | Draw_Dot(x+(i*lpx)+l,y+((j+1)*lpy)-k, ((Temp & 0x80)==0x80) ? Char_Color : BGround_Color); |
TETSUYA | 2:e331851128c1 | 232 | } |
TETSUYA | 2:e331851128c1 | 233 | Temp = Temp << 1; |
TETSUYA | 2:e331851128c1 | 234 | } |
TETSUYA | 2:e331851128c1 | 235 | } |
TETSUYA | 2:e331851128c1 | 236 | } |
TETSUYA | 2:e331851128c1 | 237 | } |
TETSUYA | 2:e331851128c1 | 238 | |
TETSUYA | 2:e331851128c1 | 239 | |
TETSUYA | 3:eab8d193745a | 240 | // 純粋仮想関数でputcを実現 |
TETSUYA | 2:e331851128c1 | 241 | int SPIOLED96x64::_putc( int c ) |
TETSUYA | 2:e331851128c1 | 242 | { |
TETSUYA | 2:e331851128c1 | 243 | static bool kstate = false; |
TETSUYA | 2:e331851128c1 | 244 | static unsigned int kbuf = 0; |
TETSUYA | 2:e331851128c1 | 245 | unsigned int p; |
TETSUYA | 2:e331851128c1 | 246 | int lpx, lpy, w; |
TETSUYA | 2:e331851128c1 | 247 | |
TETSUYA | 2:e331851128c1 | 248 | if(!kstate && ((0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xfc))){ |
TETSUYA | 2:e331851128c1 | 249 | kstate = true; |
TETSUYA | 2:e331851128c1 | 250 | kbuf = c; |
TETSUYA | 2:e331851128c1 | 251 | return c; |
TETSUYA | 2:e331851128c1 | 252 | } |
TETSUYA | 2:e331851128c1 | 253 | |
TETSUYA | 2:e331851128c1 | 254 | if(kstate){ // 2nd byte of shift-jis |
TETSUYA | 2:e331851128c1 | 255 | kstate = false; |
TETSUYA | 2:e331851128c1 | 256 | p = findface(kbuf << 8 | c); |
TETSUYA | 2:e331851128c1 | 257 | w = X_Witch; |
TETSUYA | 2:e331851128c1 | 258 | } else { |
TETSUYA | 2:e331851128c1 | 259 | p = c; |
TETSUYA | 3:eab8d193745a | 260 | w = X_WitchASC; // 半角は近接するので、1ドットあける |
TETSUYA | 2:e331851128c1 | 261 | } |
TETSUYA | 2:e331851128c1 | 262 | PutChar( x_locate , y_locate ,p); |
TETSUYA | 2:e331851128c1 | 263 | CS_Conv(&lpx, &lpy); |
TETSUYA | 2:e331851128c1 | 264 | x_locate += (w*lpx); |
TETSUYA | 2:e331851128c1 | 265 | |
TETSUYA | 2:e331851128c1 | 266 | |
TETSUYA | 2:e331851128c1 | 267 | /* |
TETSUYA | 2:e331851128c1 | 268 | if(kstate== false){ |
TETSUYA | 3:eab8d193745a | 269 | //画面右端でのクリッピング |
TETSUYA | 2:e331851128c1 | 270 | if(x_locate + (xsize - 1) >= Dis_X_MAX){ |
TETSUYA | 2:e331851128c1 | 271 | x_locate = 0; |
TETSUYA | 2:e331851128c1 | 272 | y_locate += Y_Witch; |
TETSUYA | 2:e331851128c1 | 273 | if(y_locate >= Dis_Y_MAX) y_locate=0; |
TETSUYA | 2:e331851128c1 | 274 | } |
TETSUYA | 2:e331851128c1 | 275 | } |
TETSUYA | 2:e331851128c1 | 276 | */ |
TETSUYA | 2:e331851128c1 | 277 | return c; |
TETSUYA | 2:e331851128c1 | 278 | } |
TETSUYA | 2:e331851128c1 | 279 | |
TETSUYA | 2:e331851128c1 | 280 | int SPIOLED96x64::_getc() |
TETSUYA | 2:e331851128c1 | 281 | { |
TETSUYA | 2:e331851128c1 | 282 | return -1; |
TETSUYA | 2:e331851128c1 | 283 | } |
TETSUYA | 2:e331851128c1 | 284 | |
TETSUYA | 2:e331851128c1 | 285 | /* |
TETSUYA | 2:e331851128c1 | 286 | void SPIOLED96x64::puts( char *s ) |
TETSUYA | 2:e331851128c1 | 287 | { |
TETSUYA | 2:e331851128c1 | 288 | unsigned char c; |
TETSUYA | 2:e331851128c1 | 289 | while((c = *s++) != '\0') putc((int)c & 0xff); |
TETSUYA | 2:e331851128c1 | 290 | } |
TETSUYA | 2:e331851128c1 | 291 | */ |
TETSUYA | 2:e331851128c1 | 292 | |
TETSUYA | 2:e331851128c1 | 293 | |
TETSUYA | 2:e331851128c1 | 294 | void SPIOLED96x64::locate(int column, int row) |
TETSUYA | 2:e331851128c1 | 295 | { |
TETSUYA | 2:e331851128c1 | 296 | x_locate = column; |
TETSUYA | 2:e331851128c1 | 297 | y_locate = row; |
TETSUYA | 2:e331851128c1 | 298 | } |
TETSUYA | 2:e331851128c1 | 299 | |
TETSUYA | 2:e331851128c1 | 300 | int SPIOLED96x64::row() |
TETSUYA | 2:e331851128c1 | 301 | { |
TETSUYA | 2:e331851128c1 | 302 | return y_locate; |
TETSUYA | 2:e331851128c1 | 303 | } |
TETSUYA | 2:e331851128c1 | 304 | int SPIOLED96x64::column() |
TETSUYA | 2:e331851128c1 | 305 | { |
TETSUYA | 2:e331851128c1 | 306 | return x_locate; |
TETSUYA | 2:e331851128c1 | 307 | } |
TETSUYA | 2:e331851128c1 | 308 | |
TETSUYA | 3:eab8d193745a | 309 | // SJIS漢字コードテーブルから指定の配列アドレスを算出する |
TETSUYA | 2:e331851128c1 | 310 | unsigned int SPIOLED96x64::findface(unsigned short c) |
TETSUYA | 2:e331851128c1 | 311 | { |
TETSUYA | 2:e331851128c1 | 312 | unsigned int p = 0; |
TETSUYA | 2:e331851128c1 | 313 | int i, sum; |
TETSUYA | 2:e331851128c1 | 314 | for(sum = i = 0; i < countof(font8table); i++){ |
TETSUYA | 2:e331851128c1 | 315 | if(font8table[i].start <= c && c <= font8table[i].end){ |
TETSUYA | 2:e331851128c1 | 316 | p = (sum + c - font8table[i].start); |
TETSUYA | 2:e331851128c1 | 317 | break; |
TETSUYA | 2:e331851128c1 | 318 | } |
TETSUYA | 2:e331851128c1 | 319 | sum += font8table[i].end - font8table[i].start + 1; |
TETSUYA | 2:e331851128c1 | 320 | } |
TETSUYA | 2:e331851128c1 | 321 | return p; |
TETSUYA | 2:e331851128c1 | 322 | } |
TETSUYA | 2:e331851128c1 | 323 | |
TETSUYA | 3:eab8d193745a | 324 | // RGB(555)をRGB565値に変換 |
TETSUYA | 2:e331851128c1 | 325 | // 00~1F(0~31) |
TETSUYA | 2:e331851128c1 | 326 | unsigned int SPIOLED96x64::ColorConv(unsigned int R,unsigned int G,unsigned int B) |
TETSUYA | 2:e331851128c1 | 327 | { |
TETSUYA | 2:e331851128c1 | 328 | unsigned int ret = 0; |
TETSUYA | 2:e331851128c1 | 329 | unsigned int rv, gv, bv; |
TETSUYA | 2:e331851128c1 | 330 | |
TETSUYA | 2:e331851128c1 | 331 | if ( R > 0x1f ) rv= 0x1f; |
TETSUYA | 2:e331851128c1 | 332 | else rv= (unsigned int)( R & 0x1F ); |
TETSUYA | 2:e331851128c1 | 333 | if ( G > 0x1f ) gv= 0x3f; |
TETSUYA | 2:e331851128c1 | 334 | else gv= (unsigned int)( (G<<1) & 0x3F ); |
TETSUYA | 2:e331851128c1 | 335 | if ( B > 0x1f ) bv= 0x1f; |
TETSUYA | 2:e331851128c1 | 336 | else bv= (unsigned int)( B & 0x1F ); |
TETSUYA | 2:e331851128c1 | 337 | |
TETSUYA | 2:e331851128c1 | 338 | ret = (bv << 11) + (gv << 5) + rv; |
TETSUYA | 2:e331851128c1 | 339 | return ret; |
TETSUYA | 2:e331851128c1 | 340 | } |
TETSUYA | 3:eab8d193745a | 341 | // 画面の指定色塗りつぶし指定領域し |
TETSUYA | 2:e331851128c1 | 342 | void SPIOLED96x64::Box(int x1,int y1, int x2, int y2, unsigned int Color, int fill) |
TETSUYA | 2:e331851128c1 | 343 | { |
TETSUYA | 2:e331851128c1 | 344 | unsigned char i,j; |
TETSUYA | 2:e331851128c1 | 345 | |
TETSUYA | 2:e331851128c1 | 346 | switch( fill ) { |
TETSUYA | 2:e331851128c1 | 347 | case 0: // box |
TETSUYA | 2:e331851128c1 | 348 | line( x1, y1, x2, y1, Color, 0 ); |
TETSUYA | 2:e331851128c1 | 349 | line( x2, y1, x2, y2, Color, 0 ); |
TETSUYA | 2:e331851128c1 | 350 | line( x2, y2, x1, y2, Color, 0 ); |
TETSUYA | 2:e331851128c1 | 351 | line( x1, y2, x1, y1, Color, 0 ); |
TETSUYA | 2:e331851128c1 | 352 | break; |
TETSUYA | 2:e331851128c1 | 353 | default: // fill |
TETSUYA | 2:e331851128c1 | 354 | for(i=y1;i<=y2;i++) |
TETSUYA | 2:e331851128c1 | 355 | { |
TETSUYA | 2:e331851128c1 | 356 | for(j=x1;j<=x2;j++) |
TETSUYA | 2:e331851128c1 | 357 | { |
TETSUYA | 2:e331851128c1 | 358 | Draw_Dot(j,i,Color); |
TETSUYA | 2:e331851128c1 | 359 | // DataWrite_to(Dot); |
TETSUYA | 2:e331851128c1 | 360 | } |
TETSUYA | 2:e331851128c1 | 361 | } |
TETSUYA | 2:e331851128c1 | 362 | break; |
TETSUYA | 2:e331851128c1 | 363 | } |
TETSUYA | 2:e331851128c1 | 364 | } |
TETSUYA | 2:e331851128c1 | 365 | |
TETSUYA | 3:eab8d193745a | 366 | // 画面の指定色塗りつぶし |
TETSUYA | 2:e331851128c1 | 367 | void SPIOLED96x64::Fill_Screen(unsigned int Color) |
TETSUYA | 2:e331851128c1 | 368 | { |
TETSUYA | 2:e331851128c1 | 369 | BGround_Color = Color; |
TETSUYA | 2:e331851128c1 | 370 | Box( 0,0, Dis_X_MAX, Dis_Y_MAX , Color , 1); |
TETSUYA | 2:e331851128c1 | 371 | } |
TETSUYA | 2:e331851128c1 | 372 | |
TETSUYA | 3:eab8d193745a | 373 | // Bresenhamの線分発生アルゴリズム |
TETSUYA | 3:eab8d193745a | 374 | // 整数のみによる線分描画 |
TETSUYA | 3:eab8d193745a | 375 | // (x0,y0)-(x1,y1)へのライン描画 参照:http //www2.starcat.ne.jp/~fussy/algo/algo1-1.htm |
TETSUYA | 3:eab8d193745a | 376 | // pat : 0:実線 1:破線 |
TETSUYA | 2:e331851128c1 | 377 | void SPIOLED96x64::line( int x0, int y0, int x1, int y1, unsigned int fore_col ,int pat ) |
TETSUYA | 2:e331851128c1 | 378 | { |
TETSUYA | 2:e331851128c1 | 379 | int E,x,y; |
TETSUYA | 2:e331851128c1 | 380 | int dx,dy,sx,sy,i; |
TETSUYA | 2:e331851128c1 | 381 | int cl=1 ; |
TETSUYA | 3:eab8d193745a | 382 | int clc=0; // 破線用のカウンタ |
TETSUYA | 2:e331851128c1 | 383 | |
TETSUYA | 3:eab8d193745a | 384 | sx = ( x1 > x0 ) ? 1 : -1; // xの移動方向 |
TETSUYA | 3:eab8d193745a | 385 | dx = ( x1 > x0 ) ? x1 - x0 : x0 - x1; // xの移動量 |
TETSUYA | 3:eab8d193745a | 386 | sy = ( y1 > y0 ) ? 1 : -1; // yの移動方向 |
TETSUYA | 3:eab8d193745a | 387 | dy = ( y1 > y0 ) ? y1 - y0 : y0 - y1; // yの移動量 |
TETSUYA | 2:e331851128c1 | 388 | |
TETSUYA | 2:e331851128c1 | 389 | x = x0; |
TETSUYA | 2:e331851128c1 | 390 | y = y0; |
TETSUYA | 2:e331851128c1 | 391 | |
TETSUYA | 2:e331851128c1 | 392 | if( dx >= dy ) { |
TETSUYA | 3:eab8d193745a | 393 | /* 傾きが1以下の場合 */ |
TETSUYA | 2:e331851128c1 | 394 | E = -dx; |
TETSUYA | 2:e331851128c1 | 395 | for( i = 0; i <= dx; i++ ) { |
TETSUYA | 2:e331851128c1 | 396 | switch( pat ) { |
TETSUYA | 3:eab8d193745a | 397 | case 0: // 実線 |
TETSUYA | 2:e331851128c1 | 398 | cl = fore_col; |
TETSUYA | 2:e331851128c1 | 399 | break; |
TETSUYA | 3:eab8d193745a | 400 | default: // 破線 |
TETSUYA | 2:e331851128c1 | 401 | if ( clc>=3 ) { |
TETSUYA | 2:e331851128c1 | 402 | if ( cl==fore_col ) cl=BGround_Color; |
TETSUYA | 2:e331851128c1 | 403 | else cl=fore_col; |
TETSUYA | 2:e331851128c1 | 404 | clc=0; |
TETSUYA | 2:e331851128c1 | 405 | }else{ |
TETSUYA | 2:e331851128c1 | 406 | clc++; |
TETSUYA | 2:e331851128c1 | 407 | } |
TETSUYA | 2:e331851128c1 | 408 | break; |
TETSUYA | 2:e331851128c1 | 409 | } |
TETSUYA | 2:e331851128c1 | 410 | |
TETSUYA | 2:e331851128c1 | 411 | Draw_Dot(x,y,cl); |
TETSUYA | 2:e331851128c1 | 412 | x += sx; |
TETSUYA | 2:e331851128c1 | 413 | E += 2 * dy; |
TETSUYA | 2:e331851128c1 | 414 | if( E >= 0 ) { |
TETSUYA | 2:e331851128c1 | 415 | y += sy; |
TETSUYA | 2:e331851128c1 | 416 | E -= 2 * dx; |
TETSUYA | 2:e331851128c1 | 417 | } |
TETSUYA | 2:e331851128c1 | 418 | } |
TETSUYA | 2:e331851128c1 | 419 | }else{ |
TETSUYA | 3:eab8d193745a | 420 | /* 傾きが1より大きい場合 */ |
TETSUYA | 2:e331851128c1 | 421 | E = -dy; |
TETSUYA | 2:e331851128c1 | 422 | for( i = 0; i <= dy; i++ ) { |
TETSUYA | 2:e331851128c1 | 423 | switch( pat ) { |
TETSUYA | 3:eab8d193745a | 424 | case 0: // 実線 |
TETSUYA | 2:e331851128c1 | 425 | cl = fore_col; |
TETSUYA | 2:e331851128c1 | 426 | break; |
TETSUYA | 3:eab8d193745a | 427 | default: // 破線 |
TETSUYA | 2:e331851128c1 | 428 | if ( clc>=3 ) { |
TETSUYA | 2:e331851128c1 | 429 | if ( cl==fore_col ) cl=BGround_Color; |
TETSUYA | 2:e331851128c1 | 430 | else cl=fore_col; |
TETSUYA | 2:e331851128c1 | 431 | clc=0; |
TETSUYA | 2:e331851128c1 | 432 | }else{ |
TETSUYA | 2:e331851128c1 | 433 | clc++; |
TETSUYA | 2:e331851128c1 | 434 | } |
TETSUYA | 2:e331851128c1 | 435 | break; |
TETSUYA | 2:e331851128c1 | 436 | } |
TETSUYA | 2:e331851128c1 | 437 | |
TETSUYA | 2:e331851128c1 | 438 | Draw_Dot(x,y,cl); |
TETSUYA | 2:e331851128c1 | 439 | y += sy; |
TETSUYA | 2:e331851128c1 | 440 | E += 2 * dx; |
TETSUYA | 2:e331851128c1 | 441 | if( E >= 0 ) { |
TETSUYA | 2:e331851128c1 | 442 | x += sx; |
TETSUYA | 2:e331851128c1 | 443 | E -= 2 * dy; |
TETSUYA | 2:e331851128c1 | 444 | } |
TETSUYA | 2:e331851128c1 | 445 | } |
TETSUYA | 2:e331851128c1 | 446 | } |
TETSUYA | 2:e331851128c1 | 447 | } |
TETSUYA | 2:e331851128c1 | 448 | |
TETSUYA | 3:eab8d193745a | 449 | // ミッチェナー(Miechener) の円描画 参考:http://dencha.ojaru.jp/programs_07/pg_graphic_09a1.html |
TETSUYA | 3:eab8d193745a | 450 | // x,y : 中心点 |
TETSUYA | 3:eab8d193745a | 451 | // radius: 半径 |
TETSUYA | 2:e331851128c1 | 452 | void SPIOLED96x64::circle (int radius, int x, int y , unsigned int col, int fill) |
TETSUYA | 2:e331851128c1 | 453 | { |
TETSUYA | 2:e331851128c1 | 454 | int cx, cy, d; |
TETSUYA | 2:e331851128c1 | 455 | |
TETSUYA | 2:e331851128c1 | 456 | d = 3 - 2 * radius; |
TETSUYA | 2:e331851128c1 | 457 | cy = radius; |
TETSUYA | 2:e331851128c1 | 458 | |
TETSUYA | 3:eab8d193745a | 459 | // 初期座標をあらかじめ描画する |
TETSUYA | 3:eab8d193745a | 460 | Draw_Dot( x, radius+y, col ); // 座標 ( 0, R ); |
TETSUYA | 3:eab8d193745a | 461 | Draw_Dot( x, -radius+y, col ); // 座標 ( 0, -R); |
TETSUYA | 3:eab8d193745a | 462 | Draw_Dot( radius+x, y, col ); // 座標 ( R, 0 ); |
TETSUYA | 3:eab8d193745a | 463 | Draw_Dot( -radius+x, y, col ); // 座標 (-R, 0); |
TETSUYA | 2:e331851128c1 | 464 | if ( fill ) { |
TETSUYA | 2:e331851128c1 | 465 | line( x, radius+y, x, -radius+y, col ,0 ); |
TETSUYA | 2:e331851128c1 | 466 | line( radius+x, y, -radius+x, y, col ,0 ); |
TETSUYA | 2:e331851128c1 | 467 | } |
TETSUYA | 2:e331851128c1 | 468 | |
TETSUYA | 2:e331851128c1 | 469 | for (cx = 0; cx <= cy; cx++){ |
TETSUYA | 2:e331851128c1 | 470 | if (d >= 0) { |
TETSUYA | 2:e331851128c1 | 471 | d += 10 + 4 * cx - 4 * cy; |
TETSUYA | 2:e331851128c1 | 472 | cy--; |
TETSUYA | 2:e331851128c1 | 473 | } else { |
TETSUYA | 2:e331851128c1 | 474 | d += 6 + 4 * cx; |
TETSUYA | 2:e331851128c1 | 475 | } |
TETSUYA | 2:e331851128c1 | 476 | |
TETSUYA | 3:eab8d193745a | 477 | // 描画 |
TETSUYA | 3:eab8d193745a | 478 | Draw_Dot( cy + x, cx + y, col); // 0-45 度の間 |
TETSUYA | 3:eab8d193745a | 479 | Draw_Dot( cx + x, cy + y, col); // 45-90 度の間 |
TETSUYA | 3:eab8d193745a | 480 | Draw_Dot( -cx + x, cy + y, col); // 90-135 度の間 |
TETSUYA | 3:eab8d193745a | 481 | Draw_Dot( -cy + x, cx + y, col); // 135-180 度の間 |
TETSUYA | 2:e331851128c1 | 482 | |
TETSUYA | 3:eab8d193745a | 483 | Draw_Dot( -cy + x, -cx + y, col); // 180-225 度の間 |
TETSUYA | 3:eab8d193745a | 484 | Draw_Dot( -cx + x, -cy + y, col); // 225-270 度の間 |
TETSUYA | 3:eab8d193745a | 485 | Draw_Dot( cx + x, -cy + y, col); // 270-315 度の間 |
TETSUYA | 3:eab8d193745a | 486 | Draw_Dot( cy + x, -cx + y, col); // 315-360 度の間 |
TETSUYA | 2:e331851128c1 | 487 | |
TETSUYA | 2:e331851128c1 | 488 | if ( fill ) { |
TETSUYA | 2:e331851128c1 | 489 | line( cy + x, cx + y, cy + x, -cx + y, col ,0 ); |
TETSUYA | 3:eab8d193745a | 490 | line( cx + x, cy + y, cx + x, -cy + y, col ,0 ); // 45-90 度の間 |
TETSUYA | 3:eab8d193745a | 491 | line( -cx + x, cy + y, -cx + x, cy + y, col ,0 ) ; // 90-135 度の間 |
TETSUYA | 3:eab8d193745a | 492 | line( -cy + x, cx + y, -cy + x, cx + y, col ,0 ); // 135-180 度の間 |
TETSUYA | 2:e331851128c1 | 493 | |
TETSUYA | 3:eab8d193745a | 494 | line( -cy + x, -cx + y, -cy + x, cx + y, col ,0 ); // 180-225 度の間 |
TETSUYA | 3:eab8d193745a | 495 | line( -cx + x, -cy + y, -cx + x, cy + y, col ,0 ); // 225-270 度の間 |
TETSUYA | 3:eab8d193745a | 496 | line( cx + x, -cy + y, cx + x, cy + y, col ,0 ); // 270-315 度の間 |
TETSUYA | 3:eab8d193745a | 497 | line( cy + x, -cx + y, cy + x, cx + y, col ,0 ); // 315-360 度の間 |
TETSUYA | 2:e331851128c1 | 498 | } |
TETSUYA | 2:e331851128c1 | 499 | |
TETSUYA | 2:e331851128c1 | 500 | } |
TETSUYA | 2:e331851128c1 | 501 | } |
TETSUYA | 2:e331851128c1 | 502 | |
TETSUYA | 2:e331851128c1 | 503 | // Color Pattern |
TETSUYA | 3:eab8d193745a | 504 | // pattern *P はRGB565(2バイト型)のデータ配列で渡す |
TETSUYA | 2:e331851128c1 | 505 | void SPIOLED96x64::Pattern(int x,int y,int width, int height, unsigned int *p) |
TETSUYA | 2:e331851128c1 | 506 | { |
TETSUYA | 2:e331851128c1 | 507 | |
TETSUYA | 2:e331851128c1 | 508 | int i,j; |
TETSUYA | 2:e331851128c1 | 509 | |
TETSUYA | 2:e331851128c1 | 510 | for(i=0; i<width; i++){ |
TETSUYA | 2:e331851128c1 | 511 | for(j=height-1;j>=0;j--){ |
TETSUYA | 2:e331851128c1 | 512 | Draw_Dot(x+i,y+j,*p++); |
TETSUYA | 2:e331851128c1 | 513 | } |
TETSUYA | 2:e331851128c1 | 514 | } |
TETSUYA | 2:e331851128c1 | 515 | |
TETSUYA | 2:e331851128c1 | 516 | } |
TETSUYA | 2:e331851128c1 | 517 | |
TETSUYA | 2:e331851128c1 | 518 | |
TETSUYA | 2:e331851128c1 | 519 |