Skip to content
This repository has been archived by the owner on Aug 23, 2024. It is now read-only.

Drivers for CardKB, DDS AD9833, 4 Relays #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Units/4-RELAY/docs/_static/u097.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions examples/Units/4-RELAY/examples/test_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Test ASYNC mode the MicroPython driver for M5Stack U097, 4 relays I2C grove unit.

In ASYNC mode, the LED can be controled independantly from the relays

* Author(s):
28 may 2021: Meurisse D. (shop.mchobby.be) - port to MicroPython
https://github.com/m5stack/M5Stack/blob/master/examples/Unit/4-RELAY/4-RELAY.ino
"""

from machine import I2C
from m4relay import Relays
from time import sleep_ms, sleep

# Pico - I2C(0) - sda=GP8, scl=GP9
i2c = I2C(0)
# M5Stack core
# i2c = I2C( sda=Pin(21), scl=Pin(22) )

rel = Relays(i2c)

# The LED is controled independantly
rel.synchronize( False )

# blink leds
def blink_leds():
global rel
for count in range( 10 ):
for i in range( 4 ):
rel.led(i,True)
sleep_ms(100)
for i in range( 4 ):
rel.led(i,False)
sleep_ms(100)


# Switch all relay ON
for i in range(4): # from 1 to 3
rel.relay( i, True )
blink_leds();
sleep( 1 )

# Switch All relay OFF
for i in range(4): # from 1 to 3
rel.relay( i, False )
blink_leds()
sleep( 1 )
32 changes: 32 additions & 0 deletions examples/Units/4-RELAY/examples/test_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Test the MicroPython driver for M5Stack U097, 4 relays I2C grove unit.

In SYNC mode, the LED is controled by the RELAY state

* Author(s):
28 may 2021: Meurisse D. (shop.mchobby.be) - port to MicroPython
https://github.com/m5stack/M5Stack/blob/master/examples/Unit/4-RELAY/4-RELAY.ino
"""

from machine import I2C
from m4relay import Relays
from time import sleep

# Pico - I2C(0) - sda=GP8, scl=GP9
i2c = I2C(0)
# M5Stack core
# i2c = I2C( sda=Pin(21), scl=Pin(22) )

rel = Relays(i2c)

# The LED is controled with the Relay

# Switch all relay ON
for i in range(4): # from 1 to 3
rel.relay( i, True )
sleep( 1 )

# Switch All relay OFF
for i in range(4): # from 1 to 3
rel.relay( i, False )
sleep( 1 )
56 changes: 56 additions & 0 deletions examples/Units/4-RELAY/lib/m4relay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
m4relay.py : MicroPython driver for M5Stack U097, 4 relays I2C grove unit.
* Author(s):
28 may 2021: Meurisse D. (shop.mchobby.be) - port to MicroPython
https://github.com/m5stack/M5Stack/blob/master/examples/Unit/4-RELAY/4-RELAY.ino
"""

__version__ = "0.0.1.0"
__repo__ = "https://github.com/mchobby/esp8266-upy/tree/master/m5stack-u097"

from micropython import const


SYS_CTRL = const(0x10) # System control
IO_CTRL = const(0X11) # Relay_ctrl_mode_reg


class Relays:
""" Drive the 4-Relays unit (U097)
:param i2c: the connected i2c bus machine.I2C
:param address: the device address; defaults to 0x26 """

def __init__(self, i2c, address=0x26):
self.i2c = i2c
self.address = address
self.io_buf = bytearray(1) # LED & Relay states
self.buf1 = bytearray(1)

self.synchronize( True )
self.io_buf[0] = 0x00 # set all off
self.i2c.writeto_mem( self.address, IO_CTRL, self.io_buf )

def synchronize( self, synch = True ):
""" Synchronize the Relay and the LED, switch on the relay will also switch the LED """
self.buf1[0] = 0x01 if synch else 0x00
self.i2c.writeto_mem( self.address, SYS_CTRL, self.buf1 )

def relay( self, index, state ):
""" Change the state of the relay """
assert 0<= index <=3, 'Invalid relay index'
if state:
self.io_buf[0] |= (1<<index)
else:
self.io_buf[0] &= 0xFF ^ (1<<index)

self.i2c.writeto_mem( self.address, IO_CTRL, self.io_buf )

def led( self, index, state ):
""" Change the state of the led (only for synchronize False) """
assert 0<= index <=3, 'Invalid led index'
if state:
self.io_buf[0] |= (1<<(index+4))
else:
self.io_buf[0] &= 0xFF ^ (1<<(index+4))

self.i2c.writeto_mem( self.address, IO_CTRL, self.io_buf )
59 changes: 59 additions & 0 deletions examples/Units/4-RELAY/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Using the U097 "I2C 4 relays" (Grove connector) with MicroPython

The "[U097: 4-Relay Unit](https://shop.m5stack.com/products/4-relay-unit)" from M5Stack is a 4 relays module that can be controlled via I2C. This unit fits a Grove connector to ease the connection on Grove's compatible plateforms.

![U097: 4 relays unit](docs/_static/u097.jpg)

It is possible to add Grove to your favorit plateform by wiring your own connector with a [Grove to Pin](https://shop.mchobby.be/product.php?id_product=2145) (or [Grove to Pad](https://shop.mchobby.be/product.php?id_product=1929
)).

__Remarks:__
This driver is sourced from [esp8266-upy GitHub](https://github.com/mchobby/esp8266-upy) maintained by [MCHobby](http://shop.mchobby.be)

# Wire

## Wire to Pico

![U097 to pico](docs/_static/u097-to-pico.jpg)

## Wire to M5Stack Core

![U097 to core](docs/_static/u097-to-core.jpg)

# Test

First, copy the library [lib/m4relay.py](lib/m4relay.py) on your plateform. Then you can execute the examples scripts.

The [test_simple.py](examples/test_simple.py) script (visible here below) explains hos to contole the U097 relays.

``` python
from machine import I2C
from m4relay import Relays
from time import sleep

# Pico - I2C(0) - sda=GP8, scl=GP9
i2c = I2C(0)
# M5Stack core
# i2c = I2C( sda=Pin(21), scl=Pin(22) )

rel = Relays(i2c)

# The LED is controled with the Relay

# Switch all relay ON
for i in range(4): # from 1 to 3
rel.relay( i, True )
sleep( 1 )

# Switch All relay OFF
for i in range(4): # from 1 to 3
rel.relay( i, False )
sleep( 1 )
```

The [test_async.py](examples/test_async.py) script would allows to control :
* the LEDs with `Relays.led(index=0..3, state=True/False)` and
* the Relays with `Relays.relay(index=0..3, state=True/False)`

... independantly from each other when LEDs and Relays are desynchronized with ( `Relays.synchronize( False )` ).

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Units/CardKB/docs/_static/cardkb.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions examples/Units/CardKB/examples/test_char.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
""" CardKB, Mini I2C Keyboard - simplistic read char over I2C

