示例#1
0
class Scale:
    """ A Class to represent SparkFun's HX711 Scale """

    @staticmethod
    def twosbinarystring_to_integer(value_string, bits):
        """ Given a binary string of a number in 2's complement, compute the integer value """
        value = int(value_string, 2)

        if value & (1 << (bits - 1)) != 0:
            value = value - (1 << bits)

        return value

    def __init__(self,
                 gain=1,
                 data_cycles=24,
                 clock_pin="G16",
                 data_pin="G15"):
        self.gain = gain
        self.data_cycles = data_cycles
        self.clock_pin = clock_pin
        self.data_pin = data_pin

        self.clk = Pin(self.clock_pin, mode=Pin.OUT)
        self.clk.mode(Pin.OUT)
        self.data = Pin(self.data_pin, mode=Pin.IN, pull=Pin.PULL_DOWN)
        self.data.mode(Pin.IN)

    def read(self):
        self.clk.value(0)

        wait_iterations = 0
        while wait_iterations < 100 and self.data.value() != 0:
            wait_iterations += 1
            sleep_ms(1)

        if wait_iterations >= 100:
            raise Exception("ADC Communication Failure")

        # Retrieve ADC Reading
        observation = []
        for i in range(self.data_cycles):
            self.clk.toggle()
            self.clk.toggle()

            observation.append(self.data.value())

        # Set ADC Gain for next reading
        for i in range(self.gain):
            self.clk.toggle()
            self.clk.toggle()

        ob_string = ''.join([str(i) for i in observation])
        ob_value = self.twosbinarystring_to_integer(ob_string, bits=self.data_cycles)

        return ob_value
示例#2
0
文件: pin.py 项目: ng110/micropython
pin.toggle() # test toggle again
print(pin())
# test different value settings
pin(1)
print(pin.value())
pin(0)
print(pin.value())
pin.value(1)
print(pin())
pin.value(0)
print(pin())

# test all getters and setters
pin = Pin(pin_map[0], mode=Pin.OUT)
# mode
print(pin.mode() == Pin.OUT)
pin.mode(Pin.IN)
print(pin.mode() == Pin.IN)
# pull
pin.pull(None)
print(pin.pull() == None)
pin.pull(Pin.PULL_DOWN)
print(pin.pull() == Pin.PULL_DOWN)
# drive
pin.drive(Pin.MED_POWER)
print(pin.drive() == Pin.MED_POWER)
pin.drive(Pin.HIGH_POWER)
print(pin.drive() == Pin.HIGH_POWER)
# id
print(pin.id() == pin_map[0])
示例#3
0
pin.toggle()  # test toggle again
print(pin())
# test different value settings
pin(1)
print(pin.value())
pin(0)
print(pin.value())
pin.value(1)
print(pin())
pin.value(0)
print(pin())

