Initial commit: SmartCar Framework v0.1 — 龙芯2K0300智能车开发框架\n\n- HAL: GPIO/PWM/Encoder/Framebuffer 驱动\n- Control: PID/IMU/Motor/Servo 控制\n- Vision: HSV双Otsu→4点标定IPM→洪泛填充→逐行搜线\n- Strategy: 三区前瞻偏差+速度策略\n- Debug: 文件热调参+LCD预览+cv截帧\n- Scheduler: 5ms timerfd+epoll 中央调度器
This commit is contained in:
60
hal/framebuffer.cpp
Normal file
60
hal/framebuffer.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "framebuffer.hpp"
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/fb.h>
|
||||
#include <cstring>
|
||||
|
||||
FrameBuffer::FrameBuffer() : _fd(-1), _buffer(nullptr), _width(0), _height(0), _screensize(0) {}
|
||||
|
||||
FrameBuffer::~FrameBuffer() { release(); }
|
||||
|
||||
bool FrameBuffer::init(const char* device)
|
||||
{
|
||||
_fd = open(device, O_RDWR);
|
||||
if (_fd < 0) return false;
|
||||
|
||||
fb_var_screeninfo vinfo;
|
||||
if (ioctl(_fd, FBIOGET_VSCREENINFO, &vinfo) < 0) return false;
|
||||
|
||||
_width = vinfo.xres;
|
||||
_height = vinfo.yres;
|
||||
_screensize = _width * _height * 2; // RGB565 = 2 bytes/pixel
|
||||
|
||||
_buffer = (uint16*)mmap(nullptr, _screensize, PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED, _fd, 0);
|
||||
return (_buffer != MAP_FAILED);
|
||||
}
|
||||
|
||||
void FrameBuffer::release()
|
||||
{
|
||||
if (_buffer && _buffer != MAP_FAILED)
|
||||
munmap(_buffer, _screensize);
|
||||
if (_fd >= 0) { close(_fd); _fd = -1; }
|
||||
_buffer = nullptr;
|
||||
}
|
||||
|
||||
void FrameBuffer::_rgb888_to_rgb565(const uint8* src, uint16* dst, int pixels)
|
||||
{
|
||||
for (int i = 0; i < pixels; ++i)
|
||||
{
|
||||
uint8 r = src[i * 3];
|
||||
uint8 g = src[i * 3 + 1];
|
||||
uint8 b = src[i * 3 + 2];
|
||||
dst[i] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
|
||||
}
|
||||
}
|
||||
|
||||
bool FrameBuffer::write(const uint8* rgb888_data, int w, int h)
|
||||
{
|
||||
if (!_buffer || _buffer == MAP_FAILED) return false;
|
||||
|
||||
int copy_w = (w < _width) ? w : _width;
|
||||
int copy_h = (h < _height) ? h : _height;
|
||||
|
||||
for (int y = 0; y < copy_h; ++y)
|
||||
_rgb888_to_rgb565(rgb888_data + y * w * 3, _buffer + y * _width, copy_w);
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user