当前位置:网站首页>硬件外设=maixpy3
硬件外设=maixpy3
2022-07-25 10:48:00 【栋哥修炼日记】
开关量输出
import time
from maix import gpio
led = gpio.gpio(6, "H", 1)
print(led.source)
while True:
led.set_value(0)
print(led.get_value())
time.sleep(0.5)
led.set_value(1)
print(led.get_value())
time.sleep(0.5)
led.release()
#=================================
from maix import gpio
import time
led = gpio.gpio(6, "H", 1)
print(led.source)
for i in range(3):
led.set_value(0)
print(led.get_value())
time.sleep(0.5)
led.set_value(1)
print(led.get_value())
time.sleep(0.5)
led.release()
开关量输入
#写入按键输入类
import time
class BUTTON:
def __init__(self, line, bank, chip, mode):
from maix import gpio
self.button = gpio.gpio.gpio(line, bank, chip, mode)
def is_pressed(self):
if self.button.get_value() != 1:
return True
def __del__(self):
self.button.release()
global key
key = BUTTON(6, 2)
print(key.button.source)
IIC
from maix import i2c
print(i2c.scan())#查看总线上的设备地址(返回十进制的数据)
#实例化设备是指所使用的I2C总线序号
from maix import i2c
i2c=i2c.I2C('dev/i2c-2',0x62)
#读取设备寄存器信息
from maix import i2c
i2c=i2c.I2C('dev/i2c-2',0x62)
print(i2c.read(0x1,1)
PWM
#先将PWM-6实例化,设置周期占空比,最后使能PWM即可以输出
from maix import pwm
import time
pwm6 = pwm.PWM(6)
pwm6.export()
pwm6.period = 20000000 # 表示 pwm 的周期,单位 ns
pwm6.duty_cycle = 500000 # 表示占空比,单位 ns
pwm6.enable = True # 表示是否使能 pwm
for t in range (3):
for i in range(500000, 15000000, +100000):
pwm6.duty_cycle = i
time.sleep(0.05)
for i in range(15000000, 500000, -100000):
pwm6.duty_cycle = i
time.sleep(0.05)
UART
- 对于 MaixII-Dock ,不要使用 UART-0 通道来进行串口通讯
- 这个串口是直连芯片,会有一些其他数据吞吐
- 将 MaixII-Dock 上 UART-1 TX 和 UART-1 RX 短接即可进行串口通讯测试
import serial
ser = serial.Serial("/dev/ttyS1",115200) # 连接串口
print('serial test start ...')
ser.write(b"Hello Wrold !!!\n") # 输入需要通讯的内容
for i in range(3):
ser.setDTR(True)
ser.setRTS(True)
tmp = ser.readline()
print(tmp)
ser.write(tmp)
ser.setDTR(False)
ser.setRTS(False)
SPI
- 在 Linux 系统中,SPI 是以设备的形式存在(/dev/spidevX.X)
- 通过查看开发板的管脚定义图,确定自己使用的 SPI 通道序号,片选序号。
- 查看 MaixII-Dock 管脚图,只引出了一个 SPI 通道,使用的是 SPI-1,片选0
from maix import spi
spi = spi.SpiDev()
spi.open(1, 0)#使用的是 SPI-1,片选0
spi.bits_per_word = 8
spi.max_speed_hz = 1
spi.mode = 0b11
import time
for i in range(3):
time.sleep(0.1)
to_send = [0x01, 0x02, 0x01]
print(spi.xfer2(to_send, 800000))
事件
- 入事件是linux系统中都存在的一种特殊的设备(dev/event/input),可以通过事件来检测外接的鼠标、键盘等设备是否发生变化。
- 如果检测键盘输入了什么进行了什么样的操作,一样可以通过输入事件来获取
# 应用于检测两个按键的输入
#如果外界别的设备,这需要修改event.InputDevice()中的参数
from maix import event
from select import select
def check_key():
import os
tmp = "/dev/input/by-path/"
if os.path.exists(tmp):
for i in os.listdir(tmp):
if i.find("kbd") != -1:
return tmp + i
return "/dev/input/event0"
count = 0
dev = event.InputDevice(check_key())
while True:
r, w, x = select([dev], [], [], 0) # if r == 0 or set 0 will read() raise BlockingIOError
if r:
for data in dev.read():
print(data)
if data.code == 0x02:
print('press key S1')
if data.code == 0x03:
print('press key S2')
if data.value == 1 and data.code != 0:
count += 1
print('press sum:', count)
#通过 /dev/input/event0 进行事件设备的选择,
#可以通过 os.system("ls /dev/input/") 进行查看接入了多少事件设备
ADC
- 根据数据手册可知 V831 数据地址 0x05070080 处有一个 12bit (0-4095)的 adc 引脚,
- 但该引脚默认被当做 adc-key 使用,使得一个引脚可以支持多个按键事件。
# 定义MaixII-Dock ADC模块
class v83x_ADC():
def __init__(self,addr=b"0x05070080")->None:
self.addr=addr
self.path="/sys/class/sunxi_dump/dump"
self.file=open(self.path,"wb+")
self.last=self.value()
def __del__(self):
try:
if self.file:
self.file.close()
del self.file
except Exception as e:
pass
def value(self):
self.file.write(b"0x05070080")
self.file.seek(0)
return int(self.file.read()[:-1],16)
global v83x_ADC
v83x_ADC=v83x_ADC()
#使用ADC进行设备通讯
import time
from maix import display,image
v831_adc0=v83x_ADC
while True:
time.sleep(0.1)
tmp=image.Image().new().new((240,240)),(0x2c,0x3e,0x50),"RGB")
val=v831_adc0.value()
print(val)
img=image.Image().open('/home/res/logo.png')
tmp.draw_iamge(img,50,40,alpha=1).draw_string(20,200,"adc0:"+str(val),1,(0xbd,0xc3,0xc7))
display.show(tmp)
边栏推荐
- 【mysql学习08】
- 推荐系统-协同过滤在Spark中的实现
- Reinforcement learning (IV)
- LVS load balancing lvs-nat building Web Cluster
- Txt to CSV file, blank lines appear every other line
- 工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖
- Nowcodertop7-11 - continuous updating
- LVS负载均衡之LVS-DR搭建Web群集与LVS结合Keepalived搭建高可用Web群集
- Loadbalancerlife lifecycle requested by feign client
- Use three.js to realize the cool cyberpunk style 3D digital earth large screen
猜你喜欢

