Skip to content

Commit 91862a6

Browse files
Merge branch 'dev'
2 parents 8f31120 + 1db8671 commit 91862a6

File tree

11 files changed

+345
-35
lines changed

11 files changed

+345
-35
lines changed

examples/alphanum_example.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,23 @@
22
"""This example shows how to use the Alphanum Click wrapper of the LetMeCreate
33
to display characters.
44
5-
It displays "Ci" using both displays by enabling them one after the other at
6-
100Hz to give the illusion that both characters are displayed at the same
7-
time. The user has to interrupt the program to exit it by pressing Ctrl+C.
8-
*
5+
It displays "Ci" during 5 seconds
6+
97
The Alphanum Click must be inserted in Mikrobus 1 before running this
108
program.
119
"""
1210

1311
from letmecreate.core.common import MIKROBUS_1
1412
from letmecreate.core import spi
1513
from letmecreate.click import alphanum
16-
import time
14+
from time import sleep
1715

1816

1917
spi.init()
2018
alphanum.init(MIKROBUS_1)
2119
alphanum.write('C', 'i')
2220

23-
while True:
24-
alphanum.select_left_display()
25-
time.sleep(0.01)
26-
alphanum.select_right_display()
27-
time.sleep(0.01)
21+
sleep(5)
22+
23+
alphanum.release()
24+
spi.release()

examples/lora_example.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python3
2+
"""This example shows how to use the Lora Click wrapper of the LetMeCreate to
3+
send or receive data.
4+
5+
Depending on the mode selected, it either continuously sends a message every
6+
second, or it waits for data and prints what it receives.
7+
The user has to interrupt the program to exit it by pressing Ctrl+C.
8+
9+
To run the example in send mode:
10+
$ ./lora_example.py s
11+
12+
To run the example in receive mode:
13+
$ ./lora_example.py r
14+
15+
The Lora Click must be inserted in Mikrobus 1 before running this program.
16+
"""
17+
18+
from letmecreate.core.common import MIKROBUS_1
19+
from letmecreate.click import lora
20+
from letmecreate.core import uart
21+
from letmecreate.core.uart import UART_BD_57600
22+
from time import sleep
23+
import sys
24+
25+
def receive():
26+
buffer = lora.receive(16)
27+
buffer = bytes(buffer).decode('utf-8')
28+
print(buffer)
29+
30+
def send(n):
31+
buffer = 'Hello, World! {}'.format(n)
32+
lora.send(buffer.encode('utf-8'))
33+
print(buffer)
34+
35+
if len(sys.argv) < 2:
36+
print('{} (r|s)'.format(sys.argv[0]))
37+
sys.exit(-1)
38+
39+
mode = sys.argv[1]
40+
if mode != 'r' and mode != 's':
41+
print('Invalid mode.')
42+
sys.exit(-1)
43+
44+
print('Press Ctrl+C to exit program')
45+
46+
uart.init()
47+
uart.select_bus(MIKROBUS_1)
48+
uart.set_baudrate(UART_BD_57600)
49+
50+
config = lora.get_default_configuration()
51+
lora.init(MIKROBUS_1, config)
52+
53+
n = 0
54+
while True:
55+
if mode == 's':
56+
send(n)
57+
sleep(1)
58+
elif mode == 'r':
59+
receive()
60+
61+
n += 1
62+
63+
64+
uart.release()

examples/uni_hall_example.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python3
2+
"""This example shows how to use the UNI Hall Click binding of LetMeCreate.
3+
"""
4+
5+
from letmecreate.click import uni_hall
6+
from letmecreate.core.common import MIKROBUS_1
7+
from letmecreate.core.gpio_monitor import GPIO_FALLING, GPIO_RAISING
8+
from time import sleep
9+
10+
11+
def print_hello(arg):
12+
if arg == GPIO_FALLING:
13+
print("Hello, starts dectecting north pole.")
14+
elif arg == GPIO_RAISING:
15+
print("Hello, stops dectecting north pole.")
16+
17+
uni_hall.attach_callback(MIKROBUS_1, print_hello)
18+
print("Callback is now active for 15 seconds.")
19+
print("Move the north pole of a magnet over the sensor to print \"hello\".")
20+
sleep(15)

