paul@0 | 1 | /* |
paul@0 | 2 | * Ben NanoNote communication with the Nu Electronics "Color LCD & |
paul@0 | 3 | * Joystick Shield" featuring a Nokia 6110 and the PCF8833 display |
paul@0 | 4 | * controller. |
paul@0 | 5 | * |
paul@0 | 6 | * http://shieldlist.org/nuelectronics/colorlcd-joystick |
paul@0 | 7 | * |
paul@0 | 8 | * Copyright (C) 2013 Paul Boddie |
paul@0 | 9 | * |
paul@0 | 10 | * This program is free software; you can redistribute it and/or modify |
paul@0 | 11 | * it under the terms of the GNU General Public License as published by |
paul@0 | 12 | * the Free Software Foundation; either version 2 of the License, or |
paul@0 | 13 | * (at your option) any later version. |
paul@0 | 14 | */ |
paul@0 | 15 | |
paul@0 | 16 | #include "pcf8833.h" |
paul@0 | 17 | #include <stdio.h> |
paul@0 | 18 | #include <unistd.h> |
paul@0 | 19 | |
paul@0 | 20 | void LCD_send(lcd_sendmode mode, uint8_t data) |
paul@0 | 21 | { |
paul@0 | 22 | spi_begin(); |
paul@0 | 23 | spi_send(data, mode); |
paul@0 | 24 | spi_end(); |
paul@0 | 25 | } |
paul@0 | 26 | |
paul@0 | 27 | void LCD_send_more_data(uint8_t data) |
paul@0 | 28 | { |
paul@0 | 29 | spi_begin(); |
paul@0 | 30 | spi_send(data, LCD_DATA); |
paul@0 | 31 | spi_end(); |
paul@0 | 32 | } |
paul@0 | 33 | |
paul@0 | 34 | void spi_init() |
paul@0 | 35 | { |
paul@0 | 36 | OUT(LCD_CS); |
paul@0 | 37 | OUT(LCD_RESET); |
paul@0 | 38 | OUT(LCD_CLK); |
paul@0 | 39 | OUT(LCD_SEND); |
paul@0 | 40 | OUT(LCD_BACKLIGHT); |
paul@2 | 41 | |
paul@2 | 42 | CLR(LCD_RESET); |
paul@2 | 43 | CLR(LCD_BACKLIGHT); |
paul@0 | 44 | } |
paul@0 | 45 | |
paul@0 | 46 | void spi_begin() |
paul@0 | 47 | { |
paul@0 | 48 | CLR(LCD_CS); |
paul@0 | 49 | } |
paul@0 | 50 | |
paul@0 | 51 | void spi_end() |
paul@0 | 52 | { |
paul@0 | 53 | SET(LCD_CS); |
paul@0 | 54 | } |
paul@0 | 55 | |
paul@0 | 56 | /** |
paul@0 | 57 | * Send the given value via MOSI/SEND. |
paul@0 | 58 | */ |
paul@0 | 59 | void spi_send(uint8_t v, lcd_sendmode mode) |
paul@0 | 60 | { |
paul@0 | 61 | uint8_t mask; |
paul@0 | 62 | |
paul@0 | 63 | if (mode == LCD_DATA) |
paul@0 | 64 | { |
paul@0 | 65 | #ifdef DEBUG |
paul@0 | 66 | printf("D"); |
paul@0 | 67 | #endif |
paul@0 | 68 | SET(LCD_SEND); |
paul@0 | 69 | } |
paul@0 | 70 | else |
paul@0 | 71 | { |
paul@0 | 72 | #ifdef DEBUG |
paul@0 | 73 | printf("C"); |
paul@0 | 74 | #endif |
paul@0 | 75 | CLR(LCD_SEND); |
paul@0 | 76 | } |
paul@0 | 77 | |
paul@0 | 78 | SET(LCD_CLK); |
paul@0 | 79 | CLR(LCD_CLK); |
paul@0 | 80 | |
paul@0 | 81 | for (mask = 0x80; mask; mask >>= 1) |
paul@0 | 82 | { |
paul@0 | 83 | if (v & mask) |
paul@0 | 84 | { |
paul@0 | 85 | #ifdef DEBUG |
paul@0 | 86 | printf("1"); |
paul@0 | 87 | #endif |
paul@0 | 88 | SET(LCD_SEND); |
paul@0 | 89 | } |
paul@0 | 90 | else |
paul@0 | 91 | { |
paul@0 | 92 | #ifdef DEBUG |
paul@0 | 93 | printf("0"); |
paul@0 | 94 | #endif |
paul@0 | 95 | CLR(LCD_SEND); |
paul@0 | 96 | } |
paul@0 | 97 | |
paul@0 | 98 | SET(LCD_CLK); |
paul@0 | 99 | CLR(LCD_CLK); |
paul@0 | 100 | } |
paul@0 | 101 | |
paul@0 | 102 | #ifdef DEBUG |
paul@0 | 103 | printf("\n"); |
paul@0 | 104 | #endif |
paul@0 | 105 | } |
paul@0 | 106 | |
paul@2 | 107 | void LCD_init() |
paul@0 | 108 | { |
paul@0 | 109 | /* Perform a hardware reset on the LCD. */ |
paul@0 | 110 | /* CS=1, CLK=0, D/C=0 apparently superfluous */ |
paul@0 | 111 | |
paul@0 | 112 | SET(LCD_RESET); |
paul@0 | 113 | usleep(50000); |
paul@0 | 114 | CLR(LCD_RESET); |
paul@0 | 115 | usleep(50000); |
paul@0 | 116 | SET(LCD_RESET); |
paul@0 | 117 | usleep(50000); |
paul@0 | 118 | |
paul@0 | 119 | /* CS=1, CLK=1, D/C=1, 10ms, SWRESET, 10ms apparently superfluous */ |
paul@0 | 120 | |
paul@0 | 121 | LCD_send(LCD_COMMAND, LCD_SLEEPOUT); |
paul@0 | 122 | |
paul@0 | 123 | /* Configure display memory access. */ |
paul@0 | 124 | |
paul@0 | 125 | LCD_send(LCD_COMMAND, LCD_MADCTL); |
paul@0 | 126 | LCD_send(LCD_DATA, LCD_MADCTL_MY_MX); |
paul@0 | 127 | |
paul@0 | 128 | /* Contrast setting (suggested value 0x40), 10ms apparently superfluous */ |
paul@0 | 129 | |
paul@0 | 130 | /* Switch the display on. */ |
paul@0 | 131 | |
paul@0 | 132 | LCD_send(LCD_COMMAND, LCD_DISPON); |
paul@0 | 133 | |
paul@0 | 134 | /* Configure the display mode/format. */ |
paul@0 | 135 | /* 5 == 16bpp */ |
paul@0 | 136 | |
paul@0 | 137 | LCD_send(LCD_COMMAND, LCD_COLMOD); |
paul@0 | 138 | LCD_send(LCD_DATA, 0x05); |
paul@0 | 139 | |
paul@0 | 140 | LCD_send(LCD_COMMAND, LCD_NOP); |
paul@0 | 141 | |
paul@0 | 142 | /* Switch the backlight on. */ |
paul@0 | 143 | |
paul@0 | 144 | SET(LCD_BACKLIGHT); |
paul@0 | 145 | } |
paul@0 | 146 | |
paul@2 | 147 | void LCD_off() |
paul@2 | 148 | { |
paul@2 | 149 | /* Switch the backlight off. */ |
paul@2 | 150 | |
paul@2 | 151 | CLR(LCD_BACKLIGHT); |
paul@2 | 152 | } |
paul@2 | 153 | |
paul@0 | 154 | void LCD_window(uint8_t xmin, uint8_t ymin, uint8_t xmax, uint8_t ymax) |
paul@0 | 155 | { |
paul@0 | 156 | LCD_send(LCD_COMMAND, LCD_CASET); |
paul@0 | 157 | LCD_send(LCD_DATA, xmin); |
paul@0 | 158 | LCD_send(LCD_DATA, xmax); |
paul@0 | 159 | LCD_send(LCD_COMMAND, LCD_PASET); |
paul@0 | 160 | LCD_send(LCD_DATA, ymin); |
paul@0 | 161 | LCD_send(LCD_DATA, ymax); |
paul@0 | 162 | } |
paul@0 | 163 | |
paul@0 | 164 | void LCD_blit_int(uint16_t colour) |
paul@0 | 165 | { |
paul@0 | 166 | LCD_send_more_data((colour >> 8) & 0xff); /* RGGB -> RG */ |
paul@0 | 167 | LCD_send_more_data(colour & 0xff); /* RGGB -> GB */ |
paul@0 | 168 | } |
paul@0 | 169 | |
paul@0 | 170 | void LCD_area(uint8_t xmin, uint8_t ymin, uint8_t xmax, uint8_t ymax, uint16_t colour) |
paul@0 | 171 | { |
paul@0 | 172 | uint16_t i, limit = ((xmax - xmin + 1) * (ymax - ymin + 1)) + 1; |
paul@0 | 173 | |
paul@0 | 174 | LCD_window(xmin, ymin, xmax, ymax); |
paul@0 | 175 | LCD_send(LCD_COMMAND, LCD_RAMWR); |
paul@0 | 176 | |
paul@0 | 177 | for (i = 0; i < limit; i++) |
paul@0 | 178 | { |
paul@0 | 179 | LCD_blit_int(colour); |
paul@0 | 180 | } |
paul@0 | 181 | |
paul@0 | 182 | LCD_send(LCD_COMMAND, LCD_NOP); |
paul@0 | 183 | } |
paul@0 | 184 | |
paul@1 | 185 | void LCD_image(int x, int y, const uint16_t image[], uint8_t width, uint8_t height) |
paul@0 | 186 | { |
paul@0 | 187 | LCD_image_region(x, y, image, width, height, 0, width, 0, height); |
paul@0 | 188 | } |
paul@0 | 189 | |
paul@1 | 190 | void LCD_image_region(int x, int y, const uint16_t image[], uint8_t width, uint8_t height, uint8_t from_x, uint8_t span_x, uint8_t from_y, uint8_t span_y) |
paul@0 | 191 | { |
paul@0 | 192 | uint8_t xmin, ymin, xcmax, ycmax, xcmin, xc, yc; |
paul@0 | 193 | uint16_t xmax, ymax, colour; |
paul@0 | 194 | |
paul@0 | 195 | /* Find the screen ranges for blitting. */ |
paul@0 | 196 | |
paul@0 | 197 | xmin = (x < 0) ? 0 : x; |
paul@0 | 198 | ymin = (y < 0) ? 0 : y; |
paul@0 | 199 | xmax = (x + span_x > SCREEN_X_MAX) ? SCREEN_X_MAX : x + span_x - 1; |
paul@0 | 200 | ymax = (y + span_y > SCREEN_Y_MAX) ? SCREEN_Y_MAX : y + span_y - 1; |
paul@0 | 201 | |
paul@0 | 202 | if ((xmin > SCREEN_X_MAX) || (ymin > SCREEN_Y_MAX) || (xmax < 0) || (ymax < 0)) |
paul@0 | 203 | { |
paul@0 | 204 | return; |
paul@0 | 205 | } |
paul@0 | 206 | |
paul@0 | 207 | /* Find the image ranges for reading data. */ |
paul@0 | 208 | |
paul@0 | 209 | xcmin = (x < 0) ? -x + from_x : from_x; |
paul@0 | 210 | yc = (y < 0) ? -y + from_y : from_y; |
paul@0 | 211 | xcmax = from_x + ((x + span_x > SCREEN_WIDTH) ? SCREEN_WIDTH - x : span_x); |
paul@0 | 212 | ycmax = from_y + ((y + span_y > SCREEN_HEIGHT) ? SCREEN_HEIGHT - y : span_y); |
paul@0 | 213 | |
paul@0 | 214 | LCD_window(xmin, ymin, xmax, ymax); |
paul@0 | 215 | LCD_send(LCD_COMMAND, LCD_RAMWR); |
paul@0 | 216 | |
paul@0 | 217 | /* Reading from the appropriate image ranges, blit to the appropriate |
paul@0 | 218 | screen ranges. */ |
paul@0 | 219 | |
paul@0 | 220 | xc = xcmin; |
paul@0 | 221 | while (yc < ycmax) |
paul@0 | 222 | { |
paul@0 | 223 | colour = image[yc * width + xc]; |
paul@0 | 224 | LCD_blit_int(colour); |
paul@0 | 225 | |
paul@0 | 226 | xc++; |
paul@0 | 227 | if (xc == xcmax) |
paul@0 | 228 | { |
paul@0 | 229 | xc = xcmin; |
paul@0 | 230 | yc++; |
paul@0 | 231 | } |
paul@0 | 232 | } |
paul@0 | 233 | |
paul@0 | 234 | LCD_send(LCD_COMMAND, LCD_NOP); |
paul@0 | 235 | } |
paul@0 | 236 | |
paul@2 | 237 | void LCD_normal() |
paul@0 | 238 | { |
paul@0 | 239 | LCD_send(LCD_COMMAND, LCD_NORON); |
paul@0 | 240 | } |
paul@0 | 241 | |
paul@0 | 242 | void LCD_scroll_area(uint8_t top_fixed, uint8_t scrolling, uint8_t bottom_fixed) |
paul@0 | 243 | { |
paul@0 | 244 | LCD_send(LCD_COMMAND, LCD_VSCRDEF); |
paul@0 | 245 | LCD_send(LCD_DATA, top_fixed); |
paul@0 | 246 | LCD_send(LCD_DATA, scrolling); |
paul@0 | 247 | LCD_send(LCD_DATA, bottom_fixed); |
paul@0 | 248 | } |
paul@0 | 249 | |
paul@0 | 250 | void LCD_scroll_start(uint8_t address) |
paul@0 | 251 | { |
paul@0 | 252 | LCD_send(LCD_COMMAND, LCD_SEP); |
paul@0 | 253 | LCD_send(LCD_DATA, address); |
paul@0 | 254 | } |
paul@0 | 255 | |
paul@0 | 256 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@0 | 257 | */ |