-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
345 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.