Subversion Repositories libOLED

Rev

Rev 11 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * displayclass.H
  3.  *
  4.  *  Created on: 31 Oct 2020
  5.  *      Author: mike
  6.  */
  7.  
  8. #pragma once
  9. #include <cstdint>
  10. #include "libOLED/fontclass.H"
  11.  
  12. inline int constexpr dataSize(int const width, int const height)
  13. {
  14.   return (width * height / 8);
  15. }
  16.  
  17. enum colour_t
  18. {
  19.   BLACK,   /* and 0, invert 0 */
  20.   WHITE,   /* and 0, invert 1 */
  21.   OVERLAY, /* and 1, invert 0 */
  22.   INVERT,  /* and 1, invert 1 */
  23. };
  24.  
  25. class display_t
  26. {
  27. public:
  28.   display_t(int const width, int const height, int const ramwidth,
  29.             uint8_t *const data);
  30.  
  31.   virtual ~display_t();
  32.  
  33.   /// \brief get the current rotation of the display : doesnt work too well.
  34.   uint8_t
  35.   getRotation();
  36.  
  37.   /// @brief  Get pixel width
  38.   /// @return width
  39.   int16_t
  40.   width();
  41.   /// @brief Get pixel height
  42.   /// @return height
  43.   int16_t
  44.   height();
  45.  
  46.   /// @brief common hardware reset, resets ALL displays
  47.   void
  48.   reset();
  49.  
  50.   void
  51.   init();
  52.  
  53.   /// \brief Clear display to colour
  54.   void
  55.   clearDisplay(colour_t colour = colour_t::BLACK);
  56.  
  57.   /// @brief Invert display
  58.   /// @param i true : invert display
  59.   void
  60.   invertDisplay(uint8_t i);
  61.  
  62.   /// @brief Show the workspace on the screen
  63.   void
  64.   display();
  65.  
  66.   void
  67.   startscrollright(uint8_t start, uint8_t stop);
  68.   void
  69.   startscrollleft(uint8_t start, uint8_t stop);
  70.  
  71.   void
  72.   startscrolldiagright(uint8_t start, uint8_t stop);
  73.   void
  74.   startscrolldiagleft(uint8_t start, uint8_t stop);
  75.   void
  76.   stopscroll(void);
  77.  
  78.   /// @brief Set display contrast
  79.   /// @param contrast 0..255 but theres really only two values worth bothering with, 0 and 255
  80.   void
  81.   dim(uint8_t contrast);
  82.  
  83.   // set drawing mode
  84.   void
  85.   setPixelMode(colour_t colour)
  86.   {
  87.     m_colour = colour;
  88.   }
  89.  
  90.   /// @brief Draw a single pixel
  91.   /// @param x
  92.   /// @param y
  93.   /// @param pixel
  94.   void
  95.   drawPixel(int16_t x, int16_t y, bool pixel);
  96.  
  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.
  105.   void
  106.   drawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, colour_t color = WHITE, int8_t pattern = 1);
  107.  
  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
  114.   void
  115.   drawRectangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2,
  116.                 colour_t color = WHITE);
  117.  
  118.   /// @brief Position cursor for text drawing
  119.   /// @param x top left
  120.   /// @param y top left
  121.   void
  122.   gotoxy(int x, int y)
  123.   {
  124.     m_cursor_x = x;
  125.     m_cursor_y = y;
  126.   }
  127.  
  128.   /// @brief Position cursor relative for text drawing
  129.   /// @param x relative
  130.   /// @param y relative
  131.   void
  132.   moveby(int x, int y)
  133.   {
  134.     m_cursor_x += x;
  135.     m_cursor_y += y;
  136.   }
  137.  
  138.   // pixel to pixel plotting
  139.   /// \param font The font to use
  140.   /// \param string The characters to plot
  141.   /// \param length The length of the string (0 means use strlen(string))
  142.   /// \param colour colour code
  143.   void
  144.   printString(font_t &font, char const *string, uint16_t length,
  145.               colour_t colour = WHITE);
  146.  
  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
  152.   /// \param colour colour code
  153.   void
  154.   printScaledString(font_t &font, char const *string, uint16_t length,
  155.                     uint16_t scale, colour_t colour = WHITE);
  156.  
  157.   static const uint8_t NO_DECIMAL = 255;
  158.  
  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.
  167.   void
  168.   fontDigits(font_t &font, uint8_t digits, uint8_t dp_pos, int val,
  169.              colour_t colour = WHITE);
  170.  
  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
  173.   ///  can be performed using width without type casting.
  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);
  177.  
  178.   int cursor_x()
  179.   {
  180.     return m_cursor_x;
  181.   };
  182.  
  183.   int cursor_y()
  184.   {
  185.     return m_cursor_y;
  186.   };
  187.  
  188. private:
  189.   // set=1 means send data set=0 means send control
  190.   virtual void
  191.   oledSetCD(uint8_t set) = 0;
  192.  
  193.   virtual void
  194.   oledWrite(uint8_t d) = 0;
  195.  
  196.   virtual void
  197.   oledReset() = 0;
  198.  
  199.   virtual void
  200.   oledWrite(uint8_t *buff, uint8_t len) = 0;
  201.  
  202.   uint8_t
  203.   formatNum(char *buff, uint8_t siz, uint8_t digits, uint8_t dp_pos, int val);
  204.  
  205.   int const m_width;    // pixel width
  206.   int const m_height;   // pixel height
  207.   int const m_ramwidth; // OLED controller ram pixel width
  208.  
  209.   int m_cursor_x;
  210.   int m_cursor_y;
  211.   int m_rotation;
  212.  
  213.   // currently selected colour mode
  214.   colour_t m_colour;
  215.  
  216.   uint8_t *const m_data;
  217. };
  218.