Author(s):
* Meurisse D for MC Hobby sprl

See Github: https://github.com/mchobby/esp8266-upy/tree/master/cardkb
"""

from machine import I2C
from cardkb import *

# Pyboard : X10=sda, X9=scl
# PYBStick: S3=sda, S5=scl
i2c = I2C(1)
# M5Stack : Grove connector - reduced speed needed
# i2c = I2C(freq=100000, sda=21, scl=22)

s = ''

keyb = CardKB( i2c )
while True:
ch = keyb.read_char( wait=True ) # Wait for a key to be pressed (by default)
if ord(ch) == RETURN:
print( 'Return pressed! Blank string')
s = ''
elif ord(ch) == BACKSPACE:
s = s[:-1] # remove last char
else:
s = s + ch # Add the char to the string
print( s )
24 changes: 24 additions & 0 deletions examples/Units/CardKB/examples/test_raw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
""" CardKB, Mini I2C Keyboard - read the raw values over I2C

This is not the recommanded read usage BUT may be useful for debugging

Author(s):
* Meurisse D for MC Hobby sprl

See Github: https://github.com/mchobby/esp8266-upy/tree/master/cardkb
"""

from machine import I2C
from cardkb import CardKB

# Pyboard : X10=sda, X9=scl
# PYBStick: S3=sda, S5=scl
i2c = I2C(1)
# M5Stack : Grove connector - reduced speed needed
# i2c = I2C(freq=100000, sda=21, scl=22)

keyb = CardKB( i2c )
while True:
raw = keyb.get_raw()
if raw != 0:
print( "Key: %s" % hex(raw) )
37 changes: 37 additions & 0 deletions examples/Units/CardKB/examples/test_readkey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
""" CardKB, Mini I2C Keyboard - readkey on keyboardover I2C

Author(s):
* Meurisse D for MC Hobby sprl

See Github: https://github.com/mchobby/esp8266-upy/tree/master/cardkb
"""

from machine import I2C
from cardkb import *

# Pyboard : X10=sda, X9=scl
# PYBStick: S3=sda, S5=scl
i2c = I2C(1)
# M5Stack : Grove connector - reduced speed needed
# i2c = I2C(freq=100000, sda=21, scl=22)

MOD_TEXT = { MOD_NONE : 'none', MOD_SYM : 'SYMBOL', MOD_FN : 'FUNCTION'}

keyb = CardKB( i2c )

# Ctrl key as text
CTRL_NAME = {0xB5:'UP',0xB4:'LEFT',0xB6:'DOWN',0xB7:'RIGHT',0x1B:'ESC',0x09:'TAB',0x08:'BS',0x7F:'DEL',0x0D:'CR'}

print( 'Keycode | Ascii | Modifier' )
print( '---------------------------')
while True:
keycode,ascii,modifier = keyb.read_key()
if keycode == None:
continue # restart the loop
if keyb.is_ctrl( keycode ): # Ctrl char cannot be displayed safely!!!
if keycode in CTRL_NAME:
ascii = CTRL_NAME[keycode]
else: # we do not know the name for that KeyCode
ascii = 'ctrl' # so we replace it with "ctrl" string

print( " %5s | %5s | %s" %(hex(keycode), ascii, MOD_TEXT[modifier]) )
Loading