gpt4 book ai didi

c++ - 使用板载 BeagleBone 黑色 LED 的 4 位十进制到二进制转换器

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:08:20 26 4
gpt4 key购买 nike

我正在尝试编写一个 C++ 程序,将 1 到 15 之间的数字转换为二进制,并通过板载 BeagleBone 黑色 LED 显示输出。

#include<iostream>
#include<stdio.h>
#include<unistd.h>
using namespace std;

int main(){
FILE *BINHandle = NULL;
const char *LED0="/sys/class/leds/beaglebone:green:usr0/brightness";
const char *LED1="/sys/class/leds/beaglebone:green:usr1/brightness";
const char *LED2="/sys/class/leds/beaglebone:green:usr2/brightness";
const char *LED3="/sys/class/leds/beaglebone:green:usr3/bringtness";

int a, b, c;

cout << "Enter an number between 1 and 15" << endl;
cin >> a;
cout << a << " in binary is:" << endl;

for(b = 3; b >=0; b--){
c = a >> b;

if(c & 1){

if(b = 3){
BINHandle = fopen(LED3, "r+");
fwrite("1", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}
else{
BINHandle = fopen(LED3, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}

if(b = 2){
BINHandle = fopen(LED2, "r+");
fwrite("1", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}
else{
BINHandle = fopen(LED2, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}

if(b = 1){
BINHandle = fopen(LED1, "r+");
fwrite("1", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}
else{
BINHandle = fopen(LED1, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}

if(b = 0){
BINHandle = fopen(LED0, "r+");
fwrite("1", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}
else{
BINHandle = fopen(LED0, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}

}

}
usleep(2000000);


BINHandle = fopen(LED0, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);

BINHandle = fopen(LED1, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);

BINHandle = fopen(LED2, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);

BINHandle = fopen(LED3, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);


}

现在我试图在该位为 1 时单独访问 LED。我没有收到任何错误,但是当我运行代码并输入数字时,当我应该获得输出时我得到“段错误”

最佳答案

const char *LED0="/sys/class/leds/beaglebone:green:usr0/brightness";
const char *LED1="/sys/class/leds/beaglebone:green:usr1/brightness";
const char *LED2="/sys/class/leds/beaglebone:green:usr2/brightness";
const char *LED3="/sys/class/leds/beaglebone:green:usr3/bringtness";
const char *BIN = (LED3<<3)+(LED2<<2)+(LED1<<1)+LED0;

你不能像那样将字符串组合在一起。

如果您想读取或写入多个文件,您需要打开每个文件并单独读取/写入。

关于c++ - 使用板载 BeagleBone 黑色 LED 的 4 位十进制到二进制转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33513024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com