fontDriver.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "fontDriver.h"
  2. // Constructor: at minimum pass in the size of the display
  3. /*
  4. xSize: number of pixels in the x direction of the display
  5. ySize: number of pixels in the y direction of the display
  6. *Note:
  7. This notation allows you to explicitly state what variables are passed to the parent class's constructor when the derived class' constructor is called.
  8. Additional direct or virtual base classes can also be initialized by a comma separated list with the same syntax - the 'deepest' base class is listed first.
  9. */
  10. fontDriver::fontDriver(uint16_t xSize, uint16_t ySize /* Additional Parameters */) : hyperdisplay(xSize, ySize) /* , anotherVirtualBaseClass(params), aDirectBaseClass(moreParams) */
  11. {
  12. // Perform setup of the derived class with any additional parameters here.
  13. }
  14. // getoffsetColor: allows hyperdisplay to use your custom color type
  15. /*
  16. base: the pointer to the first byte of the array that holds the color data
  17. numPixels: the number of pixels away from the beginning that the function should return the pointer to
  18. */
  19. color_t fontDriver::getOffsetColor(color_t base, uint32_t numPixels)
  20. {
  21. // This method is requried so that your color type can be totally flexible - be it an enumeration of three colors for an E-ink
  22. // display or a structure of bytes for 24-bit color it is totally up to you and how your display works.
  23. // This implementation will depend on how you choose to store colors, however one decent way to do it is provided as a reference:
  24. // This function returns an offset pointer according to the number of pixels and the _colorMode of the object
  25. // color_t pret = NULL;
  26. // your_color_type * ptemp = (your_color_type *)base; // Here's the magic. Cast the base pointer to a pointer of your color type to allow pointer arithmetic
  27. // pret = (color_t)(ptemp + numPixels); // The offset by the number of pixels. This will account for the number of bytes that your color type occupies
  28. // return pret; // And return the offset pointer
  29. }
  30. // hwPixel: the method by which hyperdisplay actually changes your screen
  31. /*
  32. x0, y0: the x and y coordinates at which to place the pixel. 0,0 is the upper-left corner of the screen, x is horizontal and y is vertical
  33. data: the pointer to where the color data is stored
  34. colorCycleLength: this indicates how many pixels worth of valid color data exist contiguously after the memory location pointed to by color.
  35. startColorOffset: this indicates how many pixels to offset by from the color pointer to arrive at the actual color to display
  36. */
  37. void fontDriver::hwpixel(hd_hw_extent_t x0, hd_hw_extent_t y0, color_t data, hd_colors_t colorCycleLength, hd_colors_t startColorOffset)
  38. {
  39. // Here you write the code that sets a pixel. It is up to you what to do with that data. Here are two basic options:
  40. // 1) Write directly to display ram: if you choose this option and your display supports it then this is all you need to show an image
  41. // 2) Write to a scratch space: you might use this option to compose a whole image and then show it all on the screen at once.
  42. // In that case you would need your own function that actually gets all that information to the display when the time is right.
  43. }
  44. // Additional hardware drawing functions
  45. /*
  46. There are additional hardware drawing functions beyond hwpixel. They are already implemented by default using
  47. hwpixel so they are not required in order to start drawing. However implementing them with more efficient
  48. methods for your particular hardware can reduce overhead and speed up the drawing process.
  49. In these functions the coordiantes x0, x1, y0, and y1 are always with respect to the hardware screen. (0,0) is the upper-left pixel
  50. The variables pertaining to color sequences (data, colorCycleLength, and startColorOffset) always have the same meaning as in hwpixel
  51. Additional variables will be described in the function prototype in bareMinimumDerivedClass.cpp
  52. */
  53. // void fontDriver::hwxline(uint16_t x0, uint16_t y0, uint16_t len, color_t data, uint16_t colorCycleLength = 1, uint16_t startColorOffset = 0, bool goLeft = false)
  54. // void fontDriver::hwyline(uint16_t x0, uint16_t y0, uint16_t len, color_t data, uint16_t colorCycleLength = 1, uint16_t startColorOffset = 0, bool goUp = false);
  55. // void fontDriver::hwrectangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, color_t data, bool filled = false, uint16_t colorCycleLength = 1, uint16_t startColorOffset = 0, bool gradientVertical = false, bool reverseGradient = false);
  56. // void fontDriver::hwfillFromArray(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint32_t numPixels, color_t data);
  57. // Additional optional implementations by the user:
  58. // ================================================
  59. // getCharInfo: you can create custom fonts without changing how printing functions work
  60. /*
  61. character: the byte-sized character to show on screen
  62. pchar: a pointer to a valid char_info_t object that needs to be filled out peroperly for the given character
  63. */
  64. void fontDriver::getCharInfo(uint8_t character, char_info_t * pchar)
  65. {
  66. /* This function supports the exisitng HyperDisplay::write() function. It can produce fonts all on its own without the user implementing 'write()'.
  67. It works by filling out a character info structure (char_info_t) for the given character. The character info structure allows you to show any glyph within a xDim * yDim rectangle.
  68. The user must:
  69. - indicate how many pixels are to be rendered for the given glyph with 'numPixels'
  70. - indicate the x and y dimensions of the glyph with 'xDim' and 'yDim'
  71. - provide a pointer to an array of hd_font_extent_t variables for x and y that indicate where to render pixels 'xLoc' and 'yLoc' (must contain at least 'numPixels' valid entries)
  72. - indicate whether or not to actually render anything for this character with 'show' (false means HyperDisplay will ignore that operation)
  73. - indicate if you want the character to cause a line feed + carriage return 'causesNewline'
  74. */
  75. /*
  76. typedef struct character_info{
  77. color_t data; // deprecated - don't use
  78. hd_font_extent_t* xLoc; // x location data relative to the upper left-corner of the character area
  79. hd_font_extent_t* yLoc; // y location data relative to the upper left-corner of the character area
  80. hd_font_extent_t xDim; // The maximum value of xLoc
  81. hd_font_extent_t yDim; // The maximum value of yLoc - also the number of pixels to move down for characters that cause new lines
  82. hd_pixels_t numPixels; // The number of color_t types that pdata points to
  83. bool show; // Whether or not to actually show the character
  84. bool causesNewline; // This indicates if the given chracter is meant to cause a newline
  85. }char_info_t; // Character information structure for placing pixels in a window
  86. */
  87. // Example:
  88. const myFontWidth = 1;
  89. const myFontHeight = 8;
  90. bool charIsRendered = (((character >= 'a') && (character <= 'z')) || ((character >= 'A') && (character <= 'Z')));
  91. static hd_font_extent_t xLocations[myFontHeight*myFontWidth]; // myFontHeight*myFontWidth 'x' coordinates here to be used by HyperDisplay later (static)
  92. static hd_font_extent_t yLocations[myFontHeight*myFontWidth]; // myFontHeight*myFontWidth 'y' coordinates here
  93. if(character == '\n'){
  94. pchar->causesNewline = true;
  95. pchar->numPixels = 0; // no pixels to render for newline
  96. pchar->xLoc = NULL; // just being explicit - HD won't use this since numPixels = 0
  97. pchar->yLoc = NULL; // just being explicit - HD won't use this since numPixels = 0
  98. pchar->xDim = 0; // '\n' should not have any width - otherwise it would look like a space
  99. pchar->yDim = myFontHeight; //
  100. pchar->show = false; // no pixels to show
  101. }else if(charIsRendered){
  102. pchar->causesNewline = false;
  103. pchar->xLoc = xLocations; // Point at the location arrays (which we will shortly make sure are filled out correctly)
  104. pchar->yLoc = yLocations; //
  105. pchar->xDim = myFontWidth; //
  106. pchar->yDim = myFontHeight; //
  107. pchar->show = true; // we want to actually show the pixels
  108. pchar->numPixels = 0; // we are going to figure out how many to show based on the character...
  109. // Here's how we determine where to put pixels for this font... (remember, setting x and y locations and 'numPixels' determines where pixels are drawn for this character, in local coordinates)
  110. for(uint8_t ix = 0; ix < 8; ix++){ // loop through 8 bits of the character
  111. if(character & (0x01 << ix)){ // if this bit is set...
  112. xLocations[pchar->numPixels] = 0; // the x location will always be zero
  113. xLocations[pchar->numPixels] = ix;// the y location will be equal to the index of the bit that was set
  114. pchar->numPixels++; // increment the number of pixels to show (also increments where to put the next location info)
  115. }
  116. }
  117. }
  118. }
  119. // write: you decide what happens when someone calls fontDriver.print or fontDriver.println
  120. /*
  121. val: the byte-sized character value to display
  122. */
  123. size_t fontDriver::write(uint8_t val){
  124. /* ALTERNATELY: if you don't like how fonts are drawn by default then you can make your own 'write()' function */
  125. // Use your imagination!
  126. // Maybe you want to draw a random line for every letter....
  127. line(random(64), random(64), random(64), random(64), random(4));
  128. // Or maybe something completely different. Anyway it is up to you.
  129. }