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:
43
hal/gpio.cpp
Normal file
43
hal/gpio.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "gpio.hpp"
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
GPIO::GPIO(int pin) : _pin(pin), _exported(false) { _export(); }
|
||||
|
||||
GPIO::~GPIO() { _unexport(); }
|
||||
|
||||
void GPIO::_export()
|
||||
{
|
||||
std::ofstream exp("/sys/class/gpio/export");
|
||||
if (exp.is_open()) { exp << _pin; _exported = true; }
|
||||
}
|
||||
|
||||
void GPIO::_unexport()
|
||||
{
|
||||
std::ofstream unexp("/sys/class/gpio/unexport");
|
||||
if (unexp.is_open()) unexp << _pin;
|
||||
}
|
||||
|
||||
void GPIO::setDirection(const std::string& dir)
|
||||
{
|
||||
std::string path = "/sys/class/gpio/gpio" + std::to_string(_pin) + "/direction";
|
||||
std::ofstream f(path);
|
||||
if (f.is_open()) f << dir;
|
||||
}
|
||||
|
||||
void GPIO::setValue(int val)
|
||||
{
|
||||
std::string path = "/sys/class/gpio/gpio" + std::to_string(_pin) + "/value";
|
||||
std::ofstream f(path);
|
||||
if (f.is_open()) f << val;
|
||||
}
|
||||
|
||||
int GPIO::getValue() const
|
||||
{
|
||||
std::string path = "/sys/class/gpio/gpio" + std::to_string(_pin) + "/value";
|
||||
std::ifstream f(path);
|
||||
int v = 0; if (f.is_open()) f >> v;
|
||||
return v;
|
||||
}
|
||||
Reference in New Issue
Block a user