SparkFun SSD168x Library  v1.0.2
Library for SSD1680 / SSD1681 e-paper displays on SPI
Loading...
Searching...
No Matches
qwiic_resdef.h
Go to the documentation of this file.
1
19//
20// Define resource objects/structures to manage fonts and bitmaps
21
22//
23#pragma once
24
25// NOTE: The way I'm including the font/resource defines, some platforms have
26// include file issues
27
28#include "qw_pgm_arduino.h"
29
30#include <stdint.h>
31
33// Notes Template singletons for managing resources
35//
36// The common use/storage of bitmap and font data is creating a static
37// array and placing it in a header file.
38//
39// This pattern is fine for simple uses, where the bitmap is only included
40// in a single file. BUT if the bitmap header is included in multiple files,
41// multiple copies of the bitmap are created.
42//
43// You could just make the bitmap array a const - but then multiple includes
44// will lead to symbol collision at link time - confusing the user.
45//
46// These simple classes help place the bitmap data in a singleton class AND
47// only create one copy/instance of the bitmap data no matter how many times a
48// resource header file is included.
49//
50// The Plan:
51// - Define a base resource class
52// - attributes as public instance vars
53// - Define a constructor that init's the instance vars
54// - Resource data access via virtual accessor - make it const
55//
56// - Create a template that subclasses the resource class, and defines a singleton
57// - It ensures only once of these classes is ever created.
58//
59// - In a separate header file - one for each resource, define the resource
60// - For example - for a bitmap - the data array, #defines for width and height
61// - The data array is declared as const static
62//
63// - In another header file - create a subclass of our singleton
64// ex:
65// class QwBMPTruck final : public bitmapSingleton<QwBMPTruck> {
66//
67// - Override the base class data accessor method
68// - In the body of this method, include the resource defined header file
69// - This defines the resource data as a static in this method. So only one copy is made
70// ex:
71// const uint8_t * bitmap(void){
72// #include "bmp_truck.h"
73// return bmp_truck_data;
74// }
75//
76// - After this resource access method, define the constructor of this class
77// - This is done after so it can use the attribute #defines of the included resource def header
78// - Have the constructor call it's superclass constructor, passing in resource attributes
79// ex:
80// QwBMPTruck(): bitmapSingleton<QwBMPTruck>(BMP_TRUCK_WIDTH, BMP_TRUCK_HEIGHT){}
81//
82// - Lastly, define a macro that makes the syntax of access easy. This macro calls the instance
83// method of the singleton, getting access to the object that contains the data of the resource
84// ex:
85// #define QW_EP_BMP_TRUCK QwBMPTruck::instance()
86//
87// To the user, they just have a bitmap, referenced as QW_EP_BMP_TRUCK and can do the following
88// ex:
89// uint8_t width = QW_EP_BMP_TRUCK.width;
90// uint8_t height = QW_EP_BMP_TRUCK.height;
91//
92// And internal methods get the data of this bitmap object using the bitmap() method
93// ex:
94// const uint8_t * pData = QW_EP_BMP_TRUCK.data();
95//
96// It sounds complicated - it isn't. Just look at examples in this folder and copy when
97// adding new resources.
98//
100// Simple Bitmap class definition
101
103
104public:
105 uint8_t width;
106 uint8_t height;
107 virtual const uint8_t* data(void) { return nullptr; };
108
109protected:
110 QwEpBitmap(uint8_t w, uint8_t h)
111 : width { w }
112 , height { h }
113 {
114 }
115};
116
117// Template that creates a singleton for bitmaps.
118template <typename T>
120public:
121 static T& instance(void)
122 {
123 static T instance;
124 return instance;
125 }
126
129
130protected:
132 using QwEpBitmap::QwEpBitmap; // inherit constructor
133};
134
136// Font things - class to hold font attributes
137
138class QwEpFont {
139
140public:
141 uint8_t width;
142 uint8_t height;
143 uint8_t start;
144 uint8_t n_chars;
145 uint16_t map_width;
146 const char* name;
147
148 virtual const uint8_t* data(void) { return nullptr; };
149
150protected:
151 QwEpFont(uint8_t w, uint8_t h, uint8_t st_chr, uint8_t n_chr, uint16_t m_w, const char* f_name)
152 : width { w }
153 , height { h }
154 , start { st_chr }
155 , n_chars { n_chr }
156 , map_width { m_w }
157 , name { f_name }
158 {
159 }
160};
161
162// Template that creates a singleton for bitmaps.
163template <typename T>
164class fontEpSingleton : public QwEpFont {
165public:
166 static T& instance(void)
167 {
168 static T instance;
169 return instance;
170 }
171
174
175protected:
177 using QwEpFont::QwEpFont; // inherit constructor
178};
Definition: qwiic_resdef.h:102
uint8_t width
Definition: qwiic_resdef.h:105
QwEpBitmap(uint8_t w, uint8_t h)
Definition: qwiic_resdef.h:110
virtual const uint8_t * data(void)
Definition: qwiic_resdef.h:107
uint8_t height
Definition: qwiic_resdef.h:106
Definition: qwiic_resdef.h:138
const char * name
Definition: qwiic_resdef.h:146
uint16_t map_width
Definition: qwiic_resdef.h:145
uint8_t start
Definition: qwiic_resdef.h:143
virtual const uint8_t * data(void)
Definition: qwiic_resdef.h:148
uint8_t n_chars
Definition: qwiic_resdef.h:144
QwEpFont(uint8_t w, uint8_t h, uint8_t st_chr, uint8_t n_chr, uint16_t m_w, const char *f_name)
Definition: qwiic_resdef.h:151
uint8_t width
Definition: qwiic_resdef.h:141
uint8_t height
Definition: qwiic_resdef.h:142
Definition: qwiic_resdef.h:119
bmpEpSingleton & operator=(const bmpEpSingleton)=delete
bmpEpSingleton()
Definition: qwiic_resdef.h:131
bmpEpSingleton(const bmpEpSingleton &)=delete
static T & instance(void)
Definition: qwiic_resdef.h:121
Definition: qwiic_resdef.h:164
static T & instance(void)
Definition: qwiic_resdef.h:166
fontEpSingleton()
Definition: qwiic_resdef.h:176
fontEpSingleton & operator=(const fontEpSingleton)=delete
fontEpSingleton(const fontEpSingleton &)=delete