# test all getters and setters
pin = Pin(pin_map[0], mode=Pin.OUT)
# mode
print(pin.mode() == Pin.OUT)
pin.mode(Pin.IN)
print(pin.mode() == Pin.IN)
# pull
pin.pull(None)
print(pin.pull() == None)
pin.pull(Pin.PULL_DOWN)
print(pin.pull() == Pin.PULL_DOWN)
# drive
pin.drive(Pin.MED_POWER)
print(pin.drive() == Pin.MED_POWER)
pin.drive(Pin.HIGH_POWER)
print(pin.drive() == Pin.HIGH_POWER)
# id
print(pin.id() == pin_map[0])
示例#4
0
class TM1639():
    FONT = {
        '0': 0b00111111,
        '1': 0b00000110,
        '2': 0b01011011,
        '3': 0b01001111,
        '4': 0b01100110,
        '5': 0b01101101,
        '6': 0b01111101,
        '7': 0b00000111,
        '8': 0b01111111,
        '9': 0b01101111,
        'a': 0b01110111,
        'b': 0b01111100,
        'c': 0b01011000,
        'd': 0b01011110,
        'e': 0b01111001,
        'f': 0b01110001,
        'g': 0b01011111,
        'h': 0b01110100,
        'i': 0b00010000,
        'j': 0b00001110,
        'l': 0b00111000,
        'n': 0b01010100,
        'o': 0b01011100,
        'p': 0b01110011,
        'r': 0b01010000,
        's': 0b01101101,
        't': 0b01111000,
        'u': 0b00111110,
        'y': 0b01101110,
        'C': 0b00111001,
        'P': 0b01110011,
        'U': 0b00111110
    }

    def __init__(self, dio, clk, stb):
        self.dio = Pin(dio, Pin.OUT)
        self.clk = Pin(clk, Pin.OUT)
        self.stb = Pin(stb, Pin.OUT)

    def enable(self, intensity=7):
        self.stb.high()
        self.clk.high()

        self.send_command(0x40)
        self.send_command(0x80 | 8 | min(7, intensity))

        self.stb.low()
        self.send_byte(0xC0)
        for i in range(16):
            self.send_byte(0x00)
        self.stb.high()

    def send_command(self, cmd):
        self.stb.low()
        self.send_byte(cmd)
        self.stb.high()

    def send_data(self, addr, data):
        self.send_command(0x44)
        self.stb.low()
        self.send_byte(0xC0 | addr)
        self.send_byte(data)
        self.stb.high()

    def send_byte(self, data):
        for i in range(8):
            self.clk.low()
            self.dio.value((data & 1) == 1)
            data >>= 1
            self.clk.high()

    def set_led(self, n, color):
        self.send_data((n << 1) + 1, color)

    def send_char(self, pos, data, dot=False):
        self.send_data(pos << 1, data | (128 if dot else 0))

    def set_digit(self, pos, digit, dot=False):
        for i in range(0, 6):
            self.send_char(i, self.get_bit_mask(pos, digit, i), dot)

    def get_bit_mask(self, pos, digit, bit):
        return ((self.FONT[digit] >> bit) & 1) << pos

    def set_text(self, text):
        dots = 0b00000000
        pos = text.find('.')
        if pos != -1:
            dots = dots | (128 >> pos + (8 - len(text)))
            text = text.replace('.', '')

        self.send_char(7, self.rotate_bits(dots))
        text = text[0:8]
        text = text[::-1]
        text += " " * (8 - len(text))
        for i in range(0, 7):
            byte = 0b00000000
            for pos in range(8):
                c = text[pos]
                if c == 'c':
                    byte = (byte | self.get_bit_mask(pos, c, i))
                elif c != ' ':
                    byte = (byte | self.get_bit_mask(pos, c, i))
            self.send_char(i, self.rotate_bits(byte))

    def receive(self):
        temp = 0
        self.dio.mode(Pin.IN, pull=Pin.PULL_UP)
        for i in range(8):
            temp >>= 1
            self.clk.low()
            if self.dio.value():
                temp |= 0x80
            self.clk.high()
        self.dio.mode(Pin.OUT)
        return temp

    def get_buttons(self):
        keys = 0
        self.stb.low()
        self.send_byte(0x42)
        for i in range(4):
            keys |= self.receive() << i
        self.stb.high()
        return keys

    def rotate_bits(self, num):
        for i in range(0, 4):
            num = self.rotr(num, 8)
        return num

    def rotr(self, num, bits):
        # num &= (2**bits-1)
        num &= ((1 << bits) - 1)
        bit = num & 1
        num >>= 1
        if bit:
            num |= (1 << (bits - 1))
        return num

    def convert(self, src, dst, n):
        count = 0x01
        s = 0x01
        for i in range(n):
            tmp = src[i]
            for j in range(8):
                if (i < 8):
                    index = 2 * j
                else:
                    index = 2 * j + 1
                if (tmp & count) == count:
                    dst[index] = dst[index] | s
                else:
                    dst[index] = dst[index] & (~s)
                if (count == 0x80):
                    count = 0x01
                else:
                    count <<= 1
            if s == 0x80:
                s = 0x01
            else:
                s <<= 1
示例#5
0
lcd = hx1230.HX1230_SPI(spi, cs, rst)

# draw a triangle pattern
for i in range(108):
    lcd.data([255,127,63,31,15,7,3,1])

# backlight on
bl = Pin(4, Pin.OUT, value=1)

# backlight off
bl(0)

# PWM dimming
# on the ESP8266 port there is a machine.PWM
from machine import PWM

# off
bl_pwm = PWM(bl, freq=500, duty=0)

# dim
bl_pwm = PWM(bl, freq=500, duty=512)

# bright
bl_pwm = PWM(bl, freq=500, duty=1024)

# return to logic level output
bl_pwm.deinit()
bl.mode(b1.OUT)
bl(0)
bl(1)
示例#6
0
    try_pin = "P004"
    try_s = "Pin(Pin.cpu.P004, mode=Pin.IN, pull=Pin.PULL_NONE, drive=Pin.LOW_POWER)"
else:
    try_pin = "P000"
    try_s = "Pin(Pin.cpu.P000, mode=Pin.IN, pull=Pin.PULL_NONE, drive=Pin.LOW_POWER)"

p = Pin(try_pin, Pin.IN)
if str(p) == try_s:
    print("OK")
else:
    print("NG")
    print("exp: " + try_s)
    print("out: " + str(p))

p = Pin("SW1", Pin.IN, Pin.PULL_UP)
if p.mode() != 1:
    print("mode: NG")
p = Pin("SW1", Pin.IN, pull=Pin.PULL_UP)
if p.pull() != 14:
    print("pull: NG")
p = Pin("SW1", mode=Pin.IN, pull=Pin.PULL_UP)

p.init(p.IN, p.PULL_UP)
p.init(p.IN, pull=p.PULL_UP)
p.init(mode=p.IN, pull=p.PULL_UP)
print(p.value())

p.init(p.OUT)
p.init(p.OPEN_DRAIN)
p.low()
print(p.value())