2 Star 0 Fork 0

程序员锡哥/cgutech-jni-serial

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
RoadLightController.cpp 7.16 KB
一键复制 编辑 原始数据 按行查看 历史
devis 提交于 2019-07-11 16:22 . 增加初始化功能
//
// Created by root on 19-7-7.
//
#include "RoadLightController.h"
#include <json/json.h>
#include <fstream>
#include <iostream>
RoadLightController::RoadLightController(const char* config) {
auto params = init(config);
std::vector<LightParam> road1 = std::get<0>(params);
std::vector<LightParam> road2 = std::get<1>(params);
roadLightParam.insert(std::pair<std::string, std::vector<LightParam>>("road1", road1));
roadLightParam.insert(std::pair<std::string, std::vector<LightParam>>("road2", road2));
for(auto param : road1) {
SerialPortForBoost* serial = nullptr;
if(serials.find(param.dev) == serials.end()) {
serial = new SerialPortForBoost();
if(!serial->Open(param.dev.c_str(), 9600)) {
printf("打开串口[%s]失败\n", param.dev.c_str());
}
serials.insert(std::make_pair(param.dev, serial));
} else {
serial = serials.at(param.dev);
}
Light *light = new Light(serial, param.green, param.yellow, param.red);
road1Lights.push_back(light);
}
for(auto param : road2) {
SerialPortForBoost* serial = nullptr;
if(serials.find(param.dev) == serials.end()) {
serial = new SerialPortForBoost();
if(serial->Open(param.dev.c_str(), 9600)) {
printf("打开串口[%s]失败\n", param.dev.c_str());
}
serials.insert(std::make_pair(param.dev, serial));
} else {
serial = serials.at(param.dev);
}
printf("dev:%s, green:%d, yellow:%d, red:%d\n", param.dev.c_str(), param.green, param.yellow, param.red);
Light *light = new Light(serial, param.green, param.yellow, param.red);
road2Lights.push_back(light);
}
}
RoadLightController::~RoadLightController() {
for(auto serial : serials) {
serial.second->Close();
delete serial.second;
}
for(auto light : road1Lights) {
delete light;
}
for(auto light: road2Lights) {
delete light;
}
}
void RoadLightController::red() {
// for(int i = 0; i < 7; i++) {
// if(i % 2 == 0) {
// for(auto light : road1Lights) {
// light->close();
// }
// } else {
// for(auto light : road1Lights) {
// light->green();
// }
// }
// usleep(500 * 1000);
// }
//
// for(auto light : road1Lights) {
// light->yellow();
// }
//
// usleep(2 * 1000 * 1000);
for(auto light : road1Lights) {
light->red();
}
usleep(2000 * 1000);
for(auto light:road2Lights) {
light->green();
}
}
void RoadLightController::green() {
// for(int i = 0; i < 7; i++) {
// if(i % 2 == 0) {
// for(auto light : road2Lights) {
// light->close();
// }
// } else {
// for(auto light : road2Lights) {
// light->green();
// }
// }
// usleep(250 * 1000);
// }
//
// for(auto light : road2Lights) {
// light->yellow();
// }
//
//
// usleep(2 * 1000 * 1000);
for(auto light : road2Lights) {
light->red();
}
usleep(2000 * 1000);
for(auto light:road1Lights) {
light->green();
}
}
void RoadLightController::close() {
for(auto light:road1Lights) {
light->close();
}
for(auto light:road2Lights) {
light->close();
}
}
std::tuple<std::vector<LightParam>, std::vector<LightParam>> RoadLightController::init(const char* config) {
std::vector<LightParam> road1;
std::vector<LightParam> road2;
if(config == nullptr) {
printf("file is null\n");
LightParam l1;
l1.dev = "/dev/ttyS1";
l1.red = 3;
l1.green = 1;
l1.yellow = 2;
road1.push_back(l1);
LightParam l2;
l2.dev = "/dev/ttyS1";
l2.red = 1;
l2.green = 2;
road1.push_back(l2);
LightParam l3;
l3.dev = "/dev/ttyS1";
l3.red = 1;
l3.green = 2;
road2.push_back(l3);
LightParam l4;
l4.dev = "/dev/ttyS1";
l4.red = 3;
l4.green = 4;
road2.push_back(l4);
LightParam l5;
l5.dev = "/dev/ttyS1";
l5.red = 1;
l5.green = 2;
road2.push_back(l5);
Json::Value root;
Json::Value road1Value;
Json::Value road2Value;
for(auto p : road1) {
Json::Value t;
t["dev"] = Json::Value(p.dev.c_str());
t["red"] = Json::Value(p.red);
t["yellow"] = Json::Value(p.yellow);
t["green"] = Json::Value(p.green);
road1Value.append(t);
}
root["road1"] = road1Value;
for(auto p : road2) {
Json::Value t;
t["dev"] = Json::Value(p.dev.c_str());
t["red"] = Json::Value(p.red);
t["yellow"] = Json::Value(p.yellow);
t["green"] = Json::Value(p.green);
road2Value.append(t);
}
root["road2"] = road2Value;
auto str = root.toStyledString();
FILE* f = fopen("light.json", "w");
fputs(str.c_str(), f);
fclose(f);
} else {
printf("open file:%s\n", config);
char buffer[1024 * 4] = {0};
std::fstream file(config);
if(file.fail()) {
return init(nullptr);
}
file.read(buffer, sizeof(buffer));
file.close();
std::cout<<buffer<<std::endl;
Json::CharReaderBuilder b;
Json::CharReader* JsonReader(b.newCharReader());
Json::Value root;
std::string errs;
bool ok = JsonReader->parse(buffer, buffer + strlen(buffer), &root, &errs);
if(!ok) {
return init(nullptr);
} else {
if(root.isMember("road1")) {
Json::Value r = root["road1"];
for(int i = 0; i < r.size(); i++) {
Json::Value tmp = r[i];
LightParam param;
param.dev = tmp["dev"].asString();
param.red = tmp["red"].asInt();
param.green = tmp["green"].asInt();
param.yellow = tmp["yellow"].asInt();
road1.push_back(param);
}
}
if(root.isMember("road2")) {
Json::Value r = root["road2"];
for(int i = 0; i < r.size(); i++) {
Json::Value tmp = r[i];
LightParam param;
param.dev = tmp["dev"].asString();
param.red = tmp["red"].asInt();
param.green = tmp["green"].asInt();
param.yellow = tmp["yellow"].asInt();
road2.push_back(param);
}
}
}
}
return std::tuple<std::vector<LightParam>, std::vector<LightParam>>(road1, road2);
}
void RoadLightController::init() {
for(auto light : road1Lights) {
light->red();
}
for(auto light:road2Lights) {
light->green();
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/msgy/cgutech-jni-serial.git
git@gitee.com:msgy/cgutech-jni-serial.git
msgy
cgutech-jni-serial
cgutech-jni-serial
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385