NetBurner 3.5.6
PDF Version
qwiic_resdef.h
1// qwiic_resdef.h
2//
3// This is a library written for SparkFun Qwiic OLED boards that use the SSD1306.
4//
5// SparkFun sells these at its website: www.sparkfun.com
6//
7// Do you like this library? Help support SparkFun. Buy a board!
8//
9// Micro OLED https://www.sparkfun.com/products/14532
10// Transparent OLED https://www.sparkfun.com/products/15173
11// "Narrow" OLED https://www.sparkfun.com/products/17153
12//
13//
14// Written by Kirk Benell @ SparkFun Electronics, March 2022
15//
16// This library configures and draws graphics to OLED boards that use the
17// SSD1306 display hardware. The library only supports I2C.
18//
19// Repository:
20// https://github.com/sparkfun/SparkFun_Qwiic_OLED_Arduino_Library
21//
22// Documentation:
23// https://sparkfun.github.io/SparkFun_Qwiic_OLED_Arduino_Library/
24//
25//
26// SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).
27//
28// SPDX-License-Identifier: MIT
29//
30// The MIT License (MIT)
31//
32// Copyright (c) 2022 SparkFun Electronics
33// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
34// associated documentation files (the "Software"), to deal in the Software without restriction,
35// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
36// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to
37// do so, subject to the following conditions:
38// The above copyright notice and this permission notice shall be included in all copies or substantial
39// portions of the Software.
40// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
41// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
42// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
43// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
44// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45
46//
47// Define resource objects/structures to manage fonts and bitmaps
48
49//
50#pragma once
51
52// NOTE: The way I'm including the font/resource defines, some platforms have
53// include file issues
54
55#include "qw_pgm_arduino.h"
56
57#include <stdint.h>
58
60// Notes Template singletons for managing resources
62//
63// The common use/storage of bitmap and font data is creating a static
64// array and placing it in a header file.
65//
66// This pattern is fine for simple uses, where the bitmap is only included
67// in a single file. BUT if the bitmap header is included in multiple files,
68// multiple copies of the bitmap are created.
69//
70// You could just make the bitmap array a const - but then multiple includes
71// will lead to symbol collision at link time - confusing the user.
72//
73// These simple classes help place the bitmap data in a singleton class AND
74// only create one copy/instance of the bitmap data no matter how many times a
75// resource header file is included.
76//
77// The Plan:
78// - Define a base resource class
79// - attributes as public instance vars
80// - Define a constructor that init's the instance vars
81// - Resource data access via virtual accessor - make it const
82//
83// - Create a template that subclasses the resource class, and defines a singleton
84// - It ensures only once of these classes is ever created.
85//
86// - In a seperate header file - one for each resource, define the resource
87// - For example - for a bitmap - the data array, #defines for width and height
88// - The data array is declard as const static
89//
90// - In another header file - create a subclass of our singleton
91// ex:
92// class QwBMPTruck final : public bitmapSingleton<QwBMPTruck> {
93//
94// - Override the base class data accessor method
95// - In the body of this method, include the resource defined header file
96// - This defines the resource data as a static in this method. So only one copy is made
97// ex:
98// const uint8_t * bitmap(void){
99// #include "bmp_truck.h"
100// return bmp_truck_data;
101// }
102//
103// - After this resource access method, define the constructor of this class
104// - This is done after so it can use the attribute #defines of the included resource def header
105// - Have the constructor call it's superclass constructor, passing in resource attributes
106// ex:
107// QwBMPTruck(): bitmapSingleton<QwBMPTruck>(BMP_TRUCK_WIDTH, BMP_TRUCK_HEIGHT){}
108//
109// - Lastly, define a macro that makes the syntax of access easy. This macro calls the instance
110// method of the singleton, getting access to the object that contains the data of the resource
111// ex:
112// #define QW_BMP_TRUCK QwBMPTruck::instance()
113//
114// To the user, they just have a bitmap, referenced as QW_BMP_TRUCK and can do the following
115// ex:
116// uint8_t width = QW_BMP_TRUCK.width;
117// uint8_t height = QW_BMP_TRUCK.height;
118//
119// And internal methods get the data of this bitmap object using the bitmap() method
120// ex:
121// const uint8_t * pData = QW_BMP_TRUCK.data();
122//
123// It shoulds complicated - it isn't. Just look at examples in ths folder and copy when
124// adding new resources.
125//
127// Simple Bitmap class definition
128
129class QwBitmap {
130
131public:
132 uint8_t width;
133 uint8_t height;
134 virtual const uint8_t* data(void) { return nullptr; };
135
136protected:
137 QwBitmap(uint8_t w, uint8_t h)
138 : width { w }
139 , height { h }
140 {
141 }
142};
143
144// Template that creates a singleton for bitmaps.
145template <typename T>
146class bmpSingleton : public QwBitmap {
147public:
148 static T& instance(void)
149 {
150 static T instance;
151 return instance;
152 }
153
154 bmpSingleton(const bmpSingleton&) = delete;
155 bmpSingleton& operator=(const bmpSingleton) = delete;
156
157protected:
158 bmpSingleton() { }
159 using QwBitmap::QwBitmap; // inherit contructor
160};
161
163// Font things - class to hold font attributes
164
165class QwFont {
166
167public:
168 uint8_t width;
169 uint8_t height;
170 uint8_t start;
171 uint8_t n_chars;
172 uint16_t map_width;
173 const char* name;
174
175 virtual const uint8_t* data(void) { return nullptr; };
176
177protected:
178 QwFont(uint8_t w, uint8_t h, uint8_t st_chr, uint8_t n_chr, uint16_t m_w, const char* f_name)
179 : width { w }
180 , height { h }
181 , start { st_chr }
182 , n_chars { n_chr }
183 , map_width { m_w }
184 , name { f_name }
185 {
186 }
187};
188
189// Template that creates a singleton for bitmaps.
190template <typename T>
191class fontSingleton : public QwFont {
192public:
193 static T& instance(void)
194 {
195 static T instance;
196 return instance;
197 }
198
199 fontSingleton(const fontSingleton&) = delete;
200 fontSingleton& operator=(const fontSingleton) = delete;
201
202protected:
203 fontSingleton() { }
204 using QwFont::QwFont; // inherit constructor
205};