libyggdrasil  v1.0.0
display.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Asgard - *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10  * This software can be used by students and other personal of the *
11  * Bern University of Applied Sciences under the terms of the MIT *
12  * license. *
13  * For other persons this software is under the terms of the GNU *
14  * General Public License version 2. *
15  * *
16  * Copyright © 2021, Bern University of Applied Sciences. *
17  * All rights reserved. *
18  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #pragma once
27 
29 
30 #include <cmath>
31 
32 #include <fcntl.h>
33 #include <linux/fb.h>
34 #include <sys/mman.h>
35 #include <sys/ioctl.h>
36 
37 namespace bsp::drv {
38 
39  enum class Color : u16 {
40  Black = 0b00000'000000'00000,
41  Navy = 0b00000'000000'10000,
42  Blue = 0b00000'000000'11000,
43  Green = 0b00000'011000'00000,
44  Teal = 0b00000'010000'01000,
45  Lime = 0b00000'111000'00000,
46  Aqua = 0b00000'111000'11000,
47  Maroon = 0b01100'000000'00000,
48  Purple = 0b01100'000000'11000,
49  Olive = 0b01100'011000'00000,
50  Gray = 0b01000'010000'01000,
51  Red = 0b11100'000000'00000,
52  Fuchsia = 0b11100'000000'11000,
53  Yellow = 0b11100'111000'00000,
54  Orange = 0b11100'100000'00000,
55  White = 0b11100'111000'11000,
56  };
57 
58 }
59 
60 namespace bsp::asg::drv {
61 
68  template<auto Context>
69  struct Display {
70  Display(const Display&) = delete;
71 
72  /*
73  * @brief Display initialization
74  *
75  * @param orientation Display orientation
76  * @return Success
77  */
78  static bool init() noexcept {
79  int fd = open("/dev/fb0", O_RDWR);
80  if (fd == -1) return false;
81 
82  ON_SCOPE_EXIT { close(fd); };
83 
84  if (ioctl(fd, FBIOGET_FSCREENINFO, &Display::s_finfo) == -1) return false;
85 
86  if (ioctl(fd, FBIOGET_VSCREENINFO, &Display::s_vinfo) == -1) return false;
87 
88  Display::s_xSize = Display::s_vinfo.xres;
89  Display::s_ySize = Display::s_vinfo.yres;
90  Display::s_bpp = Display::s_vinfo.bits_per_pixel;
91 
92  Display::s_framebufferAddress = (u32)mmap(nullptr, Display::s_finfo.line_length * Display::s_vinfo.yres, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
93  if (Display::s_framebufferAddress == 0xFFFF'FFFF) return false;
94 
95  Display::clear(bsp::drv::Color::Black);
96  Display::turnOn();
97 
98  return true;
99  }
100 
106  static bool deinit() {
107  munmap(getFramebufferAddress(), Display::s_xSize * Display::s_ySize * Display::s_bpp / 8);
108  return true;
109  }
110 
111  /*
112  * @brief Display reset
113  */
114  static void reset() noexcept {
115 
116  }
117 
118  /*
119  * @brief Enable the display
120  */
121  static void turnOn() noexcept {
122 
123  }
124 
125  /*
126  * @brief Disable the display
127  */
128  static void turnOff() noexcept {
129 
130  }
131 
132  /*
133  * @brief Get the display width
134  *
135  * @return display width
136  */
137  static inline u16 getWidth() {
138  return Display::s_xSize;
139  }
140 
141  /*
142  * @brief Get the display height
143  *
144  * @return display height
145  */
146  static inline u16 getHeight() {
147  return Display::s_ySize;
148  }
149 
150  /*
151  * @brief Set the color palette
152  *
153  * @param palettet Color palette
154  */
155  static inline void setPalette(const std::array<u32, 256> &palette) {
156 
157  }
158 
159  /*
160  * @brief Get the default color palette
161  *
162  * @return Default color palette
163  */
164  static std::array<u32, 256> getDefaultPalette() {
165  return { };
166  }
167 
168  /*
169  * @brief Get the framebuffer address
170  *
171  * @return framebuffer address
172  */
173  ALWAYS_INLINE static void* getFramebufferAddress() {
174  return reinterpret_cast<void*>(Display::s_framebufferAddress);
175  }
176 
177  /*
178  * @brief Set a Pixel
179  *
180  * @param x X coordinate
181  * @param y Y coordinate
182  * @param color Index for the color
183  */
184  ALWAYS_INLINE static void setPixel(u16 x, u16 y, u32 color) {
185  u32 offset = (x + Display::s_vinfo.xoffset) * (Display::s_bpp / 8) + (y + Display::s_vinfo.yoffset) * Display::s_finfo.line_length;
186 
187  std::memcpy(reinterpret_cast<u8*>(getFramebufferAddress()) + offset, &color, Display::s_bpp / 8);
188  }
189 
190 
196  ALWAYS_INLINE static void clear(auto color) {
197  for (u32 x = 0; x < Display::s_xSize; x++)
198  for (u32 y = 0; y < Display::s_ySize; y++)
199  Display::setPixel(x, y, static_cast<u32>(color));
200  }
201 
202  private:
203  Display() = default;
204 
205  static inline u32 s_framebufferAddress;
206 
207  static inline u16 s_xSize, s_ySize;
208  static inline u8 s_bpp;
209 
210  static inline fb_fix_screeninfo s_finfo;
211  static inline fb_var_screeninfo s_vinfo;
212 
216  template<auto, template<auto> typename>
217  friend struct bsp::drv::Display;
218  };
219 
220 }
bsp::asg::drv::Display::Display
Display(const Display &)=delete
bsp::asg::drv
Definition: display.hpp:60
u16
uint16_t u16
Definition: types.h:37
bsp::drv::Color::Black
@ Black
display.hpp
Frontend for the Display abstraction.
ON_SCOPE_EXIT
#define ON_SCOPE_EXIT
Scope Guard for always executing code when exiting the current scope.
Definition: utils.hpp:64
bsp::drv::Color
Color
Definition: display.hpp:39
u32
uint32_t u32
Definition: types.h:38
bsp::asg::drv::Display::init
static bool init() noexcept
Definition: display.hpp:78
bsp::asg::drv::Display
Display Channel implementation for Asgard.
Definition: display.hpp:69
bsp::drv
Definition: display.hpp:37