Açıklama
|
#main.py_____________# MicroPython TM1637 quad 7-segment LED display driver examples# WeMos D1 Mini -- 4 Digit Display# D1 (GPIO5) ----- CLK# D2 (GPIO4) ----- DIO# 3V3 ------------ VCC# G -------------- GNDimport tm1637from machine import Pinfrom time import sleeptm = tm1637.TM1637(clk=Pin(5), dio=Pin(4))while True: tm.number(i) i=i+1 sleep(0.1) #tm1637.py_______"""MicroPython TM1637 quad 7-segment LED display driverhttps://github.com/mcauser/micropython-tm1637MIT LicenseCopyright (c) 2016 Mike CauserPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE."""from micropython import constfrom machine import Pinfrom time import sleep_us, sleep_msTM1637_CMD1 = const(64) # 0x40 data commandTM1637_CMD2 = const(192) # 0xC0 address commandTM1637_CMD3 = const(128) # 0x80 display control commandTM1637_DSP_ON = const(8) # 0x08 display onTM1637_DELAY = const(10) # 10us delay between clk/dio pulsesTM1637_MSB = const(128) # msb is the decimal point or the colon depending on your display# 0-9, a-z, blank, dash, star_SEGMENTS = bytearray(b'\x3F\x06\x5B\x4F\x66\x6D\x7D\x07\x7F\x6F\x77\x7C\x39\x5E\x79\x71\x3D\x76\x06\x1E\x76\x38\x55\x54\x3F\x73\x67\x50\x6D\x78\x3E\x1C\x2A\x76\x6E\x5B\x00\x40\x63')class TM1637(object): """Library for quad 7-segment LED modules based on the TM1637 LED driver.""" def __init__(self, clk, dio, brightness=7): self.clk = clk self.dio = dio if not 0 <= brightness <= 7: raise ValueError("Brightness out of range") self._brightness = brightness self.clk.init(Pin.OUT, value=0) self.dio.init(Pin.OUT, value=0) sleep_us(TM1637_DELAY) self._write_data_cmd() self._write_dsp_ctrl() def _start(self): self.dio(0) sleep_us(TM1637_DELAY) self.clk(0) sleep_us(TM1637_DELAY) def _stop(self): self.dio(0) sleep_us(TM1637_DELAY) self.clk(1) sleep_us(TM1637_DELAY) self.dio(1) def _write_data_cmd(self): # automatic address increment, normal mode self._start() self._write_byte(TM1637_CMD1) self._stop() def _write_dsp_ctrl(self): # display on, set brightness self._start() self._write_byte(TM1637_CMD3 | TM1637_DSP_ON | self._brightness) self._stop() def _write_byte(self, b): for i in range(8): self.dio((b >> i) & 1) sleep_us(TM1637_DELAY) self.clk(1) sleep_us(TM1637_DELAY) self.clk(0) sleep_us(TM1637_DELAY) self.clk(0) sleep_us(TM1637_DELAY) self.clk(1) sleep_us(TM1637_DELAY) self.clk(0) sleep_us(TM1637_DELAY) def brightness(self, val=None): """Set the display brightness 0-7.""" # brightness 0 = 1/16th pulse width # brightness 7 = 14/16th pulse width if val is None: return self._brightness if not 0 <= val <= 7: raise ValueError("Brightness out of range") self._brightness = val self._write_data_cmd() self._write_dsp_ctrl() def write(self, segments, pos=0): """Display up to 6 segments moving right from a given position. The MSB in the 2nd segment controls the colon between the 2nd and 3rd segments.""" if not 0 <= pos <= 5: raise ValueError("Position out of range") self._write_data_cmd() self._start() self._write_byte(TM1637_CMD2 | pos) for seg in segments: self._write_byte(seg) self._stop() self._write_dsp_ctrl() def encode_digit(self, digit): """Convert a character 0-9, a-f to a segment.""" return _SEGMENTS[digit & 0x0f] def encode_string(self, string): """Convert an up to 4 character length string containing 0-9, a-z, space, dash, star to an array of segments, matching the length of the source string.""" segments = bytearray(len(string)) for i in range(len(string)): segments[i] = self.encode_char(string[i]) return segments def encode_char(self, char): """Convert a character 0-9, a-z, space, dash or star to a segment.""" o = ord(char) if o == 32: return _SEGMENTS[36] # space if o == 42: return _SEGMENTS[38] # star/degrees if o == 45: return _SEGMENTS[37] # dash if o >= 65 and o <= 90: return _SEGMENTS[o-55] # uppercase A-Z if o >= 97 and o <= 122: return _SEGMENTS[o-87] # lowercase a-z if o >= 48 and o <= 57: return _SEGMENTS[o-48] # 0-9 raise ValueError("Character out of range: {:d} '{:s}'".format(o, chr(o))) def hex(self, val): """Display a hex value 0x0000 through 0xffff, right aligned.""" string = '{:04x}'.format(val & 0xffff) self.write(self.encode_string(string)) def number(self, num): """Display a numeric value -999 through 9999, right aligned.""" # limit to range -999 to 9999 num = max(-999, min(num, 9999)) string = '{0: >4d}'.format(num) self.write(self.encode_string(string)) def numbers(self, num1, num2, colon=True): """Display two numeric values -9 through 99, with leading zeros and separated by a colon.""" num1 = max(-9, min(num1, 99)) num2 = max(-9, min(num2, 99)) segments = self.encode_string('{0:0>2d}{1:0>2d}'.format(num1, num2)) if colon: segments[1] |= 0x80 # colon on self.write(segments) def temperature(self, num): if num < -9: self.show('lo') # low elif num > 99: self.show('hi') # high else: string = '{0: >2d}'.format(num) self.write(self.encode_string(string)) self.write([_SEGMENTS[38], _SEGMENTS[12]], 2) # degrees C def show(self, string, colon=False): segments = self.encode_string(string) if len(segments) > 1 and colon: segments[1] |= 128 self.write(segments[:4]) def scroll(self, string, delay=250): segments = string if isinstance(string, list) else self.encode_string(string) data = [0] * 8 data[4:0] = list(segments) for i in range(len(segments) + 5): self.write(data[0+i:4+i]) sleep_ms(delay)class TM1637Decimal(TM1637): """Library for quad 7-segment LED modules based on the TM1637 LED driver. This class is meant to be used with decimal display modules (modules that have a decimal point after each 7-segment LED). """ def encode_string(self, string): """Convert a string to LED segments. Convert an up to 4 character length string containing 0-9, a-z, space, dash, star and '.' to an array of segments, matching the length of the source string.""" segments = bytearray(len(string.replace('.',''))) j = 0 for i in range(len(string)): if string[i] == '.' and j > 0: segments[j-1] |= TM1637_MSB continue segments[j] = self.encode_char(string[i]) j += 1 return segments#test.py________________# MicroPython TM1637 quad 7-segment LED display driver examples# WeMos D1 Mini -- 4 Digit Display# D1 (GPIO5) ----- CLK# D2 (GPIO4) ----- DIO# 3V3 ------------ VCC# G -------------- GNDimport tm1637from machine import Pinfrom time import sleeptm = tm1637.TM1637(clk=Pin(5), dio=Pin(4))# all LEDS on "88:88"tm.write([127, 255, 127, 127])tm.write(bytearray([127, 255, 127, 127]))tm.write(b'\x7F\xFF\x7F\x7F')tm.show('8888', True)tm.numbers(88, 88, True)# all LEDS offtm.write([0, 0, 0, 0])tm.show(' ')# write to the 2nd and 3rd segments onlytm.write([119, 124], 1) # _Ab_tm.write([124], 2) # __b_tm.write([119], 1) # _A__# display "0123"tm.write([63, 6, 91, 79])tm.write(bytearray([63, 6, 91, 79]))tm.write(b'\x3F\x06\x5B\x4F')tm.show('1234')tm.number(1234)tm.numbers(12, 34, False)# display "4567"tm.write([102, 109, 125, 7])tm.write([102], 0) # 4___tm.write([109], 1) # _5__tm.write([125], 2) # __6_tm.write([7], 3) # ___7# set middle two segments to "12", ie "4127"tm.write([6, 91], 1) # _12_# set last segment to "9", ie "4129"tm.write([111], 3) # ___9# walk through all possible LED combinationsfrom time import sleep_msfor i in range(128): tm.number(i) tm.write([i]) sleep_ms(100)# show "AbCd"tm.write([119, 124, 57, 94])tm.show('abcd')# show "COOL"tm.write([0b00111001, 0b00111111, 0b00111111, 0b00111000])tm.write([0x39, 0x3F, 0x3F, 0x38])tm.write(b'\x39\x3F\x3F\x38')tm.write([57, 63, 63, 56])tm.show('cool')tm.show('COOL')# display "dEAd", "bEEF"tm.hex(0xdead)tm.hex(0xbeef)tm.show('dead')tm.show('Beef')# show "12:59"tm.numbers(12, 59)tm.show('1259', True)# show "-123"tm.number(-123)tm.show('-123')# Show Helptm.show('Help')tm.write(tm.encode_string('Help'))tm.write([tm.encode_char('H'), tm.encode_char('e'), tm.encode_char('l'), tm.encode_char('p')])# Scroll Hello World from right to lefttm.scroll('Hello World') # 4 fpstm.scroll('Hello World', 1000) # 1 fps# Scroll all available characterstm.scroll(list(tm1637._SEGMENTS))# all LEDs dimtm.brightness(0)# all LEDs brighttm.brightness(7)# converts a digit 0-0x0f to a byte representing a single segment# use write() to render the byte on a single segmenttm.encode_digit(0)# 63tm.encode_digit(8)# 127tm.encode_digit(0x0f)# 113# 15 or 0x0f generates a segment that can output a F charactertm.encode_digit(15)# 113tm.encode_digit(0x0f)# 113# used to convert a 1-4 length string to an array of segmentstm.encode_string(' 1')# bytearray(b'\x00\x00\x00\x06')tm.encode_string('2 ')# bytearray(b'[\x00\x00\x00')tm.encode_string('1234')# bytearray(b'\x06[Of')tm.encode_string('-12-')# bytearray(b'@\x06[@')tm.encode_string('cafe')# bytearray(b'9wqy')tm.encode_string('CAFE')# bytearray(b'9wqy')tm.encode_string('a')# bytearray(b'w\x00\x00\x00')tm.encode_string('ab')# bytearray(b'w|\x00\x00')# used to convert a single character to a segment bytetm.encode_char('1')# 6tm.encode_char('9')# 111tm.encode_char('-')# 64tm.encode_char('a')# 119tm.encode_char('F')# 113# display "dEAd", "bEEF", "CAFE" and "bAbE"tm.hex(0xdead)tm.hex(0xbeef)tm.hex(0xcafe)tm.hex(0xbabe)# show "00FF" (hex right aligned)tm.hex(0xff)# show " 1" (numbers right aligned)tm.number(1)# show " 12"tm.number(12)# show " 123"tm.number(123)# show "9999" capped at 9999tm.number(20000)# show " -1"tm.number(-1)# show " -12"tm.number(-12)# show "-123"tm.number(-123)# show "-999" capped at -999tm.number(-1234)# show "01:02"tm.numbers(1, 2)# show "0102"tm.numbers(1, 2, False)# show "-5:11"tm.numbers(-5, 11)# show "12:59"tm.numbers(12, 59)# show temperature '24*C'tm.temperature(24)tm.show('24*C')# show temperature works for range -9 to +99tm.temperature(-10) # LO*Ctm.temperature(-9) # -9*Ctm.temperature(5) # 5*Ctm.temperature(99) # 99*Ctm.temperature(100) # HI*C
|
|