fixed library.
Dependents: Sharp_Memory_LCD_Test hello_GT20L16J1Y_FONT_mlcd
Fork of sharp_mlcd by
sharp_mlcd.cpp@3:d2aed1df064c, 2014-09-21 (annotated)
- Committer:
- ban4jp
- Date:
- Sun Sep 21 08:05:33 2014 +0000
- Revision:
- 3:d2aed1df064c
- Parent:
- 2:c36cdd3ad7ba
Fix multi connection spi device issue.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masato | 0:c99dda90f834 | 1 | /* |
masato | 0:c99dda90f834 | 2 | * mbed library for the Sharp memory LCD LS027BDH01 / LS013B4DN04 |
masato | 0:c99dda90f834 | 3 | * derived from C12832_lcd |
masato | 0:c99dda90f834 | 4 | * Copyright (c) 2014 Masato YAMANISHI |
masato | 0:c99dda90f834 | 5 | * Released under the MIT License: http://mbed.org/license/mit |
masato | 0:c99dda90f834 | 6 | */ |
masato | 0:c99dda90f834 | 7 | |
masato | 0:c99dda90f834 | 8 | /* mbed library for the mbed Lab Board 128*32 pixel LCD |
masato | 0:c99dda90f834 | 9 | * use C12832 controller |
masato | 0:c99dda90f834 | 10 | * Copyright (c) 2012 Peter Drescher - DC2PD |
masato | 0:c99dda90f834 | 11 | * Released under the MIT License: http://mbed.org/license/mit |
masato | 0:c99dda90f834 | 12 | * |
masato | 0:c99dda90f834 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
masato | 0:c99dda90f834 | 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
masato | 0:c99dda90f834 | 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
masato | 0:c99dda90f834 | 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
masato | 0:c99dda90f834 | 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
masato | 0:c99dda90f834 | 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
masato | 0:c99dda90f834 | 19 | * THE SOFTWARE. |
masato | 0:c99dda90f834 | 20 | */ |
masato | 0:c99dda90f834 | 21 | |
masato | 0:c99dda90f834 | 22 | // 13.10.12 initial design |
masato | 0:c99dda90f834 | 23 | // 25.10.12 add autorefresh of screen |
masato | 0:c99dda90f834 | 24 | // 25.10.12 add standart font |
masato | 0:c99dda90f834 | 25 | // 20.12.12 add bitmap graphics |
masato | 0:c99dda90f834 | 26 | |
masato | 0:c99dda90f834 | 27 | // optional defines : |
masato | 0:c99dda90f834 | 28 | // #define debug_lcd 1 |
masato | 0:c99dda90f834 | 29 | |
masato | 0:c99dda90f834 | 30 | // #include "mbed.h" |
masato | 0:c99dda90f834 | 31 | #include "sharp_mlcd.h" |
masato | 0:c99dda90f834 | 32 | #include "stdio.h" |
masato | 0:c99dda90f834 | 33 | #include "Small_7.h" |
masato | 0:c99dda90f834 | 34 | |
masato | 0:c99dda90f834 | 35 | sharp_mlcd::sharp_mlcd(const char* name) |
ban4jp | 2:c36cdd3ad7ba | 36 | : GraphicsDisplay(name), _spi(D11, NC, D13), _EXTCOM(D9), _DISP(D8), _CS(D10) // for nucleo L152RE or other |
masato | 0:c99dda90f834 | 37 | { |
ban4jp | 2:c36cdd3ad7ba | 38 | lcd_reset(); |
ban4jp | 2:c36cdd3ad7ba | 39 | } |
ban4jp | 2:c36cdd3ad7ba | 40 | |
ban4jp | 2:c36cdd3ad7ba | 41 | sharp_mlcd::sharp_mlcd(PinName mosi, PinName sclk, PinName scs, PinName extcomin, PinName disp, const char* name) |
ban4jp | 2:c36cdd3ad7ba | 42 | : GraphicsDisplay(name), _spi(mosi, NC, sclk), _EXTCOM(extcomin), _DISP(disp), _CS(scs) |
ban4jp | 2:c36cdd3ad7ba | 43 | { |
masato | 0:c99dda90f834 | 44 | lcd_reset(); |
masato | 0:c99dda90f834 | 45 | } |
masato | 0:c99dda90f834 | 46 | |
masato | 0:c99dda90f834 | 47 | int sharp_mlcd::width() |
masato | 0:c99dda90f834 | 48 | { |
masato | 0:c99dda90f834 | 49 | return WIDTH; |
masato | 0:c99dda90f834 | 50 | } |
masato | 0:c99dda90f834 | 51 | |
masato | 0:c99dda90f834 | 52 | int sharp_mlcd::height() |
masato | 0:c99dda90f834 | 53 | { |
masato | 0:c99dda90f834 | 54 | return HEIGHT; // 32; |
masato | 0:c99dda90f834 | 55 | } |
masato | 0:c99dda90f834 | 56 | |
masato | 0:c99dda90f834 | 57 | // write command to lcd controller |
masato | 0:c99dda90f834 | 58 | |
masato | 0:c99dda90f834 | 59 | |
masato | 0:c99dda90f834 | 60 | // reset and init the lcd controller |
masato | 0:c99dda90f834 | 61 | |
masato | 0:c99dda90f834 | 62 | void sharp_mlcd::lcd_reset() |
masato | 0:c99dda90f834 | 63 | { |
masato | 0:c99dda90f834 | 64 | _CS = 0; |
masato | 1:f8ba3b236d12 | 65 | _spi.format(8, 0); // 8 bit spi mode 0 |
masato | 0:c99dda90f834 | 66 | _spi.frequency(2000000); // 2 Mhz SPI clock |
masato | 0:c99dda90f834 | 67 | _DISP = 1; |
masato | 0:c99dda90f834 | 68 | flip = 0; |
masato | 1:f8ba3b236d12 | 69 | set_reverse_mode(0); |
masato | 0:c99dda90f834 | 70 | // timer.attach(attime, 0.5); // 1KHz |
masato | 0:c99dda90f834 | 71 | |
masato | 0:c99dda90f834 | 72 | _CS = 1; |
masato | 0:c99dda90f834 | 73 | wait_us(4); |
masato | 0:c99dda90f834 | 74 | _spi.write(0x20); // 0x20:clear 0x00:run |
masato | 0:c99dda90f834 | 75 | _spi.write(0); // dummy |
masato | 0:c99dda90f834 | 76 | wait_us(2); |
masato | 0:c99dda90f834 | 77 | _CS = 0; |
masato | 0:c99dda90f834 | 78 | |
ban4jp | 2:c36cdd3ad7ba | 79 | setmode(NORMAL); |
ban4jp | 2:c36cdd3ad7ba | 80 | |
masato | 0:c99dda90f834 | 81 | // clear and update LCD |
masato | 0:c99dda90f834 | 82 | cls(); |
masato | 0:c99dda90f834 | 83 | // memset(buffer,0xff,BUFFER_SIZE); // clear display buffer |
masato | 0:c99dda90f834 | 84 | // copy_to_lcd(); |
masato | 0:c99dda90f834 | 85 | auto_up = 1; // switch on auto update |
masato | 0:c99dda90f834 | 86 | // dont do this by default. Make the user call |
masato | 0:c99dda90f834 | 87 | //claim(stdout); // redirekt printf to lcd |
masato | 0:c99dda90f834 | 88 | locate(0,0); |
masato | 0:c99dda90f834 | 89 | set_font((unsigned char*)Small_7); // standart font |
masato | 0:c99dda90f834 | 90 | } |
masato | 0:c99dda90f834 | 91 | |
masato | 1:f8ba3b236d12 | 92 | void sharp_mlcd::set_reverse_mode(unsigned int r = 0) |
masato | 1:f8ba3b236d12 | 93 | { |
masato | 1:f8ba3b236d12 | 94 | reverse = r; |
masato | 1:f8ba3b236d12 | 95 | } |
masato | 1:f8ba3b236d12 | 96 | |
masato | 0:c99dda90f834 | 97 | // set one pixel in buffer |
masato | 0:c99dda90f834 | 98 | |
masato | 0:c99dda90f834 | 99 | void sharp_mlcd::pixel(int x, int y, int color) |
masato | 0:c99dda90f834 | 100 | { |
masato | 0:c99dda90f834 | 101 | // first check parameter |
ban4jp | 2:c36cdd3ad7ba | 102 | if(x >= width() || y >= height() || x < 0 || y < 0) return; |
masato | 0:c99dda90f834 | 103 | |
masato | 0:c99dda90f834 | 104 | if(draw_mode == NORMAL) { |
masato | 1:f8ba3b236d12 | 105 | if(color != reverse) |
ban4jp | 2:c36cdd3ad7ba | 106 | buffer[x/8 + (y*width()/8)] &= ~(1 << (7-(x%8))); // set pixel |
masato | 0:c99dda90f834 | 107 | else |
ban4jp | 2:c36cdd3ad7ba | 108 | buffer[x/8 + (y*width()/8)] |= (1 << (7-(x%8))); // erase pixel |
masato | 0:c99dda90f834 | 109 | } else { // XOR mode |
masato | 1:f8ba3b236d12 | 110 | if(color != reverse) |
ban4jp | 2:c36cdd3ad7ba | 111 | buffer[x/8 + (y*width()/8)] ^= (1 << (7-(x%8))); // xor pixel |
masato | 0:c99dda90f834 | 112 | } |
masato | 0:c99dda90f834 | 113 | } |
masato | 0:c99dda90f834 | 114 | |
masato | 0:c99dda90f834 | 115 | // update lcd |
masato | 0:c99dda90f834 | 116 | |
masato | 0:c99dda90f834 | 117 | void sharp_mlcd::attime() { |
masato | 0:c99dda90f834 | 118 | flip = !flip; |
masato | 0:c99dda90f834 | 119 | _EXTCOM = flip; |
masato | 0:c99dda90f834 | 120 | } |
masato | 0:c99dda90f834 | 121 | |
masato | 0:c99dda90f834 | 122 | unsigned char sharp_mlcd::bit_reverse(unsigned char c) { |
masato | 0:c99dda90f834 | 123 | unsigned char u=0; // c = TESTVALUE; |
masato | 0:c99dda90f834 | 124 | int i; |
masato | 0:c99dda90f834 | 125 | |
masato | 0:c99dda90f834 | 126 | for (i=0 ; i<4 ; i++) |
masato | 0:c99dda90f834 | 127 | u |= ((c & (1 << i)) << (7-2*i)) | ((c & (1 << 7-i)) >> (7-2*i)); |
masato | 0:c99dda90f834 | 128 | return u; |
masato | 0:c99dda90f834 | 129 | } |
masato | 0:c99dda90f834 | 130 | |
ban4jp | 2:c36cdd3ad7ba | 131 | void sharp_mlcd::copy_to_lcd(int start) |
masato | 0:c99dda90f834 | 132 | { |
masato | 0:c99dda90f834 | 133 | int x, y; |
masato | 0:c99dda90f834 | 134 | unsigned char *p = (unsigned char*)buffer; // image; |
masato | 0:c99dda90f834 | 135 | |
ban4jp | 3:d2aed1df064c | 136 | //_spi.begin(); |
ban4jp | 3:d2aed1df064c | 137 | _spi.write(0); // Dummy (mbed: change SPI owner and SPI format.) |
ban4jp | 3:d2aed1df064c | 138 | |
masato | 0:c99dda90f834 | 139 | _CS = 1; |
masato | 0:c99dda90f834 | 140 | wait_us(4); // min 3us |
masato | 0:c99dda90f834 | 141 | // |
ban4jp | 2:c36cdd3ad7ba | 142 | for (y = 0; y < height(); y++) { |
masato | 0:c99dda90f834 | 143 | _spi.write(0x80); // update mode (multi line) |
ban4jp | 2:c36cdd3ad7ba | 144 | _spi.write(bit_reverse((y + start) % height() + 1)); // set gate line address |
ban4jp | 2:c36cdd3ad7ba | 145 | for (x = 0; x < (width() / 8); x++) { |
masato | 0:c99dda90f834 | 146 | _spi.write(*p++); // (((y / 8) + x + mode) % 2)? 0xff: 0); |
masato | 0:c99dda90f834 | 147 | } |
masato | 0:c99dda90f834 | 148 | } |
ban4jp | 2:c36cdd3ad7ba | 149 | _spi.write(0x00); // dummy (16ck) |
ban4jp | 2:c36cdd3ad7ba | 150 | _spi.write(0x00); |
masato | 0:c99dda90f834 | 151 | // |
masato | 0:c99dda90f834 | 152 | wait_us(2); // min 1us |
masato | 0:c99dda90f834 | 153 | _CS = 0; |
masato | 0:c99dda90f834 | 154 | } |
masato | 0:c99dda90f834 | 155 | |
masato | 0:c99dda90f834 | 156 | void sharp_mlcd::cls(void) |
masato | 0:c99dda90f834 | 157 | { |
ban4jp | 2:c36cdd3ad7ba | 158 | memset(buffer, reverse ? 0x00 : 0xff, BUFFER_SIZE); // clear display buffer |
masato | 0:c99dda90f834 | 159 | copy_to_lcd(); |
masato | 0:c99dda90f834 | 160 | } |
masato | 0:c99dda90f834 | 161 | |
masato | 0:c99dda90f834 | 162 | |
masato | 0:c99dda90f834 | 163 | void sharp_mlcd::line(int x0, int y0, int x1, int y1, int color) |
masato | 0:c99dda90f834 | 164 | { |
masato | 0:c99dda90f834 | 165 | int dx = 0, dy = 0; |
masato | 0:c99dda90f834 | 166 | int dx_sym = 0, dy_sym = 0; |
masato | 0:c99dda90f834 | 167 | int dx_x2 = 0, dy_x2 = 0; |
masato | 0:c99dda90f834 | 168 | int di = 0; |
masato | 0:c99dda90f834 | 169 | |
masato | 0:c99dda90f834 | 170 | dx = x1-x0; |
masato | 0:c99dda90f834 | 171 | dy = y1-y0; |
masato | 0:c99dda90f834 | 172 | |
masato | 0:c99dda90f834 | 173 | // if (dx == 0) { /* vertical line */ |
masato | 0:c99dda90f834 | 174 | // if (y1 > y0) vline(x0,y0,y1,color); |
masato | 0:c99dda90f834 | 175 | // else vline(x0,y1,y0,color); |
masato | 0:c99dda90f834 | 176 | // return; |
masato | 0:c99dda90f834 | 177 | // } |
masato | 0:c99dda90f834 | 178 | |
masato | 0:c99dda90f834 | 179 | if (dx > 0) { |
masato | 0:c99dda90f834 | 180 | dx_sym = 1; |
masato | 0:c99dda90f834 | 181 | } else { |
masato | 0:c99dda90f834 | 182 | dx_sym = -1; |
masato | 0:c99dda90f834 | 183 | } |
masato | 0:c99dda90f834 | 184 | // if (dy == 0) { /* horizontal line */ |
masato | 0:c99dda90f834 | 185 | // if (x1 > x0) hline(x0,x1,y0,color); |
masato | 0:c99dda90f834 | 186 | // else hline(x1,x0,y0,color); |
masato | 0:c99dda90f834 | 187 | // return; |
masato | 0:c99dda90f834 | 188 | // } |
masato | 0:c99dda90f834 | 189 | |
masato | 0:c99dda90f834 | 190 | if (dy > 0) { |
masato | 0:c99dda90f834 | 191 | dy_sym = 1; |
masato | 0:c99dda90f834 | 192 | } else { |
masato | 0:c99dda90f834 | 193 | dy_sym = -1; |
masato | 0:c99dda90f834 | 194 | } |
masato | 0:c99dda90f834 | 195 | |
masato | 0:c99dda90f834 | 196 | dx = dx_sym*dx; |
masato | 0:c99dda90f834 | 197 | dy = dy_sym*dy; |
masato | 0:c99dda90f834 | 198 | |
masato | 0:c99dda90f834 | 199 | dx_x2 = dx*2; |
masato | 0:c99dda90f834 | 200 | dy_x2 = dy*2; |
masato | 0:c99dda90f834 | 201 | |
masato | 0:c99dda90f834 | 202 | if (dx >= dy) { |
masato | 0:c99dda90f834 | 203 | di = dy_x2 - dx; |
masato | 0:c99dda90f834 | 204 | while (x0 != x1) { |
masato | 0:c99dda90f834 | 205 | |
masato | 0:c99dda90f834 | 206 | pixel(x0, y0, color); |
masato | 0:c99dda90f834 | 207 | x0 += dx_sym; |
masato | 0:c99dda90f834 | 208 | if (di<0) { |
masato | 0:c99dda90f834 | 209 | di += dy_x2; |
masato | 0:c99dda90f834 | 210 | } else { |
masato | 0:c99dda90f834 | 211 | di += dy_x2 - dx_x2; |
masato | 0:c99dda90f834 | 212 | y0 += dy_sym; |
masato | 0:c99dda90f834 | 213 | } |
masato | 0:c99dda90f834 | 214 | } |
masato | 0:c99dda90f834 | 215 | pixel(x0, y0, color); |
masato | 0:c99dda90f834 | 216 | } else { |
masato | 0:c99dda90f834 | 217 | di = dx_x2 - dy; |
masato | 0:c99dda90f834 | 218 | while (y0 != y1) { |
masato | 0:c99dda90f834 | 219 | pixel(x0, y0, color); |
masato | 0:c99dda90f834 | 220 | y0 += dy_sym; |
masato | 0:c99dda90f834 | 221 | if (di < 0) { |
masato | 0:c99dda90f834 | 222 | di += dx_x2; |
masato | 0:c99dda90f834 | 223 | } else { |
masato | 0:c99dda90f834 | 224 | di += dx_x2 - dy_x2; |
masato | 0:c99dda90f834 | 225 | x0 += dx_sym; |
masato | 0:c99dda90f834 | 226 | } |
masato | 0:c99dda90f834 | 227 | } |
masato | 0:c99dda90f834 | 228 | pixel(x0, y0, color); |
masato | 0:c99dda90f834 | 229 | } |
masato | 0:c99dda90f834 | 230 | if(auto_up) copy_to_lcd(); |
masato | 0:c99dda90f834 | 231 | } |
masato | 0:c99dda90f834 | 232 | |
masato | 0:c99dda90f834 | 233 | void sharp_mlcd::rect(int x0, int y0, int x1, int y1, int color) |
masato | 0:c99dda90f834 | 234 | { |
masato | 0:c99dda90f834 | 235 | |
masato | 0:c99dda90f834 | 236 | if (x1 > x0) line(x0,y0,x1,y0,color); |
masato | 0:c99dda90f834 | 237 | else line(x1,y0,x0,y0,color); |
masato | 0:c99dda90f834 | 238 | |
masato | 0:c99dda90f834 | 239 | if (y1 > y0) line(x0,y0,x0,y1,color); |
masato | 0:c99dda90f834 | 240 | else line(x0,y1,x0,y0,color); |
masato | 0:c99dda90f834 | 241 | |
masato | 0:c99dda90f834 | 242 | if (x1 > x0) line(x0,y1,x1,y1,color); |
masato | 0:c99dda90f834 | 243 | else line(x1,y1,x0,y1,color); |
masato | 0:c99dda90f834 | 244 | |
masato | 0:c99dda90f834 | 245 | if (y1 > y0) line(x1,y0,x1,y1,color); |
masato | 0:c99dda90f834 | 246 | else line(x1,y1,x1,y0,color); |
masato | 0:c99dda90f834 | 247 | |
masato | 0:c99dda90f834 | 248 | if(auto_up) copy_to_lcd(); |
masato | 0:c99dda90f834 | 249 | } |
masato | 0:c99dda90f834 | 250 | |
masato | 0:c99dda90f834 | 251 | void sharp_mlcd::fillrect(int x0, int y0, int x1, int y1, int color) |
masato | 0:c99dda90f834 | 252 | { |
masato | 0:c99dda90f834 | 253 | int l,c,i; |
masato | 0:c99dda90f834 | 254 | if(x0 > x1) { |
masato | 0:c99dda90f834 | 255 | i = x0; |
masato | 0:c99dda90f834 | 256 | x0 = x1; |
masato | 0:c99dda90f834 | 257 | x1 = i; |
masato | 0:c99dda90f834 | 258 | } |
masato | 0:c99dda90f834 | 259 | |
masato | 0:c99dda90f834 | 260 | if(y0 > y1) { |
masato | 0:c99dda90f834 | 261 | i = y0; |
masato | 0:c99dda90f834 | 262 | y0 = y1; |
masato | 0:c99dda90f834 | 263 | y1 = i; |
masato | 0:c99dda90f834 | 264 | } |
masato | 0:c99dda90f834 | 265 | |
masato | 0:c99dda90f834 | 266 | for(l = x0; l<= x1; l ++) { |
masato | 0:c99dda90f834 | 267 | for(c = y0; c<= y1; c++) { |
masato | 0:c99dda90f834 | 268 | pixel(l,c,color); |
masato | 0:c99dda90f834 | 269 | } |
masato | 0:c99dda90f834 | 270 | } |
masato | 0:c99dda90f834 | 271 | if(auto_up) copy_to_lcd(); |
masato | 0:c99dda90f834 | 272 | } |
masato | 0:c99dda90f834 | 273 | |
masato | 0:c99dda90f834 | 274 | |
masato | 0:c99dda90f834 | 275 | |
masato | 0:c99dda90f834 | 276 | void sharp_mlcd::circle(int x0, int y0, int r, int color) |
masato | 0:c99dda90f834 | 277 | { |
masato | 0:c99dda90f834 | 278 | |
masato | 0:c99dda90f834 | 279 | int draw_x0, draw_y0; |
masato | 0:c99dda90f834 | 280 | int draw_x1, draw_y1; |
masato | 0:c99dda90f834 | 281 | int draw_x2, draw_y2; |
masato | 0:c99dda90f834 | 282 | int draw_x3, draw_y3; |
masato | 0:c99dda90f834 | 283 | int draw_x4, draw_y4; |
masato | 0:c99dda90f834 | 284 | int draw_x5, draw_y5; |
masato | 0:c99dda90f834 | 285 | int draw_x6, draw_y6; |
masato | 0:c99dda90f834 | 286 | int draw_x7, draw_y7; |
masato | 0:c99dda90f834 | 287 | int xx, yy; |
masato | 0:c99dda90f834 | 288 | int di; |
masato | 0:c99dda90f834 | 289 | //WindowMax(); |
masato | 0:c99dda90f834 | 290 | if (r == 0) { /* no radius */ |
masato | 0:c99dda90f834 | 291 | return; |
masato | 0:c99dda90f834 | 292 | } |
masato | 0:c99dda90f834 | 293 | |
masato | 0:c99dda90f834 | 294 | draw_x0 = draw_x1 = x0; |
masato | 0:c99dda90f834 | 295 | draw_y0 = draw_y1 = y0 + r; |
masato | 0:c99dda90f834 | 296 | if (draw_y0 < height()) { |
masato | 0:c99dda90f834 | 297 | pixel(draw_x0, draw_y0, color); /* 90 degree */ |
masato | 0:c99dda90f834 | 298 | } |
masato | 0:c99dda90f834 | 299 | |
masato | 0:c99dda90f834 | 300 | draw_x2 = draw_x3 = x0; |
masato | 0:c99dda90f834 | 301 | draw_y2 = draw_y3 = y0 - r; |
masato | 0:c99dda90f834 | 302 | if (draw_y2 >= 0) { |
masato | 0:c99dda90f834 | 303 | pixel(draw_x2, draw_y2, color); /* 270 degree */ |
masato | 0:c99dda90f834 | 304 | } |
masato | 0:c99dda90f834 | 305 | |
masato | 0:c99dda90f834 | 306 | draw_x4 = draw_x6 = x0 + r; |
masato | 0:c99dda90f834 | 307 | draw_y4 = draw_y6 = y0; |
masato | 0:c99dda90f834 | 308 | if (draw_x4 < width()) { |
masato | 0:c99dda90f834 | 309 | pixel(draw_x4, draw_y4, color); /* 0 degree */ |
masato | 0:c99dda90f834 | 310 | } |
masato | 0:c99dda90f834 | 311 | |
masato | 0:c99dda90f834 | 312 | draw_x5 = draw_x7 = x0 - r; |
masato | 0:c99dda90f834 | 313 | draw_y5 = draw_y7 = y0; |
ban4jp | 2:c36cdd3ad7ba | 314 | if (draw_x5 >= 0) { |
masato | 0:c99dda90f834 | 315 | pixel(draw_x5, draw_y5, color); /* 180 degree */ |
masato | 0:c99dda90f834 | 316 | } |
masato | 0:c99dda90f834 | 317 | |
masato | 0:c99dda90f834 | 318 | if (r == 1) { |
masato | 0:c99dda90f834 | 319 | return; |
masato | 0:c99dda90f834 | 320 | } |
masato | 0:c99dda90f834 | 321 | |
masato | 0:c99dda90f834 | 322 | di = 3 - 2*r; |
masato | 0:c99dda90f834 | 323 | xx = 0; |
masato | 0:c99dda90f834 | 324 | yy = r; |
masato | 0:c99dda90f834 | 325 | while (xx < yy) { |
masato | 0:c99dda90f834 | 326 | |
masato | 0:c99dda90f834 | 327 | if (di < 0) { |
masato | 0:c99dda90f834 | 328 | di += 4*xx + 6; |
masato | 0:c99dda90f834 | 329 | } else { |
masato | 0:c99dda90f834 | 330 | di += 4*(xx - yy) + 10; |
masato | 0:c99dda90f834 | 331 | yy--; |
masato | 0:c99dda90f834 | 332 | draw_y0--; |
masato | 0:c99dda90f834 | 333 | draw_y1--; |
masato | 0:c99dda90f834 | 334 | draw_y2++; |
masato | 0:c99dda90f834 | 335 | draw_y3++; |
masato | 0:c99dda90f834 | 336 | draw_x4--; |
masato | 0:c99dda90f834 | 337 | draw_x5++; |
masato | 0:c99dda90f834 | 338 | draw_x6--; |
masato | 0:c99dda90f834 | 339 | draw_x7++; |
masato | 0:c99dda90f834 | 340 | } |
masato | 0:c99dda90f834 | 341 | xx++; |
masato | 0:c99dda90f834 | 342 | draw_x0++; |
masato | 0:c99dda90f834 | 343 | draw_x1--; |
masato | 0:c99dda90f834 | 344 | draw_x2++; |
masato | 0:c99dda90f834 | 345 | draw_x3--; |
masato | 0:c99dda90f834 | 346 | draw_y4++; |
masato | 0:c99dda90f834 | 347 | draw_y5++; |
masato | 0:c99dda90f834 | 348 | draw_y6--; |
masato | 0:c99dda90f834 | 349 | draw_y7--; |
masato | 0:c99dda90f834 | 350 | |
ban4jp | 2:c36cdd3ad7ba | 351 | if ( (draw_x0 <= width()) && (draw_y0 >= 0) ) { |
masato | 0:c99dda90f834 | 352 | pixel(draw_x0, draw_y0, color); |
masato | 0:c99dda90f834 | 353 | } |
masato | 0:c99dda90f834 | 354 | |
masato | 0:c99dda90f834 | 355 | if ( (draw_x1 >= 0) && (draw_y1 >= 0) ) { |
masato | 0:c99dda90f834 | 356 | pixel(draw_x1, draw_y1, color); |
masato | 0:c99dda90f834 | 357 | } |
masato | 0:c99dda90f834 | 358 | |
masato | 0:c99dda90f834 | 359 | if ( (draw_x2 <= width()) && (draw_y2 <= height()) ) { |
masato | 0:c99dda90f834 | 360 | pixel(draw_x2, draw_y2, color); |
masato | 0:c99dda90f834 | 361 | } |
masato | 0:c99dda90f834 | 362 | |
ban4jp | 2:c36cdd3ad7ba | 363 | if ( (draw_x3 >= 0 ) && (draw_y3 <= height()) ) { |
masato | 0:c99dda90f834 | 364 | pixel(draw_x3, draw_y3, color); |
masato | 0:c99dda90f834 | 365 | } |
masato | 0:c99dda90f834 | 366 | |
masato | 0:c99dda90f834 | 367 | if ( (draw_x4 <= width()) && (draw_y4 >= 0) ) { |
masato | 0:c99dda90f834 | 368 | pixel(draw_x4, draw_y4, color); |
masato | 0:c99dda90f834 | 369 | } |
masato | 0:c99dda90f834 | 370 | |
masato | 0:c99dda90f834 | 371 | if ( (draw_x5 >= 0) && (draw_y5 >= 0) ) { |
masato | 0:c99dda90f834 | 372 | pixel(draw_x5, draw_y5, color); |
masato | 0:c99dda90f834 | 373 | } |
ban4jp | 2:c36cdd3ad7ba | 374 | if ( (draw_x6 <= width()) && (draw_y6 <= height()) ) { |
masato | 0:c99dda90f834 | 375 | pixel(draw_x6, draw_y6, color); |
masato | 0:c99dda90f834 | 376 | } |
masato | 0:c99dda90f834 | 377 | if ( (draw_x7 >= 0) && (draw_y7 <= height()) ) { |
masato | 0:c99dda90f834 | 378 | pixel(draw_x7, draw_y7, color); |
masato | 0:c99dda90f834 | 379 | } |
masato | 0:c99dda90f834 | 380 | } |
masato | 0:c99dda90f834 | 381 | if(auto_up) copy_to_lcd(); |
masato | 0:c99dda90f834 | 382 | } |
masato | 0:c99dda90f834 | 383 | |
masato | 0:c99dda90f834 | 384 | void sharp_mlcd::fillcircle(int x, int y, int r, int color) |
masato | 0:c99dda90f834 | 385 | { |
masato | 0:c99dda90f834 | 386 | int i,up; |
masato | 0:c99dda90f834 | 387 | up = auto_up; |
masato | 0:c99dda90f834 | 388 | auto_up = 0; // off |
masato | 0:c99dda90f834 | 389 | for (i = 0; i <= r; i++) |
masato | 0:c99dda90f834 | 390 | circle(x,y,i,color); |
masato | 0:c99dda90f834 | 391 | auto_up = up; |
masato | 0:c99dda90f834 | 392 | if(auto_up) copy_to_lcd(); |
masato | 0:c99dda90f834 | 393 | } |
masato | 0:c99dda90f834 | 394 | |
masato | 0:c99dda90f834 | 395 | void sharp_mlcd::setmode(int mode) |
masato | 0:c99dda90f834 | 396 | { |
masato | 0:c99dda90f834 | 397 | draw_mode = mode; |
masato | 0:c99dda90f834 | 398 | } |
masato | 0:c99dda90f834 | 399 | |
masato | 0:c99dda90f834 | 400 | void sharp_mlcd::locate(int x, int y) |
masato | 0:c99dda90f834 | 401 | { |
masato | 0:c99dda90f834 | 402 | char_x = x; |
masato | 0:c99dda90f834 | 403 | char_y = y; |
masato | 0:c99dda90f834 | 404 | } |
masato | 0:c99dda90f834 | 405 | |
masato | 0:c99dda90f834 | 406 | |
masato | 0:c99dda90f834 | 407 | |
masato | 0:c99dda90f834 | 408 | int sharp_mlcd::columns() |
masato | 0:c99dda90f834 | 409 | { |
masato | 0:c99dda90f834 | 410 | return width() / font[1]; |
masato | 0:c99dda90f834 | 411 | } |
masato | 0:c99dda90f834 | 412 | |
masato | 0:c99dda90f834 | 413 | |
masato | 0:c99dda90f834 | 414 | |
masato | 0:c99dda90f834 | 415 | int sharp_mlcd::rows() |
masato | 0:c99dda90f834 | 416 | { |
masato | 0:c99dda90f834 | 417 | return height() / font[2]; |
masato | 0:c99dda90f834 | 418 | } |
masato | 0:c99dda90f834 | 419 | |
masato | 0:c99dda90f834 | 420 | |
masato | 0:c99dda90f834 | 421 | |
masato | 0:c99dda90f834 | 422 | int sharp_mlcd::_putc(int value) |
masato | 0:c99dda90f834 | 423 | { |
masato | 0:c99dda90f834 | 424 | if (value == '\n') { // new line |
masato | 0:c99dda90f834 | 425 | char_x = 0; |
masato | 0:c99dda90f834 | 426 | char_y = char_y + font[2]; |
ban4jp | 2:c36cdd3ad7ba | 427 | if (char_y > height() - font[2]) { |
masato | 0:c99dda90f834 | 428 | char_y = 0; |
masato | 0:c99dda90f834 | 429 | } |
masato | 0:c99dda90f834 | 430 | } else { |
masato | 0:c99dda90f834 | 431 | character(char_x, char_y, value); |
masato | 0:c99dda90f834 | 432 | if(auto_up) copy_to_lcd(); |
masato | 0:c99dda90f834 | 433 | } |
masato | 0:c99dda90f834 | 434 | return value; |
masato | 0:c99dda90f834 | 435 | } |
masato | 0:c99dda90f834 | 436 | |
masato | 0:c99dda90f834 | 437 | void sharp_mlcd::character(int x, int y, int c) |
masato | 0:c99dda90f834 | 438 | { |
masato | 0:c99dda90f834 | 439 | unsigned int hor,vert,offset,bpl,j,i,b; |
masato | 0:c99dda90f834 | 440 | unsigned char* zeichen; |
masato | 0:c99dda90f834 | 441 | unsigned char z,w; |
masato | 0:c99dda90f834 | 442 | |
masato | 0:c99dda90f834 | 443 | if ((c < 31) || (c > 127)) return; // test char range |
masato | 0:c99dda90f834 | 444 | |
masato | 0:c99dda90f834 | 445 | // read font parameter from start of array |
masato | 0:c99dda90f834 | 446 | offset = font[0]; // bytes / char |
masato | 0:c99dda90f834 | 447 | hor = font[1]; // get hor size of font |
masato | 0:c99dda90f834 | 448 | vert = font[2]; // get vert size of font |
masato | 0:c99dda90f834 | 449 | bpl = font[3]; // bytes per line |
masato | 0:c99dda90f834 | 450 | |
ban4jp | 2:c36cdd3ad7ba | 451 | zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap |
ban4jp | 2:c36cdd3ad7ba | 452 | w = zeichen[0]; // width of actual char |
ban4jp | 2:c36cdd3ad7ba | 453 | |
ban4jp | 2:c36cdd3ad7ba | 454 | if (char_x > width() - w) { |
ban4jp | 2:c36cdd3ad7ba | 455 | char_x = w; |
ban4jp | 2:c36cdd3ad7ba | 456 | char_y += vert; |
ban4jp | 2:c36cdd3ad7ba | 457 | if (char_y > height() - vert) { |
masato | 0:c99dda90f834 | 458 | char_y = 0; |
masato | 0:c99dda90f834 | 459 | } |
ban4jp | 2:c36cdd3ad7ba | 460 | |
ban4jp | 2:c36cdd3ad7ba | 461 | x = 0; |
ban4jp | 2:c36cdd3ad7ba | 462 | y = char_y; |
ban4jp | 2:c36cdd3ad7ba | 463 | |
ban4jp | 2:c36cdd3ad7ba | 464 | } else { |
ban4jp | 2:c36cdd3ad7ba | 465 | char_x += w; |
masato | 0:c99dda90f834 | 466 | } |
masato | 0:c99dda90f834 | 467 | |
masato | 0:c99dda90f834 | 468 | // construct the char into the buffer |
masato | 0:c99dda90f834 | 469 | for (j=0; j<vert; j++) { // vert line |
masato | 0:c99dda90f834 | 470 | for (i=0; i<hor; i++) { // horz line |
ban4jp | 2:c36cdd3ad7ba | 471 | z = zeichen[bpl * i + ((j & 0xFFF8) >> 3)+1]; |
masato | 0:c99dda90f834 | 472 | b = 1 << (j & 0x07); |
masato | 0:c99dda90f834 | 473 | if (( z & b ) == 0x00) { |
masato | 0:c99dda90f834 | 474 | pixel(x+i,y+j,0); |
masato | 0:c99dda90f834 | 475 | } else { |
masato | 0:c99dda90f834 | 476 | pixel(x+i,y+j,1); |
masato | 0:c99dda90f834 | 477 | } |
masato | 0:c99dda90f834 | 478 | |
masato | 0:c99dda90f834 | 479 | } |
masato | 0:c99dda90f834 | 480 | } |
masato | 0:c99dda90f834 | 481 | } |
masato | 0:c99dda90f834 | 482 | |
masato | 0:c99dda90f834 | 483 | void sharp_mlcd::set_font(unsigned char* f) |
masato | 0:c99dda90f834 | 484 | { |
masato | 0:c99dda90f834 | 485 | font = f; |
masato | 0:c99dda90f834 | 486 | } |
masato | 0:c99dda90f834 | 487 | |
masato | 0:c99dda90f834 | 488 | void sharp_mlcd::set_auto_up(unsigned int up) |
masato | 0:c99dda90f834 | 489 | { |
ban4jp | 2:c36cdd3ad7ba | 490 | if (up) { |
ban4jp | 2:c36cdd3ad7ba | 491 | auto_up = 1; |
ban4jp | 2:c36cdd3ad7ba | 492 | } else { |
ban4jp | 2:c36cdd3ad7ba | 493 | auto_up = 0; |
ban4jp | 2:c36cdd3ad7ba | 494 | } |
masato | 0:c99dda90f834 | 495 | } |
masato | 0:c99dda90f834 | 496 | |
masato | 0:c99dda90f834 | 497 | unsigned int sharp_mlcd::get_auto_up(void) |
masato | 0:c99dda90f834 | 498 | { |
masato | 0:c99dda90f834 | 499 | return (auto_up); |
masato | 0:c99dda90f834 | 500 | } |
masato | 0:c99dda90f834 | 501 | |
masato | 1:f8ba3b236d12 | 502 | void sharp_mlcd::print_bm(Bitmap bm, int x, int y, int color = 1) |
masato | 0:c99dda90f834 | 503 | { |
masato | 0:c99dda90f834 | 504 | int h,v,b; |
masato | 0:c99dda90f834 | 505 | char d; |
masato | 0:c99dda90f834 | 506 | |
masato | 0:c99dda90f834 | 507 | for(v=0; v < bm.ySize; v++) { // lines |
masato | 0:c99dda90f834 | 508 | for(h=0; h < bm.xSize; h++) { // pixel |
ban4jp | 2:c36cdd3ad7ba | 509 | if(h + x >= width()) break; |
ban4jp | 2:c36cdd3ad7ba | 510 | if(v + y >= height()) break; |
ban4jp | 2:c36cdd3ad7ba | 511 | d = bm.data[bm.Byte_in_Line * v + ((h & 0xFFF8) >> 3)]; |
masato | 0:c99dda90f834 | 512 | b = 0x80 >> (h & 0x07); |
masato | 0:c99dda90f834 | 513 | if((d & b) == 0) { |
masato | 1:f8ba3b236d12 | 514 | pixel(x+h,y+v,!color); |
masato | 0:c99dda90f834 | 515 | } else { |
masato | 1:f8ba3b236d12 | 516 | pixel(x+h,y+v,color); |
masato | 0:c99dda90f834 | 517 | } |
masato | 0:c99dda90f834 | 518 | } |
masato | 0:c99dda90f834 | 519 | } |
masato | 0:c99dda90f834 | 520 | } |
masato | 0:c99dda90f834 | 521 |