Macros.h
Go to the documentation of this file.
1 /****
2  * Macros.h
3  *
4  * Copyright 2021 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming-Graphics Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: May 2021 - mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "Header.h"
25 
26 /*
27  * Set of macros to allow construction of drawings as uint8_t[] arrays:
28  *
29  * DEFINE_FSTR_ARRAY(myDrawing, uint8_t,
30  * GDRAW_RESET()
31  * GDRAW_PENWIDTH(3)
32  * GDRAW_XABS(10)
33  * GDRAW_YABS(10)
34  * GDRAW_COLOR(Color::Red)
35  * GDRAW_CIRCLE(50)
36  * GDRAW_XREL(100)
37  * GDRAW_COLOR(Color::Green)
38  * GDRAW_FILLCIRCLE(50)
39  * )
40  */
41 
42 #define GDRAW_FIELD(value, shift) uint8_t(uint8_t(Graphics::Drawing::value) << shift)
43 #define GDRAW_OPCODE(opcode) GDRAW_FIELD(opcode, 6)
44 #define GDRAW_TYPE(type) GDRAW_FIELD(Header::type, 4)
45 #define GDRAW_CMD(cmd) GDRAW_OPCODE(OpCode::execute) | GDRAW_TYPE(Type::uint8) | uint8_t(Graphics::Drawing::cmd),
46 #define GDRAW_UINT16(value) uint8_t(value), uint8_t((value) >> 8),
47 #define GDRAW_INT16(value) uint8_t(abs(value)), uint8_t(abs(value) >> 8),
48 #define GDRAW_UINT32(value) uint8_t(value), uint8_t((value) >> 8), uint8_t((value) >> 16), uint8_t((value) >> 24),
49 #define GDRAW_MAKE_UINT32(w1, w2) (uint32_t(w2) << 16) | uint32_t(w1)
50 #define GDRAW_REGDEF(reg, size) uint8_t(offsetof(Graphics::Drawing::Registers, reg) / size)
51 #define GDRAW_REG_UINT8(opcode, reg, value) \
52  GDRAW_OPCODE(opcode) | GDRAW_TYPE(Type::uint8) | GDRAW_REGDEF(reg, 1), uint8_t(value),
53 #define GDRAW_REG_UINT16(opcode, reg, value) \
54  GDRAW_OPCODE(opcode) | GDRAW_TYPE(Type::uint16) | GDRAW_REGDEF(reg, 2), GDRAW_UINT16(value)
55 #define GDRAW_REG_INT16(reg, value) \
56  ((value) < 0 ? GDRAW_OPCODE(OpCode::sub) : GDRAW_OPCODE(OpCode::add)) | GDRAW_TYPE(Type::uint16) | \
57  GDRAW_REGDEF(reg, 2), \
58  GDRAW_INT16(value)
59 #define GDRAW_REG_UINT32(opcode, reg, value) \
60  GDRAW_OPCODE(opcode) | GDRAW_TYPE(Type::uint32) | GDRAW_REGDEF(reg, 4), GDRAW_UINT32(value)
61 #define GDRAW_RESET() GDRAW_CMD(Command::reset)
62 #define GDRAW_SAVE() GDRAW_CMD(Command::push)
63 #define GDRAW_RESTORE() GDRAW_CMD(Command::pop)
64 #define GDRAW_XREL(cx) GDRAW_REG_INT16(x2, cx)
65 #define GDRAW_YREL(cy) GDRAW_REG_INT16(y2, cy)
66 #define GDRAW_XABS(x_) GDRAW_REG_UINT16(OpCode::store, x2, x_)
67 #define GDRAW_YABS(y_) GDRAW_REG_UINT16(OpCode::store, y2, y_)
68 #define GDRAW_ID(id_) GDRAW_REG_UINT16(OpCode::store, id, id_)
69 #define GDRAW_SELECT_PEN(id) GDRAW_REG_UINT16(OpCode::store, penId, id)
70 #define GDRAW_SELECT_BRUSH(id) GDRAW_REG_UINT16(OpCode::store, brushId, id)
71 #define GDRAW_SELECT_TEXT(id) GDRAW_REG_UINT16(OpCode::store, textId, id)
72 #define GDRAW_OFFSET_LENGTH(off, len) GDRAW_REG_UINT32(OpCode::store, length, GDRAW_MAKE_UINT32(len, off))
73 #define GDRAW_PEN_COLOR(col) GDRAW_REG_UINT32(OpCode::store, penColor, uint32_t(col))
74 #define GDRAW_PEN_WIDTH(width) GDRAW_REG_UINT16(OpCode::store, penWidth, width)
75 #define GDRAW_STORE_PEN(id) GDRAW_ID(id) GDRAW_CMD(Command::storePen)
76 #define GDRAW_BRUSH_COLOR(col) GDRAW_REG_UINT32(OpCode::store, brushColor, uint32_t(col))
77 #define GDRAW_STORE_BRUSH(id) GDRAW_ID(id) GDRAW_CMD(Command::storeBrush)
78 #define GDRAW_END_ANGLE(value) GDRAW_REG_UINT16(OpCode::store, endAngle, value) GDRAW_CMD(Command::angle)
79 #define GDRAW_MOVE() GDRAW_CMD(Command::move)
80 #define GDRAW_LINE() GDRAW_CMD(Command::line)
81 #define GDRAW_LINE_TO() GDRAW_CMD(Command::lineto)
82 #define GDRAW_RADIUS(value) GDRAW_REG_UINT16(OpCode::store, radius, value)
83 #define GDRAW_RECT(radius) GDRAW_RADIUS(radius) GDRAW_CMD(Command::drawRect)
84 #define GDRAW_FILL_RECT(radius) GDRAW_RADIUS(radius) GDRAW_CMD(Command::fillRect)
85 #define GDRAW_ELLIPSE() GDRAW_CMD(Command::drawEllipse)
86 #define GDRAW_FILL_ELLIPSE() GDRAW_CMD(Command::fillEllipse)
87 #define GDRAW_START_ANGLE(angle) GDRAW_REG_UINT16(OpCode::store, startAngle, value)
88 #define GDRAW_ANGLE(angle) GDRAW_REG_UINT16(OpCode::store, angle, value)
89 #define GDRAW_ARC(startAngle) GDRAW_START_ANGLE(startAngle) GDRAW_CMD(Command::drawArc)
90 #define GDRAW_FILL_ARC(startAngle) GDRAW_START_ANGLE(startAngle) GDRAW_CMD(Command::fillArc)
91 #define GDRAW_CIRCLE(radius) GDRAW_RADIUS(radius) GDRAW_CMD(Command::drawCircle)
92 #define GDRAW_FILL_CIRCLE(radius) GDRAW_RADIUS(radius) GDRAW_CMD(Command::fillCircle)
93 #define GDRAW_BEGIN_SUB(id) GDRAW_ID(id) GDRAW_CMD(Command::beginSub)
94 #define GDRAW_END_SUB() GDRAW_CMD(Command::endSub)
95 #define GDRAW_CALL(id) GDRAW_ID(id) GDRAW_CMD(Command::call)
96 #define GDRAW_RESOURCE(id, dataType, kind) \
97  GDRAW_ID(id) \
98  GDRAW_OPCODE(OpCode::store) | GDRAW_TYPE(Type::resource) | GDRAW_FIELD(Header::dataType, 2) | \
99  GDRAW_FIELD(Header::LengthSize::uint8, 1) | GDRAW_FIELD(Header::kind, 0)
100 #define GDRAW_DEFINE_CHARS(id, len, ...) GDRAW_RESOURCE(id, DataType::charArray, ResourceKind::text), len, __VA_ARGS__,
101 #define GDRAW_DRAW_CHARS(len, ...) \
102  GDRAW_DEFINE_CHARS(0, len, __VA_ARGS__) GDRAW_SELECT_TEXT(0) GDRAW_CMD(Command::drawText)
103 #define GDRAW_DRAW_TEXT(id) GDRAW_SELECT_TEXT(id) GDRAW_CMD(Command::drawText)
104 #define GDRAW_FONT_STYLE(font_id, style_) \
105  GDRAW_REG_UINT32(OpCode::store, style, GDRAW_MAKE_UINT32(uint32_t(style_), font_id))