Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
francois-berder committed Feb 14, 2017
2 parents 8f31120 + 1db8671 commit 91862a6
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 35 deletions.
17 changes: 7 additions & 10 deletions examples/alphanum_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@
"""This example shows how to use the Alphanum Click wrapper of the LetMeCreate
to display characters.
It displays "Ci" using both displays by enabling them one after the other at
100Hz to give the illusion that both characters are displayed at the same
time. The user has to interrupt the program to exit it by pressing Ctrl+C.
*
It displays "Ci" during 5 seconds
The Alphanum Click must be inserted in Mikrobus 1 before running this
program.
"""

from letmecreate.core.common import MIKROBUS_1
from letmecreate.core import spi
from letmecreate.click import alphanum
import time
from time import sleep


spi.init()
alphanum.init(MIKROBUS_1)
alphanum.write('C', 'i')

while True:
alphanum.select_left_display()
time.sleep(0.01)
alphanum.select_right_display()
time.sleep(0.01)
sleep(5)

alphanum.release()
spi.release()
64 changes: 64 additions & 0 deletions examples/lora_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python3
"""This example shows how to use the Lora Click wrapper of the LetMeCreate to
send or receive data.
Depending on the mode selected, it either continuously sends a message every
second, or it waits for data and prints what it receives.
The user has to interrupt the program to exit it by pressing Ctrl+C.
To run the example in send mode:
$ ./lora_example.py s
To run the example in receive mode:
$ ./lora_example.py r
The Lora Click must be inserted in Mikrobus 1 before running this program.
"""

from letmecreate.core.common import MIKROBUS_1
from letmecreate.click import lora
from letmecreate.core import uart
from letmecreate.core.uart import UART_BD_57600
from time import sleep
import sys

def receive():
buffer = lora.receive(16)
buffer = bytes(buffer).decode('utf-8')
print(buffer)

def send(n):
buffer = 'Hello, World! {}'.format(n)
lora.send(buffer.encode('utf-8'))
print(buffer)

if len(sys.argv) < 2:
print('{} (r|s)'.format(sys.argv[0]))
sys.exit(-1)

mode = sys.argv[1]
if mode != 'r' and mode != 's':
print('Invalid mode.')
sys.exit(-1)

print('Press Ctrl+C to exit program')

uart.init()
uart.select_bus(MIKROBUS_1)
uart.set_baudrate(UART_BD_57600)

config = lora.get_default_configuration()
lora.init(MIKROBUS_1, config)

n = 0
while True:
if mode == 's':
send(n)
sleep(1)
elif mode == 'r':
receive()

n += 1


uart.release()
20 changes: 20 additions & 0 deletions examples/uni_hall_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python3
"""This example shows how to use the UNI Hall Click binding of LetMeCreate.
"""

from letmecreate.click import uni_hall
from letmecreate.core.common import MIKROBUS_1
from letmecreate.core.gpio_monitor import GPIO_FALLING, GPIO_RAISING
from time import sleep


def print_hello(arg):
if arg == GPIO_FALLING:
print("Hello, starts dectecting north pole.")
elif arg == GPIO_RAISING:
print("Hello, stops dectecting north pole.")

