| 1 | /* |
| 2 | |
| 3 | u8x8_display.c |
| 4 | |
| 5 | Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/) |
| 6 | |
| 7 | Copyright (c) 2016, olikraus@gmail.com |
| 8 | All rights reserved. |
| 9 | |
| 10 | Redistribution and use in source and binary forms, with or without modification, |
| 11 | are permitted provided that the following conditions are met: |
| 12 | |
| 13 | * Redistributions of source code must retain the above copyright notice, this list |
| 14 | of conditions and the following disclaimer. |
| 15 | |
| 16 | * Redistributions in binary form must reproduce the above copyright notice, this |
| 17 | list of conditions and the following disclaimer in the documentation and/or other |
| 18 | materials provided with the distribution. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 21 | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 22 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 25 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 27 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 30 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 32 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | |
| 34 | |
| 35 | Abstraction layer for the graphics controller. |
| 36 | Main goal is the placement of a 8x8 pixel block (tile) on the display. |
| 37 | |
| 38 | */ |
| 39 | |
| 40 | |
| 41 | #include "u8x8.h" |
| 42 | |
| 43 | |
| 44 | /*==========================================*/ |
| 45 | /* internal library function */ |
| 46 | |
| 47 | /* |
| 48 | this is a helper function for the U8X8_MSG_DISPLAY_SETUP_MEMORY function. |
| 49 | It can be called within the display callback function to carry out the usual standard tasks. |
| 50 | |
| 51 | */ |
| 52 | void u8x8_d_helper_display_setup_memory(u8x8_t *u8x8, const u8x8_display_info_t *display_info) |
| 53 | { |
| 54 | /* 1) set display info struct */ |
| 55 | u8x8->display_info = display_info; |
| 56 | u8x8->x_offset = u8x8->display_info->default_x_offset; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | this is a helper function for the U8X8_MSG_DISPLAY_INIT function. |
| 61 | It can be called within the display callback function to carry out the usual standard tasks. |
| 62 | |
| 63 | */ |
| 64 | void u8x8_d_helper_display_init(u8x8_t *u8x8) |
| 65 | { |
| 66 | /* 2) apply port directions to the GPIO lines and apply default values for the IO lines*/ |
| 67 | u8x8_gpio_Init(u8x8); /* macro, which calls gpio_and_delay_cb with U8X8_MSG_GPIO_AND_DELAY_INIT */ |
| 68 | u8x8_cad_Init(u8x8); /* this will also call U8X8_MSG_BYTE_INIT, byte init will NOT call GPIO_INIT */ |
| 69 | |
| 70 | /* 3) do reset */ |
| 71 | u8x8_gpio_SetReset(u8x8, 1); |
| 72 | u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_MILLI, u8x8->display_info->reset_pulse_width_ms); |
| 73 | u8x8_gpio_SetReset(u8x8, 0); |
| 74 | u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_MILLI, u8x8->display_info->reset_pulse_width_ms); |
| 75 | u8x8_gpio_SetReset(u8x8, 1); |
| 76 | u8x8_gpio_Delay(u8x8, U8X8_MSG_DELAY_MILLI, u8x8->display_info->post_reset_wait_ms); |
| 77 | } |
| 78 | |
| 79 | /*==========================================*/ |
| 80 | /* official functions */ |
| 81 | |
| 82 | uint8_t u8x8_DrawTile(u8x8_t *u8x8, uint8_t x, uint8_t y, uint8_t cnt, uint8_t *tile_ptr) |
| 83 | { |
| 84 | u8x8_tile_t tile; |
| 85 | tile.x_pos = x; |
| 86 | tile.y_pos = y; |
| 87 | tile.cnt = cnt; |
| 88 | tile.tile_ptr = tile_ptr; |
| 89 | return u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_DRAW_TILE, 1, (void *)&tile); |
| 90 | } |
| 91 | |
| 92 | /* should be implemented as macro */ |
| 93 | void u8x8_SetupMemory(u8x8_t *u8x8) |
| 94 | { |
| 95 | u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_SETUP_MEMORY, 0, NULL); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | This will just init the display interface, compared to InitDisplay, it will not issue a reset and also not upload the init code. |
| 100 | Comparison: |
| 101 | Call u8x8_InitInterface u8x8_InitDisplay |
| 102 | Init Interface yes yes |
| 103 | Reset Display no yes |
| 104 | Upload Display Init Code no yes |
| 105 | |
| 106 | u8x8_InitInterface() is an alternative function to u8x8_InitDisplay(). Do not call both. |
| 107 | |
| 108 | */ |
| 109 | void u8x8_InitInterface(u8x8_t *u8x8) |
| 110 | { |
| 111 | u8x8_gpio_Init(u8x8); /* macro, which calls gpio_and_delay_cb with U8X8_MSG_GPIO_AND_DELAY_INIT */ |
| 112 | u8x8_cad_Init(u8x8); /* this will also call U8X8_MSG_BYTE_INIT, byte init will NOT call GPIO_INIT, which alread is called in the prev step */ |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | This will sent the display init message to the display. |
| 117 | The display itself will then call u8x8_d_helper_display_init() from above. This includes: |
| 118 | GPIO Init (set port directions) |
| 119 | BYTE init (part of CAD init: which may set some levels) |
| 120 | CAD init (which will set things like I2C default address) |
| 121 | Issue a reset to the display: This will usually turn off the display |
| 122 | Additonally each display will set the init code to the display, which will also turn of the display in most cases (Arduino code disable power save mode later) |
| 123 | |
| 124 | Actually this procedure should be better called InitInterfaceAndDisplay, because it actually does both. |
| 125 | (actually u8x8_InitInterface() is not called directly but only u8x8_gpio_Init and u8x8_cad_Init which |
| 126 | in turn is called by u8x8_InitInterface()) |
| 127 | |
| 128 | |
| 129 | InitDisplay is called by the Arduino begin() function |
| 130 | |
| 131 | In some cases it is not required to init the display (for example if the display is already running, but the controller comes out of deep sleep mode). |
| 132 | Then InitDisplay can be skipped, but u8x8_InitInterface() (== u8x8_gpio_Init() and u8x8_cad_Init()) need to be executed. |
| 133 | |
| 134 | */ |
| 135 | void u8x8_InitDisplay(u8x8_t *u8x8) |
| 136 | { |
| 137 | u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_INIT, 0, NULL); /* this will call u8x8_d_helper_display_init() and send the init seqence to the display */ |
| 138 | /* u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_SET_FLIP_MODE, 0, NULL); */ /* It would make sense to call flip mode 0 here after U8X8_MSG_DISPLAY_INIT */ |
| 139 | } |
| 140 | |
| 141 | void u8x8_SetPowerSave(u8x8_t *u8x8, uint8_t is_enable) |
| 142 | { |
| 143 | u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_SET_POWER_SAVE, is_enable, NULL); |
| 144 | } |
| 145 | |
| 146 | void u8x8_SetFlipMode(u8x8_t *u8x8, uint8_t mode) |
| 147 | { |
| 148 | u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_SET_FLIP_MODE, mode, NULL); |
| 149 | } |
| 150 | |
| 151 | void u8x8_SetContrast(u8x8_t *u8x8, uint8_t value) |
| 152 | { |
| 153 | u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_SET_CONTRAST, value, NULL); |
| 154 | } |
| 155 | |
| 156 | void u8x8_RefreshDisplay(u8x8_t *u8x8) |
| 157 | { |
| 158 | u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_REFRESH, 0, NULL); |
| 159 | } |
| 160 | |
| 161 | void u8x8_ClearDisplayWithTile(u8x8_t *u8x8, const uint8_t *buf) |
| 162 | { |
| 163 | u8x8_tile_t tile; |
| 164 | uint8_t h; |
| 165 | |
| 166 | tile.x_pos = 0; |
| 167 | tile.cnt = 1; |
| 168 | tile.tile_ptr = (uint8_t *)buf; /* tile_ptr should be const, but isn't */ |
| 169 | |
| 170 | h = u8x8->display_info->tile_height; |
| 171 | tile.y_pos = 0; |
| 172 | do |
| 173 | { |
| 174 | u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_DRAW_TILE, u8x8->display_info->tile_width, (void *)&tile); |
| 175 | tile.y_pos++; |
| 176 | } while( tile.y_pos < h ); |
| 177 | } |
| 178 | |
| 179 | void u8x8_ClearDisplay(u8x8_t *u8x8) |
| 180 | { |
| 181 | uint8_t buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 182 | u8x8_ClearDisplayWithTile(u8x8, buf); |
| 183 | } |
| 184 | |
| 185 | void u8x8_FillDisplay(u8x8_t *u8x8) |
| 186 | { |
| 187 | uint8_t buf[8] = { 255, 255, 255, 255, 255, 255, 255, 255 }; |
| 188 | u8x8_ClearDisplayWithTile(u8x8, buf); |
| 189 | } |
| 190 | |
| 191 | void u8x8_ClearLine(u8x8_t *u8x8, uint8_t line) |
| 192 | { |
| 193 | uint8_t buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 194 | u8x8_tile_t tile; |
| 195 | if ( line < u8x8->display_info->tile_height ) |
| 196 | { |
| 197 | tile.x_pos = 0; |
| 198 | tile.y_pos = line; |
| 199 | tile.cnt = 1; |
| 200 | tile.tile_ptr = (uint8_t *)buf; /* tile_ptr should be const, but isn't */ |
| 201 | u8x8->display_cb(u8x8, U8X8_MSG_DISPLAY_DRAW_TILE, u8x8->display_info->tile_width, (void *)&tile); |
| 202 | } |
| 203 | } |