MySQL | GROUP_ The concat function concatenates the values of a column with commas

SQL注入 Less17(报错注入+子查询)

Some usages of beautifulsoup

The principle analysis of filter to solve the request parameter garbled code

Small program of vegetable distribution in community

A troubleshooting record of DirectShow playback problems
Details of the list of state products that Apple announced to be eligible for the sales tax holiday in the United States

leetcode 剑指 Offer 27. 二叉树的镜像

基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现

推荐系统-协同过滤在Spark中的实现
随机推荐
Introduction to shortcut keys in debug chapter
Dynamic planning question 05_ Missile interception
A troubleshooting record of DirectShow playback problems
活动报名 | 玩转 Kubernetes 容器服务提高班正式开营!
Only know that the preform is used to generate objects? See how I use unity to generate UI prefabs
动态规划问题03_最大子段和
[树] 100. 相同的树
2022 年中回顾|一文看懂预训练模型最新进展
ESP8266 使用 DRV8833驱动板驱动N20电机
让运动自然发生,FITURE打造全新生活方式
大话DevOps监控,团队如何选择监控工具?
苹果美国宣布符合销售免税假期的各州产品清单细节
shell-第四天作业
Greedy problem 01_ Activity arrangement code analysis
Nowcodertop12-16 - continuous updating
RedisUtil
常见WEB攻击与防御
【mysql学习08】
leetcode 剑指 Offer 27. 二叉树的镜像
Details of the list of state products that Apple announced to be eligible for the sales tax holiday in the United States