uni_hall.attach_callback(MIKROBUS_1, print_hello)
print("Callback is now active for 15 seconds.")
print("Move the north pole of a magnet over the sensor to print \"hello\".")
sleep(15)
5 changes: 3 additions & 2 deletions letmecreate/click/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Joystick
- Light
- LIN Hall
- Lora
- Motion
- Oled
- Proximity
Expand All @@ -31,7 +32,7 @@
"""

__all__ = ['seven_seg', 'led_matrix', 'accel', 'adc', 'air_quality', 'alcohol',
'bargraph', 'CO', 'color', 'color2', 'eve', 'fan', 'gyro',
'ir_distance', 'ir_eclipse', 'joystick', 'light', 'lin_hall',
'bargraph', 'co', 'color', 'color2', 'eve', 'fan', 'gyro',
'ir_distance', 'ir_eclipse', 'joystick', 'light', 'lin_hall', 'lora',
'motion', 'oled', 'proximity', 'relay', 'relay2', 'relay4', 'rtc',
'thermo3', 'uni_hall']
25 changes: 4 additions & 21 deletions letmecreate/click/alphanum.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,8 @@ def raw_write(a, b):
raise Exception("alphanum click raw write failed")


def select_left_display():
"""Select left display.
This function will enable the left display and disable the right display.
Note: An exception is thrown if it fails to select the left display.
"""
ret = _LIB.alphanum_click_select_left_display()
if ret < 0:
raise Exception("alphanum click select left display failed")


def select_right_display():
"""Select right display.
This function will enable the right display and disable the left display.
Note: An exception is thrown if it fails to select the right display.
"""
ret = _LIB.alphanum_click_select_right_display()
def release():
"""Stop refreshing display"""
ret = _LIB.alphanum_click_release()
if ret < 0:
raise Exception("alphanum click select right display failed")
raise Exception("alphanum click release failed")
174 changes: 174 additions & 0 deletions letmecreate/click/lora.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#!/usr/bin/env python3
"""Python binding of Lora wrapper of LetMeCreate library."""

import ctypes

LORA_CLICK_AUTO_FREQ_BAND_250KHZ = 0
LORA_CLICK_AUTO_FREQ_BAND_125KHZ = 1
LORA_CLICK_AUTO_FREQ_BAND_62_5KHZ = 2
LORA_CLICK_AUTO_FREQ_BAND_31_3KHZ = 3
LORA_CLICK_AUTO_FREQ_BAND_15_6KHZ = 4
LORA_CLICK_AUTO_FREQ_BAND_7_8KHZ = 5
LORA_CLICK_AUTO_FREQ_BAND_3_9KHZ = 6
LORA_CLICK_AUTO_FREQ_BAND_200KHZ = 7
LORA_CLICK_AUTO_FREQ_BAND_100KHZ = 8
LORA_CLICK_AUTO_FREQ_BAND_50KHZ = 9
LORA_CLICK_AUTO_FREQ_BAND_25KHZ = 10
LORA_CLICK_AUTO_FREQ_BAND_12_5KHZ = 11
LORA_CLICK_AUTO_FREQ_BAND_6_3KHZ = 12
LORA_CLICK_AUTO_FREQ_BAND_3_1KHZ = 13
LORA_CLICK_AUTO_FREQ_BAND_166_7KHZ = 14
LORA_CLICK_AUTO_FREQ_BAND_83_3KHZ = 15
LORA_CLICK_AUTO_FREQ_BAND_41_7KHZ = 16
LORA_CLICK_AUTO_FREQ_BAND_20_8KHZ = 17
LORA_CLICK_AUTO_FREQ_BAND_10_4KHZ = 18
LORA_CLICK_AUTO_FREQ_BAND_5_2KHZ = 19
LORA_CLICK_AUTO_FREQ_BAND_2_6KHZ = 20
LORA_CLICK_AUTO_FREQ_BAND_COUNT = 21

LORA_CLICK_CODING_RATE_4_5 = 0
LORA_CLICK_CODING_RATE_4_6 = 1
LORA_CLICK_CODING_RATE_4_7 = 2
LORA_CLICK_CODING_RATE_4_8 = 3
LORA_CLICK_CODING_RATE_COUNT = 4

LORA_CLICK_BANDWIDTH_125KHZ = 0
LORA_CLICK_BANDWIDTH_250KHZ = 1
LORA_CLICK_BANDWIDTH_500KHZ = 2

class LoraClickConfig(ctypes.Structure):
"""Lora Click configuration"""
_fields_ = [
("frequency", ctypes.c_uint32),
("spreading_factor", ctypes.c_uint8),
("auto_freq_band", ctypes.c_uint),
("coding_rate", ctypes.c_uint),
("bandwidth", ctypes.c_uint),
("power", ctypes.c_int8),
("bitrate", ctypes.c_uint16),
("freq_deviation", ctypes.c_uint16),
("preamble_length", ctypes.c_uint16),
("enable_crc_header", ctypes.c_bool)]

_LIB = ctypes.CDLL('libletmecreate_click.so')

_LIB.lora_click_get_default_configuration.restype = LoraClickConfig

def get_default_configuration():
"""Returns default configuration:
frequency = 868000000
spreading_factor = 12
auto_freq_band = LORA_CLICK_AUTO_FREQ_BAND_125KHZ
coding_rate = LORA_CLICK_CODING_RATE_4_8
bandwidth = LORA_CLICK_BANDWIDTH_250KHZ
power = 14
bitrate = 5000
freq_deviation = 5000
preamble_length = 8
enable_crc_header = true
"""
return _LIB.lora_click_get_default_configuration()


def init(mikrobus_index, config):
"""Initialize the Lora Click and configure it.
mikrobus_index: 0 (MIKROBUS_1) or 1 (MIKROBUS_2)
config: Configuration of the Lora Click
Note: An exception is thrown if it fails to initialize the Lora Click.
"""
ret = _LIB.lora_click_init(mikrobus_index, config)
if ret < 0:
raise Exception("")


def configure(config):
"""Configure the Lora Click
config: Configuration of the Lora Click
Note: An exception is thrown if it fails to configure the Lora Click.
"""
ret = _LIB.lora_click_configure(config)
if ret < 0:
raise Exception("")


def send(data):
"""Send a list of bytes
data: list of bytes
This is a blocking call.
Note: An exception is thrown if it fails to send all bytes.
"""
length = len(data)
tx_buffer = (ctypes.c_uint8 * length)(*data)
ret = _LIB.lora_click_send(tx_buffer, length)
if ret < 0:
raise Exception("")


def receive(length):
"""Receive a list of bytes
length: Number of bytes to receive
This is a blocking call, it will not return until the number of requested
bytes has been received.
Note: An exception is thrown if it fails to receive all bytes.
"""
rx_buffer = (ctypes.c_uint8 * length)()
ret = _LIB.lora_click_receive(rx_buffer, length)
if ret < 0:
raise Exception("")
return [rx_buffer[i] for i in range(length)]


def write_eeprom(start_address, data):
"""Write some bytes in EEPROM
start_address: Must be in range 0x300-0x3FF
data: A list of bytes to write
Note: An exception is thrown if it fails to write bytes to the EEPROM.
"""
length = len(data)
tmp = (ctypes.c_uint8 * length)(*data)
ret = _LIB.lora_click_write_eeprom(start_address, tmp, length)
if ret < 0:
raise Exception("")


def read_eeprom(start_address, length):
"""Read a list of bytes from EEPROM
start_address: Must be in range 0x300-0x3FF
length: Number of bytes to read
Note: An exception is thrown if it fails to read bytes from the EEPROM.
"""
data = (ctypes.c_uint8 * length)()
ret = _LIB.lora_click_read_eeprom(start_address, data, length)
if ret < 0:
raise Exception("")
return [data[i] for i in range(length)]


def get_eui():
"""Read the EUI from the Lora Click
This function returns a list of 8 bytes representing the EUI of the
device.
Note: An exception is thrown if it fails to read the EUI.
"""
eui = (ctypes.c_uint8 * 8)()
ret = _LIB.lora_click_get_eui(eui)
if ret < 0:
raise Exception("")
return [eui[i] for i in range(8)]
2 changes: 1 addition & 1 deletion letmecreate/click/relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get_relay_1_state(mikrobus_index):
Note: An exception is thrown if it fails to set state of relay 1.
"""
state = ctypes.c_uint8(0)
ret = _LIB.relay_click_set_relay_1_state(mikrobus_index,
ret = _LIB.relay_click_get_relay_1_state(mikrobus_index,
ctypes.byref(state))
if ret < 0:
raise Exception("relay click get relay 1 state")
Expand Down
12 changes: 12 additions & 0 deletions letmecreate/core/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
#!/usr/bin/env python3
"""Define constants for mikrobus index."""

import ctypes

_LIB = ctypes.CDLL('libletmecreate_core.so')

MIKROBUS_1 = 0
MIKROBUS_2 = 1
MIKROBUS_COUNT = 2


def check_valid_mikrobus(mikrobus_index):
"""Check if mikrobus exists
Returns 0 if succesful, -1 otherwise.
"""
return _LIB.check_valid_mikrobus(mikrobus_index)
Loading

0 comments on commit 91862a6

Please sign in to comment.