Example #1
0
class I2C:
    DIGITAL = PyMata.DIGITAL
    ANALOG = PyMata.ANALOG

    def __init__(self, pin_type, clk_pin, data_pin, port="/dev/ttyACM0",
                 debug=False):
        """

        :param pin_type: DIGITAL 或者 ANALOG
        :param clk_pin: 时钟总线接入的针脚
        :param data_pin: 数据总线接入的针脚
        :param port: 虚谷连接I2C设备的COM口,默认为"/dev/ttyACM0"
        :param debug: 当为True的时候,会输出debug信息
        """
        self.board = PyMata(port, bluetooth=False, verbose=debug)
        # i2c设备初始化
        self.i2c = self.board.i2c_config(0, pin_type, clk_pin, data_pin)

    def read(self, addr=0x48, register=0, read_byte=2):
        """

        :param addr: i2c设备的一个地址
        :param register: i2c设备某个地址的寄存器
        :param read_byte: 一次读取的字节数量
        :return:
        """
        # 向i2c的一个地址发送一个信号
        self.board.i2c_write(addr, PyMata.I2C_READ_CONTINUOUSLY)
        time.sleep(0.5)
        # 读取这个地址的寄存器中缓存的数据
        self.board.i2c_read(addr, register, read_byte, PyMata.I2C_READ)
        time.sleep(0.5)
        # 获取该地址中的数据
        data = self.board.i2c_get_read_data(addr)
        return data

    def write(self, value, addr=0x48, register=0):
        self.board.i2c_write(addr, addr, register, value)
import time

from PyMata.pymata import PyMata

board = PyMata("/dev/ttyACM0")

# for leonardo
board.i2c_config(0, board.DIGITAL, 3, 2)

# for uno
# board.i2c_config(0, board.ANALOG, 4, 5)

while True:
    try:
        board.i2c_write(
            0x23,
            board.I2C_READ_CONTINUOUSLY)  # same results with board.I2C_READ
        time.sleep(.2)
        board.i2c_read(
            0x23, 0, 2,
            board.I2C_READ)  # same results with board.I2C_READ_CONTINUOUSLY
        time.sleep(.3)

        data = board.i2c_get_read_data(0x23)
        print('Got read data: %s' % data)
        lux = (data[1] << 8 | data[2]) >> 4
        lux /= 1.2
        print(str(lux) + ' lux')
    except KeyboardInterrupt:
        board.close()