letmecreate/click/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Joystick
2020
- Light
2121
- LIN Hall
22+
- Lora
2223
- Motion
2324
- Oled
2425
- Proximity
@@ -31,7 +32,7 @@
3132
"""
3233

3334
__all__ = ['seven_seg', 'led_matrix', 'accel', 'adc', 'air_quality', 'alcohol',
34-
'bargraph', 'CO', 'color', 'color2', 'eve', 'fan', 'gyro',
35-
'ir_distance', 'ir_eclipse', 'joystick', 'light', 'lin_hall',
35+
'bargraph', 'co', 'color', 'color2', 'eve', 'fan', 'gyro',
36+
'ir_distance', 'ir_eclipse', 'joystick', 'light', 'lin_hall', 'lora',
3637
'motion', 'oled', 'proximity', 'relay', 'relay2', 'relay4', 'rtc',
3738
'thermo3', 'uni_hall']

letmecreate/click/alphanum.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,8 @@ def raw_write(a, b):
6666
raise Exception("alphanum click raw write failed")
6767

6868

69-
def select_left_display():
70-
"""Select left display.
71-
72-
This function will enable the left display and disable the right display.
73-
74-
Note: An exception is thrown if it fails to select the left display.
75-
"""
76-
ret = _LIB.alphanum_click_select_left_display()
77-
if ret < 0:
78-
raise Exception("alphanum click select left display failed")
79-
80-
81-
def select_right_display():
82-
"""Select right display.
83-
84-
This function will enable the right display and disable the left display.
85-
86-
Note: An exception is thrown if it fails to select the right display.
87-
"""
88-
ret = _LIB.alphanum_click_select_right_display()
69+
def release():
70+
"""Stop refreshing display"""
71+
ret = _LIB.alphanum_click_release()
8972
if ret < 0:
90-
raise Exception("alphanum click select right display failed")
73+
raise Exception("alphanum click release failed")

letmecreate/click/lora.py

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
#!/usr/bin/env python3
2+
"""Python binding of Lora wrapper of LetMeCreate library."""
3+
4+
import ctypes
5+
6+
LORA_CLICK_AUTO_FREQ_BAND_250KHZ = 0
7+
LORA_CLICK_AUTO_FREQ_BAND_125KHZ = 1
8+
LORA_CLICK_AUTO_FREQ_BAND_62_5KHZ = 2
9+
LORA_CLICK_AUTO_FREQ_BAND_31_3KHZ = 3
10+
LORA_CLICK_AUTO_FREQ_BAND_15_6KHZ = 4
11+
LORA_CLICK_AUTO_FREQ_BAND_7_8KHZ = 5
12+
LORA_CLICK_AUTO_FREQ_BAND_3_9KHZ = 6
13+
LORA_CLICK_AUTO_FREQ_BAND_200KHZ = 7
14+
LORA_CLICK_AUTO_FREQ_BAND_100KHZ = 8
15+
LORA_CLICK_AUTO_FREQ_BAND_50KHZ = 9
16+
LORA_CLICK_AUTO_FREQ_BAND_25KHZ = 10
17+
LORA_CLICK_AUTO_FREQ_BAND_12_5KHZ = 11
18+
LORA_CLICK_AUTO_FREQ_BAND_6_3KHZ = 12
19+
LORA_CLICK_AUTO_FREQ_BAND_3_1KHZ = 13
20+
LORA_CLICK_AUTO_FREQ_BAND_166_7KHZ = 14
21+
LORA_CLICK_AUTO_FREQ_BAND_83_3KHZ = 15
22+
LORA_CLICK_AUTO_FREQ_BAND_41_7KHZ = 16
23+
LORA_CLICK_AUTO_FREQ_BAND_20_8KHZ = 17
24+
LORA_CLICK_AUTO_FREQ_BAND_10_4KHZ = 18
25+
LORA_CLICK_AUTO_FREQ_BAND_5_2KHZ = 19
26+
LORA_CLICK_AUTO_FREQ_BAND_2_6KHZ = 20
27+
LORA_CLICK_AUTO_FREQ_BAND_COUNT = 21
28+
29+
LORA_CLICK_CODING_RATE_4_5 = 0
30+
LORA_CLICK_CODING_RATE_4_6 = 1
31+
LORA_CLICK_CODING_RATE_4_7 = 2
32+
LORA_CLICK_CODING_RATE_4_8 = 3
33+
LORA_CLICK_CODING_RATE_COUNT = 4
34+
35+
LORA_CLICK_BANDWIDTH_125KHZ = 0
36+
LORA_CLICK_BANDWIDTH_250KHZ = 1
37+
LORA_CLICK_BANDWIDTH_500KHZ = 2
38+
39+
class LoraClickConfig(ctypes.Structure):
40+
"""Lora Click configuration"""
41+
_fields_ = [
42+
("frequency", ctypes.c_uint32),
43+
("spreading_factor", ctypes.c_uint8),
44+
("auto_freq_band", ctypes.c_uint),
45+
("coding_rate", ctypes.c_uint),
46+
("bandwidth", ctypes.c_uint),
47+
("power", ctypes.c_int8),
48+
("bitrate", ctypes.c_uint16),
49+
("freq_deviation", ctypes.c_uint16),
50+
("preamble_length", ctypes.c_uint16),
51+
("enable_crc_header", ctypes.c_bool)]
52+
53+
_LIB = ctypes.CDLL('libletmecreate_click.so')
54+
55+
_LIB.lora_click_get_default_configuration.restype = LoraClickConfig
56+
57+
def get_default_configuration():
58+
"""Returns default configuration:
59+
60+
frequency = 868000000
61+
spreading_factor = 12
62+
auto_freq_band = LORA_CLICK_AUTO_FREQ_BAND_125KHZ
63+
coding_rate = LORA_CLICK_CODING_RATE_4_8
64+
bandwidth = LORA_CLICK_BANDWIDTH_250KHZ
65+
power = 14
66+
bitrate = 5000
67+
freq_deviation = 5000
68+
preamble_length = 8
69+
enable_crc_header = true
70+
"""
71+
return _LIB.lora_click_get_default_configuration()
72+
73+
74+
def init(mikrobus_index, config):
75+
"""Initialize the Lora Click and configure it.
76+
77+
mikrobus_index: 0 (MIKROBUS_1) or 1 (MIKROBUS_2)
78+
config: Configuration of the Lora Click
79+
80+
Note: An exception is thrown if it fails to initialize the Lora Click.
81+
"""
82+
ret = _LIB.lora_click_init(mikrobus_index, config)
83+
if ret < 0:
84+
raise Exception("")
85+
86+
87+
def configure(config):
88+
"""Configure the Lora Click
89+
90+
config: Configuration of the Lora Click
91+
92+
Note: An exception is thrown if it fails to configure the Lora Click.
93+
"""
94+
ret = _LIB.lora_click_configure(config)
95+
if ret < 0:
96+
raise Exception("")
97+
98+
99+
def send(data):
100+
"""Send a list of bytes
101+
102+
data: list of bytes
103+
104+
This is a blocking call.
105+
106+
Note: An exception is thrown if it fails to send all bytes.
107+
"""
108+
length = len(data)
109+
tx_buffer = (ctypes.c_uint8 * length)(*data)
110+
ret = _LIB.lora_click_send(tx_buffer, length)
111+
if ret < 0:
112+
raise Exception("")
113+
114+
115+
def receive(length):
116+
"""Receive a list of bytes
117+
118+
length: Number of bytes to receive
119+
120+
This is a blocking call, it will not return until the number of requested
121+
bytes has been received.
122+
123+
Note: An exception is thrown if it fails to receive all bytes.
124+
"""
125+
rx_buffer = (ctypes.c_uint8 * length)()
126+
ret = _LIB.lora_click_receive(rx_buffer, length)
127+
if ret < 0:
128+
raise Exception("")
129+
return [rx_buffer[i] for i in range(length)]
130+
131+
132+
def write_eeprom(start_address, data):
133+
"""Write some bytes in EEPROM
134+
135+
start_address: Must be in range 0x300-0x3FF
136+
data: A list of bytes to write
137+
138+
Note: An exception is thrown if it fails to write bytes to the EEPROM.
139+
"""
140+
length = len(data)
141+
tmp = (ctypes.c_uint8 * length)(*data)
142+
ret = _LIB.lora_click_write_eeprom(start_address, tmp, length)
143+
if ret < 0:
144+
raise Exception("")
145+
146+
147+
def read_eeprom(start_address, length):
148+
"""Read a list of bytes from EEPROM
149+
150+
start_address: Must be in range 0x300-0x3FF
151+
length: Number of bytes to read
152+
153+
Note: An exception is thrown if it fails to read bytes from the EEPROM.
154+
"""
155+
data = (ctypes.c_uint8 * length)()
156+
ret = _LIB.lora_click_read_eeprom(start_address, data, length)
157+
if ret < 0:
158+
raise Exception("")
159+
return [data[i] for i in range(length)]
160+
161+
162+
def get_eui():
163+
"""Read the EUI from the Lora Click
164+
165+
This function returns a list of 8 bytes representing the EUI of the
166+
device.
167+
168+
Note: An exception is thrown if it fails to read the EUI.
169+
"""
170+
eui = (ctypes.c_uint8 * 8)()
171+
ret = _LIB.lora_click_get_eui(eui)
172+
if ret < 0:
173+
raise Exception("")
174+
return [eui[i] for i in range(8)]

letmecreate/click/relay.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def get_relay_1_state(mikrobus_index):
5050
Note: An exception is thrown if it fails to set state of relay 1.
5151
"""
5252
state = ctypes.c_uint8(0)
53-
ret = _LIB.relay_click_set_relay_1_state(mikrobus_index,
53+
ret = _LIB.relay_click_get_relay_1_state(mikrobus_index,
5454
ctypes.byref(state))
5555
if ret < 0:
5656
raise Exception("relay click get relay 1 state")

letmecreate/core/common.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
#!/usr/bin/env python3
22
"""Define constants for mikrobus index."""
33

4+
import ctypes
5+
6+
_LIB = ctypes.CDLL('libletmecreate_core.so')
7+
48
MIKROBUS_1 = 0
59
MIKROBUS_2 = 1
10+
MIKROBUS_COUNT = 2
11+
12+
13+
def check_valid_mikrobus(mikrobus_index):
14+
"""Check if mikrobus exists
15+
Returns 0 if succesful, -1 otherwise.
16+
"""
17+
return _LIB.check_valid_mikrobus(mikrobus_index)

0 commit comments

Comments
 (0)