12月 02

esp32 +oled显示英文、中文

from machine import Pin, I2C
from oled import SSD1306_I2C
# 定义管脚
scl = Pin(22)
sda = Pin(21)
# 初始化
i2c = I2C(scl=scl, sda=sda)
# 像素 128*64
oled = SSD1306_I2C(128, 64, i2c)
# 清屏
oled.fill(0)
oled.show()

# 显示英文,从0,0像素开始
oled.text('hello', 0, 0)
oled.show()

# 以下为显示中文,重新显示需要清屏。fonts字体为自定义取模。
# 字索引为utf-8 16进制
# https://www.23bei.com/tool-217.html 在线取模
fonts= {
    # 你
    0xe4bda0:
[0x11,0x11,0x11,0x23,0x22,0x64,0xA8,0x20,0x21,0x21,0x22,0x24,0x20,0x20,0x21,0x20,
0x00,0x00,0x00,0xFC,0x04,0x08,0x40,0x40,0x50,0x48,0x4C,0x44,0x40,0x40,0x40,0x80],
    # 好
    0xe5a5bd:
[0x10,0x11,0x10,0x10,0xFC,0x24,0x24,0x27,0x24,0x44,0x28,0x10,0x28,0x44,0x84,0x00,
0x00,0xFC,0x04,0x08,0x10,0x20,0x24,0xFE,0x20,0x20,0x20,0x20,0x20,0x20,0xA0,0x40]
    }

def chinese(ch_str, x_axis, y_axis): 
   offset_ = 0 
   y_axis = y_axis*8  # 中文高  
   x_axis = (x_axis*16)  # 中文宽
   for k in ch_str:
       code = 0x00  # 中文转成16进制编码 
       data_code = k.encode("utf-8")
       code |= data_code[0] << 16 
       code |= data_code[1] << 8
       code |= data_code[2]
       byte_data = fonts[code]
       for y in range(0, 16):
           a_ = bin(byte_data[y]).replace('0b', '')
           while len(a_) < 8:
               a_ = '0'+a_
           b_ = bin(byte_data[y+16]).replace('0b', '')
           while len(b_) < 8:
               b_ = '0'+b_
           for x in range(0, 8):
               oled.pixel(x_axis+offset_+x, y+y_axis, int(a_[x]))   
               oled.pixel(x_axis+offset_+x+8, y+y_axis, int(b_[x]))   
       offset_ += 16
       
chinese('你好',0,0)
oled.show()
6月 29

python3 多CPU加速

#!env python3
# -*- coding:utf-8 -*-
# Author:      simonzhang.net
import time
from multiprocessing import Pool

def toPrint(n):
    print(n, int(time.time()))
    time.slee(0.5)

if __name__=='__main__':
    # 系统有20个CPU
    p = Pool(20)
    # 加载数据
    for i in range(60):
        p.apply_async(toPrint, args=(i,))
    # 加载完成开始执行
    p.close()
    p.join() 

9月 18

snmp 交换机 带宽计算 python

交换机设置SNMP协议,例为V2C版本,获取接口速度。

<code>def getData():
     shost = "192.168.0.2"
     sDevport = "20"
     sin1 = os.popen('snmpwalk -v 2c -c pass ' + \
                      shost + ' IF-MIB::ifHCInOctets.'+ \
                     sDevport).read()[:-1].split()[-1]
     sout1 = os.popen('snmpwalk -v 2c -c pass ' + \
                       shost + ' IF-MIB::ifHCOutOctets.'+ \
                       sDevport).read()[:-1].split()[-1]
     time.sleep(5)
     sin2 = os.popen('snmpwalk -v 2c -c pass ' + \
                      shost + ' IF-MIB::ifHCInOctets.'+ \
                      sDevport).read()[:-1].split()[-1]
     sout2 = os.popen('snmpwalk -v 2c -c pass ' + \
                       shost + ' IF-MIB::ifHCOutOctets.'+ \
                       sDevport).read()[:-1].split()[-1]
     inT = round((float(sin2) - float(sin1))*8/1024/1024/5, 2)
     outT = round((float(sout2) - float(sout1))*8/1024/1024/5, 2)
     return inT, outT</code>

踩坑列表

1)sDevport 端口不是设备的物理端口。如交换机25口,用命令snmpwalk -v 2c -c pass 192.168.1.2 |grep 25,查找 IF-MIB::ifDescr.XX。XX为扩展的口。

2)不要是用IF-MIB::ifInOctets.XX,用IF-MIB::ifHCInOctets.XX。前者为32位,后者为64位记录。前者容易满计算麻烦。

3)交换机返回的为累加统计,所以需要中间等待后,再次获取,计算两次的差。

4)返回的值为byte,需要先乘8,计算为bit然后再除1024,计算进位量。