Rev 11 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2 | mjames | 1 | /* |
| 2 | * displayclass.H |
||
| 3 | * |
||
| 4 | * Created on: 31 Oct 2020 |
||
| 5 | * Author: mike |
||
| 6 | */ |
||
| 7 | |||
| 8 | #pragma once |
||
| 9 | #include <cstdint> |
||
| 3 | mjames | 10 | #include "libOLED/fontclass.H" |
| 2 | mjames | 11 | |
| 10 | mjames | 12 | inline int constexpr dataSize(int const width, int const height) |
| 2 | mjames | 13 | { |
| 4 | mjames | 14 | return (width * height / 8); |
| 2 | mjames | 15 | } |
| 16 | |||
| 17 | enum colour_t |
||
| 18 | { |
||
| 10 | mjames | 19 | BLACK, /* and 0, invert 0 */ |
| 20 | WHITE, /* and 0, invert 1 */ |
||
| 7 | mjames | 21 | OVERLAY, /* and 1, invert 0 */ |
| 10 | mjames | 22 | INVERT, /* and 1, invert 1 */ |
| 2 | mjames | 23 | }; |
| 24 | |||
| 25 | class display_t |
||
| 26 | { |
||
| 27 | public: |
||
| 10 | mjames | 28 | display_t(int const width, int const height, int const ramwidth, |
| 29 | uint8_t *const data); |
||
| 2 | mjames | 30 | |
| 10 | mjames | 31 | virtual ~display_t(); |
| 2 | mjames | 32 | |
| 13 | mjames | 33 | /// \brief get the current rotation of the display : doesnt work too well. |
| 4 | mjames | 34 | uint8_t |
| 10 | mjames | 35 | getRotation(); |
| 36 | |||
| 13 | mjames | 37 | /// @brief Get pixel width |
| 38 | /// @return width |
||
| 4 | mjames | 39 | int16_t |
| 10 | mjames | 40 | width(); |
| 13 | mjames | 41 | /// @brief Get pixel height |
| 42 | /// @return height |
||
| 4 | mjames | 43 | int16_t |
| 10 | mjames | 44 | height(); |
| 2 | mjames | 45 | |
| 13 | mjames | 46 | /// @brief common hardware reset, resets ALL displays |
| 9 | mjames | 47 | void |
| 10 | mjames | 48 | reset(); |
| 7 | mjames | 49 | |
| 4 | mjames | 50 | void |
| 10 | mjames | 51 | init(); |
| 2 | mjames | 52 | |
| 6 | mjames | 53 | /// \brief Clear display to colour |
| 4 | mjames | 54 | void |
| 10 | mjames | 55 | clearDisplay(colour_t colour = colour_t::BLACK); |
| 13 | mjames | 56 | |
| 57 | /// @brief Invert display |
||
| 58 | /// @param i true : invert display |
||
| 4 | mjames | 59 | void |
| 10 | mjames | 60 | invertDisplay(uint8_t i); |
| 13 | mjames | 61 | |
| 62 | /// @brief Show the workspace on the screen |
||
| 4 | mjames | 63 | void |
| 10 | mjames | 64 | display(); |
| 2 | mjames | 65 | |
| 4 | mjames | 66 | void |
| 10 | mjames | 67 | startscrollright(uint8_t start, uint8_t stop); |
| 4 | mjames | 68 | void |
| 10 | mjames | 69 | startscrollleft(uint8_t start, uint8_t stop); |
| 2 | mjames | 70 | |
| 4 | mjames | 71 | void |
| 10 | mjames | 72 | startscrolldiagright(uint8_t start, uint8_t stop); |
| 4 | mjames | 73 | void |
| 10 | mjames | 74 | startscrolldiagleft(uint8_t start, uint8_t stop); |
| 4 | mjames | 75 | void |
| 10 | mjames | 76 | stopscroll(void); |
| 2 | mjames | 77 | |
| 13 | mjames | 78 | /// @brief Set display contrast |
| 79 | /// @param contrast 0..255 but theres really only two values worth bothering with, 0 and 255 |
||
| 4 | mjames | 80 | void |
| 10 | mjames | 81 | dim(uint8_t contrast); |
| 2 | mjames | 82 | |
| 7 | mjames | 83 | // set drawing mode |
| 9 | mjames | 84 | void |
| 10 | mjames | 85 | setPixelMode(colour_t colour) |
| 9 | mjames | 86 | { |
| 87 | m_colour = colour; |
||
| 88 | } |
||
| 7 | mjames | 89 | |
| 13 | mjames | 90 | /// @brief Draw a single pixel |
| 91 | /// @param x |
||
| 92 | /// @param y |
||
| 93 | /// @param pixel |
||
| 4 | mjames | 94 | void |
| 10 | mjames | 95 | drawPixel(int16_t x, int16_t y, bool pixel); |
| 2 | mjames | 96 | |
| 13 | mjames | 97 | /// @brief Draw a line according to Bressenham algorithm, with optional dash / dot patternign |
| 98 | /// @param x1 coordinate start |
||
| 99 | /// @param y1 coordinate start |
||
| 100 | /// @param x2 coordinate end |
||
| 101 | /// @param y2 coordinate end |
||
| 102 | /// @param color colour code |
||
| 103 | /// @param pattern a binary pattern with at least a single bit set - if least significant bit set, draw pixel |
||
| 104 | /// if bit zero dont draw pixel. After drawing a pixel, shift pattern right one bit. If pattern becomes all zeros, reset with function argument. |
||
| 4 | mjames | 105 | void |
| 11 | mjames | 106 | drawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, colour_t color = WHITE, int8_t pattern = 1); |
| 2 | mjames | 107 | |
| 13 | mjames | 108 | /// @brief Draw a solid rectangle using byte operations, rather than line draw |
| 109 | /// @param x1 top left |
||
| 110 | /// @param y1 top left |
||
| 111 | /// @param x2 bottom right |
||
| 112 | /// @param y2 bottom right |
||
| 113 | /// @param color colour code |
||
| 9 | mjames | 114 | void |
| 10 | mjames | 115 | drawRectangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, |
| 13 | mjames | 116 | colour_t color = WHITE); |
| 6 | mjames | 117 | |
| 13 | mjames | 118 | /// @brief Position cursor for text drawing |
| 119 | /// @param x top left |
||
| 120 | /// @param y top left |
||
| 4 | mjames | 121 | void |
| 10 | mjames | 122 | gotoxy(int x, int y) |
| 4 | mjames | 123 | { |
| 124 | m_cursor_x = x; |
||
| 125 | m_cursor_y = y; |
||
| 126 | } |
||
| 2 | mjames | 127 | |
| 13 | mjames | 128 | /// @brief Position cursor relative for text drawing |
| 129 | /// @param x relative |
||
| 130 | /// @param y relative |
||
| 9 | mjames | 131 | void |
| 10 | mjames | 132 | moveby(int x, int y) |
| 9 | mjames | 133 | { |
| 134 | m_cursor_x += x; |
||
| 135 | m_cursor_y += y; |
||
| 136 | } |
||
| 137 | |||
| 4 | mjames | 138 | // pixel to pixel plotting |
| 139 | /// \param font The font to use |
||
| 140 | /// \param string The characters to plot |
||
| 13 | mjames | 141 | /// \param length The length of the string (0 means use strlen(string)) |
| 142 | /// \param colour colour code |
||
| 4 | mjames | 143 | void |
| 10 | mjames | 144 | printString(font_t &font, char const *string, uint16_t length, |
| 145 | colour_t colour = WHITE); |
||
| 2 | mjames | 146 | |
| 4 | mjames | 147 | // scaled plotting |
| 148 | /// \param font The font to use |
||
| 149 | /// \param string The characters to plot |
||
| 150 | /// \param length The length of the string |
||
| 151 | /// \param scale The scale factor is 256/scale so 256 is 1:1, 128 is twice the size |
||
| 13 | mjames | 152 | /// \param colour colour code |
| 4 | mjames | 153 | void |
| 10 | mjames | 154 | printScaledString(font_t &font, char const *string, uint16_t length, |
| 155 | uint16_t scale, colour_t colour = WHITE); |
||
| 2 | mjames | 156 | |
| 7 | mjames | 157 | static const uint8_t NO_DECIMAL = 255; |
| 158 | |||
| 10 | mjames | 159 | /// \brief Display signed integer value with expected field width, and optional |
| 160 | /// decimal point position |
||
| 161 | /// \param font Reference to font in use |
||
| 162 | /// \param digits Number of digits to display (not including decimal point) |
||
| 163 | /// \param dp_pos If less than 10, attempt to place decimal point in given position. |
||
| 164 | /// set to > 10 to not try to display decimal point |
||
| 165 | /// \param val value to display |
||
| 166 | /// \param colour Drawing colour for font. |
||
| 4 | mjames | 167 | void |
| 10 | mjames | 168 | fontDigits(font_t &font, uint8_t digits, uint8_t dp_pos, int val, |
| 169 | colour_t colour = WHITE); |
||
| 2 | mjames | 170 | |
| 10 | mjames | 171 | /// \brief Calculate the length of the numeric string, and display it justified or padded |
| 172 | /// \return The formatted width : returned as signed so calculations which have negative results |
||
| 11 | mjames | 173 | /// can be performed using width without type casting. |
| 10 | mjames | 174 | int8_t |
| 175 | fontSigDigits(font_t &font, uint8_t x, uint8_t y, bool right_justify, |
||
| 176 | uint8_t dp_pos, int val, colour_t colour = WHITE); |
||
| 5 | mjames | 177 | |
| 10 | mjames | 178 | int cursor_x() |
| 4 | mjames | 179 | { |
| 180 | return m_cursor_x; |
||
| 10 | mjames | 181 | }; |
| 2 | mjames | 182 | |
| 10 | mjames | 183 | int cursor_y() |
| 4 | mjames | 184 | { |
| 185 | return m_cursor_y; |
||
| 10 | mjames | 186 | }; |
| 2 | mjames | 187 | |
| 188 | private: |
||
| 189 | // set=1 means send data set=0 means send control |
||
| 4 | mjames | 190 | virtual void |
| 10 | mjames | 191 | oledSetCD(uint8_t set) = 0; |
| 2 | mjames | 192 | |
| 4 | mjames | 193 | virtual void |
| 10 | mjames | 194 | oledWrite(uint8_t d) = 0; |
| 2 | mjames | 195 | |
| 4 | mjames | 196 | virtual void |
| 10 | mjames | 197 | oledReset() = 0; |
| 2 | mjames | 198 | |
| 4 | mjames | 199 | virtual void |
| 10 | mjames | 200 | oledWrite(uint8_t *buff, uint8_t len) = 0; |
| 2 | mjames | 201 | |
| 9 | mjames | 202 | uint8_t |
| 10 | mjames | 203 | formatNum(char *buff, uint8_t siz, uint8_t digits, uint8_t dp_pos, int val); |
| 5 | mjames | 204 | |
| 10 | mjames | 205 | int const m_width; // pixel width |
| 206 | int const m_height; // pixel height |
||
| 2 | mjames | 207 | int const m_ramwidth; // OLED controller ram pixel width |
| 208 | |||
| 209 | int m_cursor_x; |
||
| 4 | mjames | 210 | int m_cursor_y; |
| 211 | int m_rotation; |
||
| 2 | mjames | 212 | |
| 7 | mjames | 213 | // currently selected colour mode |
| 214 | colour_t m_colour; |
||
| 215 | |||
| 4 | mjames | 216 | uint8_t *const m_data; |
| 217 | }; |