0%

一.gpio查看命令

//查看引脚状态

1
cat /sys/kernel/debug/gpio 

//查看gpio引脚的复用情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cat /sys/kernel/debug/pinctrl/pinctrl/pinmux-pins

Pinmux settings per pin
Format: pin (name): mux_owner gpio_owner hog?
pin 0 (gpio0-0): wireless-wlan (GPIO UNCLAIMED) function wireless-wlan group wifi-wake-host
pin 1 (gpio0-1): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 2 (gpio0-2): (MUX UNCLAIMED) gpio0:2
pin 3 (gpio0-3): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 4 (gpio0-4): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 5 (gpio0-5): (MUX UNCLAIMED) gpio0:5
pin 6 (gpio0-6): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 7 (gpio0-7): (MUX UNCLAIMED) gpio0:7
pin 8 (gpio0-8): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 9 (gpio0-9): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 10 (gpio0-10): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 11 (gpio0-11): ff050000.i2c (GPIO UNCLAIMED) function i2c1 group i2c1-xfer
pin 12 (gpio0-12): ff050000.i2c (GPIO UNCLAIMED) function i2c1 group i2c1-xfer
pin 13 (gpio0-13): (MUX UNCLAIMED) (GPIO UNCLAIMED)

二.从用户层控制gpio

要从用户层控制gpio前提是这个引脚是gpio或者已经复用为gpio

阅读全文 »

最近有项目需要用到RK3568的GPIO进行功能开发,找到关于GPIO操作例程,特此记录一下,例程是基于英伟达开发板的,简单修改一下就可以应用到RK3568,中间可能需要修改设备树,这里略过设备树修改,以后会再专门讲解
首先使用shell命令测试一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
echo 192 > /sys/class/gpio/export
#导出编号为192的gpio
#如果这个gpio导出成功,会多出一个文件夹/sys/class/gpio/gpio192/
echo 192 > /sys/class/gpio/unexport
#取消导出

echo out > /sys/class/gpio/gpio192/direction
#设置为输出
echo 1 > /sys/class/gpio/gpio192/value
#输出高

echo in > /sys/class/gpio/gpio192/direction
#设置为输入
cat /sys/class/gpio/gpio192/value
#读取输入值

#也可以直接向value中写入high或low,gpio被设置成输出,并输出对应电平
echo high > /sys/class/gpio/gpio192/value
#设置为输出,并输出高电平

#上述命令都会需要root权限

每个芯片都有自己的gpio编号计算公式,需要先将引脚编号转换成数字编号再进行操作

阅读全文 »

在Linux C编程中,获取时间是一个常见的需求。可以使用系统提供的函数来获取当前时间,以及进行时间的格式化和计算。下面将详细介绍如何在Linux C中获取时间。

1. 获取当前时间

要获取当前时间,可以使用time函数。time函数返回自1970年1月1日以来经过的秒数。示例代码如下:

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <time.h>

int main() {
time_t Time;
time(&Time);
printf("当前时间:%s", ctime(&Time));
return 0;
}

上述代码中,time函数将当前时间的秒数保存在Time变量中,然后使用ctime函数将其转换为可读的字符串格式并打印出来。

阅读全文 »

如果您正在使用C语言编程,并需要在JSON数据中增加值,本文将为您提供一种方法。在C语言中,我们可以使用第三方库,如 cJSON 来处理JSON数据。下面是一个简单的例子,演示如何在一个JSON数组中增加值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <cJSON.h>
int main()
{
char* json_str = "[{\"name\":\"Bob\",\"age\":25},{\"name\":\"Alice\",\"age\":22}]";
cJSON* json = cJSON_Parse(json_str);
if (!json)
{
printf("Error before: [%s]\n",cJSON_GetErrorPtr());
}
else
{
cJSON* new_person = cJSON_CreateObject();
cJSON_AddStringToObject(new_person, "name", "Charlie");
cJSON_AddNumberToObject(new_person, "age", 28);
cJSON_AddItemToArray(json, new_person);
char* new_json_str = cJSON_Print(json);
printf("%s\n", new_json_str);
free(new_json_str);
cJSON_Delete(json);
}
return 0;
}

在这个例子中,我们首先定义了一个 JSON 字符串,包含两个人的信息。然后,我们使用 cJSON_Parse 方法将其解析为一个 cJSON 对象。接下来,我们创建了一个新的 cJSON 对象,表示一个新的人。使用 cJSON_AddStringToObject 和 cJSON_AddNumberToObject 方法,我们向新 cJSON 对象中添加了姓名和年龄属性。最后,使用 cJSON_AddItemToArray 方法,我们将新 cJSON 对象添加到原先的 JSON 数组中。最后,将修改后的 JSON 对象打印出来。

在这个例子中,我们演示了如何使用 cJSON 库来向 JSON 数组中增加值。当然,您可以根据您的需要修改这个示例,实现不同的操作。

播放音频文件分两种情况:

一、单纯播放音频文件推荐操作简单使用方便(sox软件、madplay库)。
sox软件播放音频

1、ubuntu安装命令

1
2
sudo apt-get install sox                       // 工具。
sudo apt-get install libsox-fmt-all // 包含MP3的解码器和其他格式的解码器。

2、代码通过系统 system 播放(play是sox软件播放的命令)

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <stdlib.h>
int main() {
system("play 1.mp3");
printf("播放结束了 \n");
exit:
printf("按任意键退出 ...\n");
getchar();
return 0;
}

常用命令和说明推荐文档:
sox常用命令
sox-音频处理工具

madplay库(自己没有测试需要编译就搁置了,推荐网上现成的)

madplay安装及使用

二、想要控制采样率、通道等(alsa库)
阅读全文 »

由于项目中遇到需要发送http请求,然后再解析接收到的响应。大概在网上搜索了一下,有两个比较不错,分别是http-parserfast-http
http-parser是由C编写的工具;fast-http是大部分移植自http-parser,用lisp语言编写的,不太适合目前的项目。fast-http介绍文档《fast-http》。
有一篇文章《HTTP 协议解析库:fast-http》介绍了fast-http,并且和http-parser进行了简单比较,结论是fast-http更快,快一倍。

阅读全文 »

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
去掉字符串首尾的 \x20 \r \n 字符
by sincoder
*/
void clean_string(char *str)
{
char *start = str - 1;
char *end = str;
char *p = str;
while(*p)
{
switch(*p)
{
case ' ':
case '\r':
case '\n':
{
if(start + 1==p)
start = p;
}
break;
default:
break;
}
++p;
}
//现在来到了字符串的尾部 反向向前
--p;
++start;
if(*start == 0)
{
//已经到字符串的末尾了
*str = 0 ;
return;
}
end = p + 1;
while(p > start)
{
switch(*p)
{
case ' ':
case '\r':
case '\n':
{
if(end - 1 == p)
end = p;
}
break;
default:
break;
}
--p;
}
memmove(str,start,end-start);
*(str + (int)end - (int)start) = 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import sys

from PyQt5.QtWidgets import QApplication, QWidget # 1.导入模块

if __name__ == '__main__':
# 2.创建一个 QApplication 对象,指向QApplication ,接受命令行参数

app = QApplication(sys.argv)
# 3. 创建一个 QWidget对象
w = QWidget()

# 4. 设置窗口标题
w.setWindowTitle("第一个PyQt")

# 5. 展示窗口
w.show()

# 程序进行循环等待状态
app.exec()