class BiColorDisplayController:
    # display blink control values

    board_address = 0 # i2c address
    blink_rate = 0  # blink rate
    brightness = 0 # brightness

    # blink rate defines
    HT16K33_BLINK_CMD = 0x80
    HT16K33_BLINK_DISPLAYON = 0x01
    HT16K33_BLINK_OFF = 0
    HT16K33_BLINK_2HZ = 1
    HT16K33_BLINK_1HZ = 2
    HT16K33_BLINK_HALFHZ = 3

    # oscillator control values
    OSCILLATOR_ON = 0x21
    OSCILLATOR_OFF = 0x20

    # led color selectors
    LED_OFF = 0
    LED_RED = 1
    LED_YELLOW = 2
    LED_GREEN = 3

    # brightness
    MAX_BRIGHTNESS = 15

    # the display buffer
    # This is an 16x16 matrix that stores pixel data for the display
    # Even rows store the green pixel data and odd rows store the red pixel data.
    #
    # A physical row on the device is controlled by a row pair (ie 0,1 and 2,3, etc) for the 2 colors.
    #
    # To output yellow the red and green information for the rows will be set high.
    #
    display_buffer = [[0 for x in xrange(8)] for x in xrange(8)]


    def __init__(self, address, blink_rate, brightness):

        # create an instance of PyMata - we are using an Arduino UNO
        """

        @param address: I2C address of the device
        @param blink_rate: desired blink rate
        @param brightness: brightness level for the display
        """
        self.firmata = PyMata("/dev/ttyACM0")

        self.board_address = address
        self.blink_rate = blink_rate
        self.brightness = brightness
        self.clear_display_buffer()

        # configure firmata for i2c on an UNO
        self.firmata.i2c_config(0, self.firmata.ANALOG, 4, 5)

        # turn on oscillator
        self.oscillator_set(self.OSCILLATOR_ON)

        # set blink rate
        self.set_blink_rate(self.blink_rate)

        # set brightness
        self.set_brightness(self.brightness)

    def set_blink_rate(self, b):
        """
        Set the user's desired blink rate (0 - 3)
        @param b: blink rate
        """
        if (b > 3):
            b = 0 # turn off if not sure
        self.firmata.i2c_write(self.board_address,
                                 (self.HT16K33_BLINK_CMD | self.HT16K33_BLINK_DISPLAYON | (b << 1)))

    def oscillator_set(self, osc_mode):
        """
        Turn oscillator on or off
        @param osc_mode: osc mode (OSCILLATOR_ON or OSCILLATOR_OFF)
        """
        self.firmata.i2c_write(self.board_address, osc_mode)

    def set_brightness(self, brightness):
        """
        Set the brightness level for the entire display
        @param brightness: brightness level (0 -15)
        """
        if brightness > 15:
            brightness = 15
        brightness |= 0xE0
        self.brightness = brightness
        self.firmata.i2c_write(0x70,  brightness)

    def set_pixel(self, row, column, color, suppress_write):
        # rows 0,2,4,6,8,10,12,14 are green
        # rows 1,3,5,7,9,11,13,15 are red
        #
        # A row entry consists of 2 bytes, the first always being 0 and the second
        # being the state of the pixel (high or low)
        """

        @param row: pixel row number
        @param column: pix column number
        @param color: pixel color (yellow is both red and green both on)
        @param suppress_write: if true, just sets the internal data structure, else writes out the pixel to the display
        """
        if (row < 0) or (row >= 8):
            print "set_pixel(): ROW out of range"
            return
        if (column < 0) or (column >= 8):
            print "set_pixel(): COLUMN out of range"
            return

        self.display_buffer[row][column] = color

        # output changes to row on display

        green = 0
        red = 0

        # calculate row for green rows and then adjust it for red

        for col in range(0, 8):
            # assemble green data for the row and output
            if self.display_buffer[row][col] == self.LED_GREEN:
                green |= 1 << col
            elif self.display_buffer[row][col] == self.LED_RED:
                red |= 1 << col
            elif self.display_buffer[row][col] == self.LED_YELLOW:
                green |= 1 << col
                red |= 1 << col
            elif self.display_buffer[row][col] == self.LED_OFF:
                green &= ~(1 << col)
                red &= ~(1 << col)

        if suppress_write == False:
            self.firmata.i2c_write(0x70, row * 2, 0, green)
            self.firmata.i2c_write(0x70, row * 2 + 1, 0, red)

    def set_bit_map(self, shape, color):
        """
        Populate the bit map with the supplied "shape" and color
        and then write the entire bitmap to the display
        @param shape: pattern to display
        @param color: color for the pattern
        """
        for row in xrange(0, 8):
            data = shape[row]
            # shift data into buffer
            bit_mask = 0x80
            for column in xrange(0, 8):
                if data & bit_mask:
                    self.set_pixel(row, column, color, True)
                bit_mask >>= 1
        self.output_entire_buffer()

    def output_entire_buffer(self):
        """
        Write the entire buffer to the display
        """
        green = 0
        red = 0

        for row in xrange(0, 8):
            for col in xrange(0, 8):
                if self.display_buffer[row][col] == self.LED_GREEN:
                    green |= 1 << col
                elif self.display_buffer[row][col] == self.LED_RED:
                    red |= 1 << col
                elif self.display_buffer[row][col] == self.LED_YELLOW:
                    green |= 1 << col
                    red |= 1 << col
                elif self.display_buffer[row][col] == self.LED_OFF:
                    green &= ~(1 << col)
                    red &= ~(1 << col)

            self.firmata.i2c_write(0x70, row * 2, 0, green)
            self.firmata.i2c_write(0x70, row * 2 + 1, 0, red)


    def clear_display_buffer(self):
        """
        Set all led's to off.
        """
        for row in range(0, 8):
            self.firmata.i2c_write(0x70,  row * 2, 0, 0)
            self.firmata.i2c_write(0x70,  (row * 2) + 1, 0, 0)

            for column in range(0, 8):
                self.display_buffer[row][column] = 0

    def close(self):
        """
        close the interface down cleanly

        """
        self.firmata.close()
Example #4
0
import signal
from time import sleep

from PyMata.pymata import PyMata

scl = 0
sda = 1

board = PyMata("/dev/ttyACM0", verbose=True)

board.i2c_config(0, board.ANALOG, 0, 1)


def signal_handler(sig, frame):
    print('You pressed Ctrl+C')
    if board is not None:
        board.reset()
    sys.exit(0)


board.i2c_write(0x27, 1, 1, 1)
sleep(2)
print("Test")
sleep(2)
print("Koniec")

signal.signal(signal.SIGINT, signal_handler)

board.close()

# Porzucony temat
Example #5
0
# for uno
# board.i2c_config(0, board.ANALOG, 4, 5)

def signal_handler(sig, frame):
    print('You pressed Ctrl+C!!!!')
    if board is not None:
        board.reset()
    sys.exit(0)


signal.signal(signal.SIGINT, signal_handler)


def lux_callback(data):
    datax = data[2]
    print('Got read data: %s' % data)
    lux = (datax[1] << 8 | datax[2]) >> 4
    lux /= 1.2
    print(str(lux) + ' lux')


while True:
    try:
        board.i2c_write(0x23, board.I2C_READ_CONTINUOUSLY)  # same results with board.I2C_READ
        time.sleep(.3)
        board.i2c_read(0x23, 0, 2, board.I2C_READ, lux_callback)  # same results with board.I2C_READ_CONTINUOUSLY
        time.sleep(.3)
    except KeyboardInterrupt:
        board.close()