dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
osd_utils.h
Go to the documentation of this file.
1 
16 /*
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful, but
23  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25  * for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, see <http://www.gnu.org/licenses/>
29  */
30 
31 #ifndef OSDUTILS_H
32 #define OSDUTILS_H
33 
34 #include "openpilot.h"
35 #include "fonts.h"
36 #include "images.h"
37 #include "misc_math.h"
38 #include "pios_video.h"
39 
40 // Size of an array (num items.)
41 #define SIZEOF_ARRAY(x) (sizeof(x) / sizeof((x)[0]))
42 
43 #define HUD_VSCALE_FLAG_CLEAR 1
44 #define HUD_VSCALE_FLAG_NO_NEGATIVE 2
45 
46 #if defined(PIOS_VIDEO_SPLITBUFFER)
47 #define PIXELS_PER_BIT 8
48 #define CALC_BIT_IN_WORD(x) ((x) & 7)
49 #define CALC_BIT_MASK(x) (1 << (7 - ((x) & 7)))
50 // Horizontal line calculations.
51 // Edge cases.
52 #define COMPUTE_HLINE_EDGE_L_MASK(b) ((1 << (8 - (b))) - 1)
53 #define COMPUTE_HLINE_EDGE_R_MASK(b) (~((1 << (7 - (b))) - 1))
54 #else
55 #if PIOS_VIDEO_BITS_PER_PIXEL != 2
56 #error "Only 2 bits / pixel is currently supported"
57 #endif
58 #define PIXELS_PER_BIT (8 / PIOS_VIDEO_BITS_PER_PIXEL)
59 #define CALC_BIT_IN_WORD(x) (2 * ((x) & 3))
60 #define CALC_BITSHIFT_WORD(x) (2 * ((x) & 3))
61 #define CALC_BIT_MASK(x) (3 << (6 - CALC_BITSHIFT_WORD(x)))
62 #define PACK_BITS(mask, level) (level << 7 | mask << 6 | level << 5 | mask << 4 | level << 3 | mask << 2 | level << 1 | mask)
63 #define CALC_BIT0_IN_WORD(x) (2 * ((x) & 3))
64 #define CALC_BIT1_IN_WORD(x) (2 * ((x) & 3) + 1)
65 // Horizontal line calculations.
66 // Edge cases.
67 #define COMPUTE_HLINE_EDGE_L_MASK(b) ((1 << (7 - (b))) - 1)
68 #define COMPUTE_HLINE_EDGE_R_MASK(b) (~((1 << (6 - (b))) - 1))
69 #endif /* defined(PIOS_VIDEO_SPLITBUFFER) */
70 
71 // Macros for computing addresses and bit positions.
72 #define CALC_BUFF_ADDR(x, y) (((x) / PIXELS_PER_BIT) + ((y) * BUFFER_WIDTH))
73 #define DEBUG_DELAY
74 // Macro for writing a word with a mode (NAND = clear, OR = set, XOR = toggle)
75 // at a given position
76 #define WRITE_WORD_MODE(buff, addr, mask, mode) \
77  switch (mode) { \
78  case 0: buff[addr] &= ~mask; break; \
79  case 1: buff[addr] |= mask; break; \
80  case 2: buff[addr] ^= mask; break; }
81 
82 #define WRITE_WORD_NAND(buff, addr, mask) { buff[addr] &= ~mask; DEBUG_DELAY; }
83 #define WRITE_WORD_OR(buff, addr, mask) { buff[addr] |= mask; DEBUG_DELAY; }
84 #define WRITE_WORD_XOR(buff, addr, mask) { buff[addr] ^= mask; DEBUG_DELAY; }
85 #define WRITE_WORD(buff, addr, mask, value) { buff[addr] = (buff[addr] & ~mask) | (value & mask);}
86 
87 
88 // This computes an island mask.
89 #define COMPUTE_HLINE_ISLAND_MASK(b0, b1) (COMPUTE_HLINE_EDGE_L_MASK(b0) ^ COMPUTE_HLINE_EDGE_L_MASK(b1));
90 
91 // Macro for initializing stroke/fill modes. Add new modes here
92 // if necessary.
93 #define SETUP_STROKE_FILL(stroke, fill, mode) \
94  stroke = 0; fill = 0; \
95  if (mode == 0) { stroke = 0; fill = 1; } \
96  if (mode == 1) { stroke = 1; fill = 0; } \
97 
98 // Line endcaps (for horizontal and vertical lines.)
99 #define ENDCAP_NONE 0
100 #define ENDCAP_ROUND 1
101 #define ENDCAP_FLAT 2
102 
103 #define DRAW_ENDCAP_HLINE(e, x, y, s, f, l) \
104  if ((e) == ENDCAP_ROUND) /* single pixel endcap */ \
105 { write_pixel_lm(x, y, f, l); } \
106  else if ((e) == ENDCAP_FLAT) /* flat endcap: FIXME, quicker to draw a vertical line(?) */ \
107 { write_pixel_lm(x, y - 1, s, l); write_pixel_lm(x, y, s, l); write_pixel_lm(x, y + 1, s, l); }
108 
109 #define DRAW_ENDCAP_VLINE(e, x, y, s, f, l) \
110  if ((e) == ENDCAP_ROUND) /* single pixel endcap */ \
111 { write_pixel_lm(x, y, f, l); } \
112  else if ((e) == ENDCAP_FLAT) /* flat endcap: FIXME, quicker to draw a horizontal line(?) */ \
113 { write_pixel_lm(x - 1, y, s, l); write_pixel_lm(x, y, s, l); write_pixel_lm(x + 1, y, s, l); }
114 
115 // Macros for writing pixels in a midpoint circle algorithm.
116 #define CIRCLE_PLOT_8(buff, cx, cy, x, y, mode) \
117  CIRCLE_PLOT_4(buff, cx, cy, x, y, mode); \
118  if ((x) != (y)) { CIRCLE_PLOT_4(buff, cx, cy, y, x, mode); }
119 
120 #define CIRCLE_PLOT_4(buff, cx, cy, x, y, mode) \
121  write_pixel(buff, (cx) + (x), (cy) + (y), mode); \
122  write_pixel(buff, (cx) - (x), (cy) + (y), mode); \
123  write_pixel(buff, (cx) + (x), (cy) - (y), mode); \
124  write_pixel(buff, (cx) - (x), (cy) - (y), mode);
125 
126 // Font flags.
127 #define FONT_BOLD 1 // bold text (no outline)
128 #define FONT_INVERT 2 // invert: border white, inside black
129 // Text alignments.
130 #define TEXT_VA_TOP 0
131 #define TEXT_VA_MIDDLE 1
132 #define TEXT_VA_BOTTOM 2
133 #define TEXT_HA_LEFT 0
134 #define TEXT_HA_CENTER 1
135 #define TEXT_HA_RIGHT 2
136 
137 // Max/Min macros
138 #define MAX3(a, b, c) MAX(a, MAX(b, c))
139 #define MIN3(a, b, c) MIN(a, MIN(b, c))
140 #define LIMIT(x, l, h) MAX(l, MIN(x, h))
141 
142 // Check if coordinates are valid. If not, return. Assumes signed coordinates for working correct also with values lesser than 0.
143 #define CHECK_COORDS(x, y) if (x < GRAPHICS_LEFT || x > GRAPHICS_RIGHT || y < GRAPHICS_TOP || y > GRAPHICS_BOTTOM) { return; }
144 #define CHECK_COORD_X(x) if (x < GRAPHICS_LEFT || x > GRAPHICS_RIGHT) { return; }
145 #define CHECK_COORD_Y(y) if (y < GRAPHICS_TOP || y > GRAPHICS_BOTTOM) { return; }
146 
147 // Clip coordinates out of range. Assumes signed coordinates for working correct also with values lesser than 0.
148 #define CLIP_COORDS(x, y) { CLIP_COORD_X(x); CLIP_COORD_Y(y); }
149 #define CLIP_COORD_X(x) { x = x < GRAPHICS_LEFT ? GRAPHICS_LEFT : x > GRAPHICS_RIGHT ? GRAPHICS_RIGHT : x; }
150 #define CLIP_COORD_Y(y) { y = y < GRAPHICS_TOP ? GRAPHICS_TOP : y > GRAPHICS_BOTTOM ? GRAPHICS_BOTTOM : y; }
151 
152 // Macro to swap two variables using XOR swap.
153 #define SWAP(a, b) { a ^= b; b ^= a; a ^= b; }
154 
155 
156 // Text dimension structures.
158  int width, height;
159 };
160 
161 // Structure for a point
162 typedef struct {
163  int16_t x;
164  int16_t y;
165 } point_t;
166 
167 void clearGraphics();
168 void draw_image(uint16_t x, uint16_t y, const struct Image * image);
169 void plotFourQuadrants(int32_t centerX, int32_t centerY, int32_t deltaX, int32_t deltaY);
170 void ellipse(int centerX, int centerY, int horizontalRadius, int verticalRadius);
171 void drawArrow(uint16_t x, uint16_t y, uint16_t angle, uint16_t size_quarter);
172 void drawBox(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
173 void write_pixel_lm(int x, int y, int mmode, int lmode);
174 void write_hline_lm(int x0, int x1, int y, int lmode, int mmode);
175 void write_hline_outlined(int x0, int x1, int y, int endcap0, int endcap1, int mode, int mmode);
176 void write_vline_lm(int x, int y0, int y1, int lmode, int mmode);
177 void write_vline_outlined(int x, int y0, int y1, int endcap0, int endcap1, int mode, int mmode);
178 void write_filled_rectangle_lm(int x, int y, int width, int height, int lmode, int mmode);
179 void write_rectangle_outlined(int x, int y, int width, int height, int mode, int mmode);
180 void write_circle_outlined(int cx, int cy, int r, int dashp, int bmode, int mode, int mmode);
181 
182 void write_line_lm(int x0, int y0, int x1, int y1, int mmode, int lmode);
183 void write_line_outlined(int x0, int y0, int x1, int y1,
184  __attribute__((unused)) int endcap0, __attribute__((unused)) int endcap1,
185  int mode, int mmode);
186 void write_line_outlined_dashed(int x0, int y0, int x1, int y1,
187  __attribute__((unused)) int endcap0, __attribute__((unused)) int endcap1,
188  int mode, int mmode, int dots);
189 const struct FontEntry* get_font_info(int font);
190 void calc_text_dimensions(char *str, const struct FontEntry *font, int xs, int ys, struct FontDimensions *dim);
191 void write_string(char *str, int x, int y, int xs, int ys, int va, int ha, int flags, int font);
192 void draw_polygon(int16_t x, int16_t y, float angle, const point_t * points, uint8_t n_points, int mode, int mmode);
193 void lla_to_ned(int32_t lat, int32_t lon, float alt, float *NED);
194 #endif /* OSDUTILS_H */
195 
Definition: fonts.h:43
void write_string(char *str, int x, int y, int xs, int ys, int va, int ha, int flags, int font)
Definition: osd_utils.c:1344
void write_circle_outlined(int cx, int cy, int r, int dashp, int bmode, int mode, int mmode)
void draw_polygon(int16_t x, int16_t y, float angle, const point_t *points, uint8_t n_points, int mode, int mmode)
Definition: osd_utils.c:1396
void write_vline_lm(int x, int y0, int y1, int lmode, int mmode)
Definition: osd_utils.c:483
void write_vline_outlined(int x, int y0, int y1, int endcap0, int endcap1, int mode, int mmode)
Definition: osd_utils.c:508
void draw_image(uint16_t x, uint16_t y, const struct Image *image)
Definition: osd_utils.c:70
Definition: images.h:1074
OSD gen module, handles OSD draw. Parts from CL-OSD and SUPEROSD projects.
void write_rectangle_outlined(int x, int y, int width, int height, int mode, int mmode)
Definition: osd_utils.c:681
void write_line_outlined_dashed(int x0, int y0, int x1, int y1, __attribute__((unused)) int endcap0, __attribute__((unused)) int endcap1, int mode, int mmode, int dots)
Definition: osd_utils.c:1029
uint32_t lon
Definition: msp_messages.h:98
uint32_t lat
Definition: msp_messages.h:97
void write_line_outlined(int x0, int y0, int x1, int y1, __attribute__((unused)) int endcap0, __attribute__((unused)) int endcap1, int mode, int mmode)
Definition: osd_utils.c:945
uint16_t flags
Definition: uavtalk_priv.h:52
int16_t x
Definition: osd_utils.h:163
void calc_text_dimensions(char *str, const struct FontEntry *font, int xs, int ys, struct FontDimensions *dim)
Definition: osd_utils.c:1308
int16_t y
Definition: osd_utils.h:164
void write_line_lm(int x0, int y0, int x1, int y1, int mmode, int lmode)
Definition: osd_utils.c:922
struct _msp_pid_item alt
Definition: msp_messages.h:99
void plotFourQuadrants(int32_t centerX, int32_t centerY, int32_t deltaX, int32_t deltaY)
Definition: osd_utils.c:136
enum channel_mode mode
Definition: pios_servo.c:58
void write_hline_outlined(int x0, int x1, int y, int endcap0, int endcap1, int mode, int mmode)
Definition: osd_utils.c:397
void clearGraphics()
Definition: osd_utils.c:60
void drawBox(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
Definition: osd_utils.c:214
const struct FontEntry * get_font_info(int font)
Definition: osd_utils.c:1290
Includes PiOS and core architecture components.
uint8_t unused[4]
Definition: bl_messages.h:70
void ellipse(int centerX, int centerY, int horizontalRadius, int verticalRadius)
Definition: osd_utils.c:152
void drawArrow(uint16_t x, uint16_t y, uint16_t angle, uint16_t size_quarter)
Definition: osd_utils.c:199
void write_hline_lm(int x0, int x1, int y, int lmode, int mmode)
Definition: osd_utils.c:372
void write_filled_rectangle_lm(int x, int y, int width, int height, int lmode, int mmode)
Definition: osd_utils.c:659
void lla_to_ned(int32_t lat, int32_t lon, float alt, float *NED)
Definition: osd_utils.c:1447
void write_pixel_lm(int x, int y, int mmode, int lmode)
Definition: osd_utils.c:261
typedef __attribute__
Definition: serial_4way.h:43