|
| 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)] |
0 commit comments