6009浏览
查看: 6009|回复: 1

[漂移驴车项目] 【漂移驴车-进阶】添加尾灯part --- 1 树莓派驱动LED灯板

[复制链接]
本帖最后由 极凹甫 于 2022-4-8 10:20 编辑



1、 接口功能
接口就是你想要这个模块提供哪些功能,有了这个接口,后面的工作就慢慢来实现吧。

主要有on,off, flash, show 4个接口提供给用户使用。
  1. class LedMatrix():
  2.     '''
  3.     # 初始化ledMartix的引脚
  4.     '''
  5.     def __init__(self):
  6.         pass
  7.     '''
  8.     #led 全亮
  9.     '''
  10.     def on(self):
  11.         pass
  12.     '''
  13.     #led 全灭
  14.     '''
  15.     def off(self):
  16.         pass
  17.     '''
  18.     # led 闪烁
  19.     '''
  20.     def flash(self):
  21.         pass
  22.     """
  23.     用来显示一个字符的编码,你可以自己定义一个编码来显示
  24.     自定义工具地址:https://www.smilefrog.net/tools/DotMatrix/
  25.     自己画个图,可以得到一串如下code
  26.     [0x38, 0x78, 0x18, 0x18, 0x18, 0x18, 0x7E, 0x7E]
  27.     将此列表传入show,即可显示。
  28.     """
  29.     def show(self, code):
  30.         pass
  31.     '''
  32.     尾灯显示一个数字
  33.     '''
  34.     def show_num(self, num=0):
  35.         pass
  36.    
  37.     '''
  38.     释放引脚和相关资源
  39.     '''
  40.     def destory(self):
  41.         pass
复制代码
2、实现LedMatrix代码

  1. class LedMatrix(object):
  2.     '''
  3.     # 初始化
  4.     '''
  5.     def __init__(self, SDI=24, RCLK=25, SRCLK=9):
  6.         self.SDI = SDI
  7.         self.RCLK = RCLK
  8.         self.SRCLK = SRCLK
  9.         self.mxled_enable = False
  10.         self.mxled_thread = None
  11.         self.reset_count = 0
  12.         self.setup()
  13.     '''
  14.     ledMartix的引脚初始化
  15.     '''
  16.     def setup(self):
  17.         GPIO.setmode(GPIO.BCM)  # Number GPIOs by its BCM location
  18.         GPIO.setup(self.SDI, GPIO.OUT)
  19.         GPIO.setup(self.RCLK, GPIO.OUT)
  20.         GPIO.setup(self.SRCLK, GPIO.OUT)
  21.         GPIO.output(self.SDI, GPIO.LOW)
  22.         GPIO.output(self.RCLK, GPIO.LOW)
  23.         GPIO.output(self.SRCLK, GPIO.LOW)
  24.     '''
  25.     595的驱动原理是移位,8*8的led点阵需要横向8位,竖向8位来点亮响应的led
  26.     本函数就是为了移动一个8位2进制到响应的控制引脚。
  27.     '''
  28.     def hc595_shift(self, data):
  29.         for bit in range(0, 8):
  30.             GPIO.output(self.SDI, 0x80 & (data << bit))
  31.             GPIO.output(self.SRCLK, GPIO.HIGH)
  32.             #time.sleep(0.0001)
  33.             GPIO.output(self.SRCLK, GPIO.LOW)
  34.         GPIO.output(self.RCLK, GPIO.HIGH)
  35.         #time.sleep(0.0001)
  36.         GPIO.output(self.RCLK, GPIO.LOW)
  37.     '''
  38.     595如果闪烁或者显示一个图形,需要一个线程在一直刷新
  39.     reset的目的在于切换不同的显示模式的时候,停止正在运行的线程。
  40.     全亮和全灭不需要使用线程刷新可以一直保持。
  41.     '''
  42.     def reset(self):
  43.         if self.mxled_enable:
  44.             self.mxled_enable = False
  45.             self.reset_count += 1
  46.             time.sleep(0.01)
  47.     '''
  48.     #led 全亮
  49.     '''
  50.     def on(self):
  51.         self.reset()
  52.         self.hc595_shift(0xFF)  # 11111111
  53.         self.hc595_shift(0xFF)
  54.         self.mxled_enable = True
  55.     '''
  56.     #led 全灭
  57.     '''
  58.     def off(self):
  59.         self.reset()
  60.         self.hc595_shift(0x00)  # 00000000
  61.         self.hc595_shift(0x00)
  62.     """
  63.     用来显示一个字符的编码,你可以自己定义一个编码来显示
  64.     自定义工具地址:https://www.smilefrog.net/tools/DotMatrix/
  65.     自己画个图,可以得到一串如下code
  66.     [0x38, 0x78, 0x18, 0x18, 0x18, 0x18, 0x7E, 0x7E]
  67.     将此列表传入show,即可显示。
  68.     并启动一个线程,来保持这个刷新的频率
  69.     """
  70.     def show(self, code, is_flash=False):
  71.         self.reset()
  72.         self.mxled_enable = True
  73.         self.mxled_thread = threading.Thread(target=self._show, args=[code, is_flash])
  74.         self.mxled_thread.start()
  75.     '''
  76.     code_row_refresh 逐行扫描的row值,一共8行,从下到上逐渐是1
  77.     0x01    0000 0001
  78.     0x02    0000 0010
  79.     0x04    0000 0100
  80.     0x08    0000 1000
  81.     0x10    0001 0000
  82.     0x20    0010 0000
  83.     0x40    0100 0000
  84.     0x80    1000 0000
  85.     '''
  86.     code_row_refresh = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80]
  87.     '''
  88.     code: 要显示图形的编码
  89.     逐行扫描,显示一个code,该code的长度必须是8
  90.     类似[0x38, 0x78, 0x18, 0x18, 0x18, 0x18, 0x7E, 0x7E]
  91.     共8行,每一行显示点亮 为1的led小灯。
  92.    
  93.     is_flash: 显示的图形 你是否需要闪烁效果。默认是False
  94.     '''
  95.     def _show(self, code, is_flash=False):
  96.         reset_flag = self.reset_count
  97.         while self.mxled_enable:
  98.             for i in range(len(code) - 1, -1, -1):
  99.                 self.hc595_shift(LedMatrix.code_row_refresh[7 - i])
  100.                 self.hc595_shift(code[i])
  101.                 if is_flash:
  102.                     time.sleep(0.01)
  103.                 else:
  104.                     time.sleep(0.002)
  105.             if self.reset_count < reset_flag: break
  106.     '''
  107.     # led 闪烁
  108.     '''
  109.     def flash(self, rate_hz=2):
  110.         self.reset()
  111.         self.mxled_enable = True
  112.         self.mxled_thread = threading.Thread(target=self._flash, args=[rate_hz])
  113.         self.mxled_thread.start()
  114.     def _flash(self, rate_hz=2):
  115.         reset_flag = self.reset_count
  116.         while self.mxled_enable:
  117.             self.hc595_shift(0xFF)  # 11111111
  118.             self.hc595_shift(0xFF)
  119.             time.sleep(1/rate_hz)
  120.             self.hc595_shift(0x00)  # 00000000
  121.             self.hc595_shift(0x00)
  122.             time.sleep(1/rate_hz)
  123.             if self.reset_count < reset_flag: break
  124.     '''  数字0~9的code'''
  125.     code_0 = [0x7E, 0x7E, 0x42, 0x42, 0x42, 0x42, 0x7E, 0x7E]
  126.     code_1 = [0x38, 0x78, 0x18, 0x18, 0x18, 0x18, 0x7E, 0x7E]
  127.     code_2 = [0x7E, 0x06, 0x06, 0x7E, 0x7E, 0x60, 0x60, 0x7E]
  128.     code_3 = [0x3E, 0x3E, 0x02, 0x3E, 0x3E, 0x02, 0x3E, 0x3E]
  129.     code_4 = [0x0C, 0x1C, 0x34, 0x64, 0x44, 0x7E, 0x7E, 0x04]
  130.     code_5 = [0x7E, 0x60, 0x60, 0x7E, 0x7E, 0x06, 0x06, 0x7E]
  131.     code_6 = [0x7E, 0x60, 0x60, 0x7E, 0x7E, 0x66, 0x66, 0x7E]
  132.     code_7 = [0x7E, 0x7E, 0x06, 0x0C, 0x0C, 0x18, 0x30, 0x30]
  133.     code_8 = [0x7E, 0x7E, 0x42, 0x7E, 0x7E, 0x42, 0x7E, 0x7E]
  134.     code_9 = [0x7E, 0x7E, 0x42, 0x7E, 0x7E, 0x02, 0x02, 0x7E]
  135.     code_num = [code_0, code_1, code_2, code_3, code_4,
  136.                 code_5, code_6, code_7, code_8, code_9]
  137.     '''
  138.     尾灯显示一个数字
  139.     '''
  140.     def show_num(self, num=0, is_flash=False):
  141.         self.show(LedMatrix.code_num[num % 10], is_flash)
  142.     '''
  143.     释放引脚和相关资源
  144.     '''
  145.     def destory(self):
  146.         self.off()
  147.         GPIO.cleanup()
复制代码

3、测试
将ledmatrix.py 上传到树莓派任意目录,然后启动python 交互环境来运行试试吧。
  1. >>> import ledmatrix as mx
  2. >>> led = mx.LedMatrix()
  3. >>> led.off()
  4. >>> led.flash()
  5. >>> led.flash(5)
  6. >>> led.flash()
  7. >>> led.show_num(9)
  8. >>> led.show_num(9, 2)
  9. >>> led.off()
  10. >>> led.destory()
  11. >>>
  12. >>> exit()
复制代码

你的尾灯是不是启动了。
后面会将这个ledmatrix加入到驴车的part框架中来。
让我们一起让驴车又快,又酷,又好玩!
⚠️: 文中的代码,别直接用;很简单看懂了,你会调试通过的。








昊男Henry  初级技师

发表于 2022-4-5 17:39:37

炫酷尾灯,我也试试看~感谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail