Skip to content

Commit f8625fe

Browse files
committed
1. Move mud adaptation liberaries into mud directory. 2. Fixed servral bugs.
1 parent b2341f0 commit f8625fe

File tree

15 files changed

+615
-5
lines changed

15 files changed

+615
-5
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=arduino_unique_id_generator
2+
version=1.0.0-BETA2
3+
author=Dongger
4+
maintainer=Dongger <48190591@qq.com>
5+
sentence=Arduino AVR boards unique ID generator
6+
paragraph=Arduino unique ID generator for Arduino AVR boards
7+
category=Communication
8+
url=https://github.com/TheFirstLineOfCode/mud
9+
architectures=*
10+
includes=arduino_unique_id_generator.h
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <ArduinoUniqueID.h>
2+
3+
#include <debug.h>
4+
5+
#include "arduino_unique_id_generator.h"
6+
7+
char *generateThingIdUsingUniqueIdLibrary(const char* modelName) {
8+
int modelNameLength = strlen(modelName);
9+
char *thingId = malloc(sizeof(char) * (modelNameLength + 1 + 8 + 1));
10+
sprintf(thingId, "%s-%x%x%x%x%x%x%x%x", modelName,
11+
UniqueID8[0] / 16, UniqueID8[1] / 16, UniqueID8[2] / 16, UniqueID8[3] / 16,
12+
UniqueID8[4] / 16, UniqueID8[5] / 16, UniqueID8[6] / 16, UniqueID8[7] / 16);
13+
14+
#ifdef ENABLE_DEBUG
15+
char buffer[64];
16+
sprintf(buffer, "Thing ID has generated. Thing ID: %s.", thingId);
17+
debugOut(buffer);
18+
#endif
19+
20+
return thingId;
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef ARDUINO_UNIQUE_ID_GENERATOR_H
2+
#define ARDUINO_UNIQUE_ID_GENERATOR_H
3+
4+
char *generateThingIdUsingUniqueIdLibrary(const char* modelName);
5+
6+
#endif
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=mud
2+
version=1.0.0-BETA2
3+
author=Dongger
4+
maintainer=Dongger <48190591@qq.com>
5+
sentence=C library for IoT developement
6+
paragraph=C library for IoT application developement on Lithosphere platform
7+
category=Communication
8+
url=https://github.com/TheFirstLineOfCode/mud
9+
architectures=*
10+
includes=thing.h
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=mud_arduino_avr
2+
version=1.0.0-BETA2
3+
author=Dongger
4+
maintainer=Dongger <48190591@qq.com>
5+
sentence=Arduino AVR boards mud adaptation
6+
paragraph=Mud adaptation library for Arduino AVR boards
7+
category=Communication
8+
url=https://github.com/TheFirstLineOfCode/mud
9+
architectures=*
10+
includes=mcu_board_adaptation.h
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
#include <ArduinoUniqueID.h>
2+
#include <EEPROM.h>
3+
4+
#include <tuxp.h>
5+
#include <thing.h>
6+
#include <debug.h>
7+
#include <radio_module_adaptation.h>
8+
9+
#include "mcu_board_adaptation.h"
10+
11+
#if defined(ARDUINO_UNO) && defined(USE_SOFTWARE_SERIAL)
12+
SoftwareSerial softwareSerial(SOFTWARE_SERIAL_RX_PIN, SOFTWARE_SERIAL_TX_PIN);
13+
#endif
14+
15+
static char *modelName;
16+
17+
void debugOutputImpl(const char out[]) {
18+
Serial.println(out);
19+
}
20+
21+
void printToSerialPort(char title[], uint8_t message[], int size) {
22+
#if defined(ENABLE_DEBUG)
23+
24+
Serial.print(title);
25+
26+
if (size == 0) {
27+
Serial.println(F("0 bytes. {}."));
28+
} else {
29+
Serial.print(size);
30+
Serial.print(F(" bytes. {"));
31+
for (int i = 0; i < size; i++) {
32+
Serial.print(message[i]);
33+
if (i == size - 1)
34+
break;
35+
36+
Serial.print(F(","));
37+
}
38+
39+
Serial.println(F("}."));
40+
}
41+
42+
#endif
43+
}
44+
45+
void configureSerial() {
46+
Serial.begin(9600);
47+
while (!Serial) {
48+
delay(200);
49+
}
50+
}
51+
52+
void initializeEepRom(int eepRomLength) {
53+
for (int i = 0; i < eepRomLength - 3; i++)
54+
EEPROM.write(i, 0);
55+
56+
EEPROM.write(eepRomLength - 3, 0xfd);
57+
EEPROM.write(eepRomLength - 2, 0xfe);
58+
EEPROM.write(eepRomLength - 1, 0xff);
59+
}
60+
61+
void (* resetFunc) () = 0;
62+
63+
void resetImpl() {
64+
resetFunc();
65+
}
66+
67+
long getTimeImpl() {
68+
return millis();
69+
}
70+
71+
int readAddressFromEepRom(uint8_t *address, int position) {
72+
for (int i = 0; i < SIZE_RADIO_ADDRESS; i++)
73+
*(address + i) = EEPROM.read(position + i);
74+
75+
return position + SIZE_RADIO_ADDRESS;
76+
}
77+
78+
uint8_t getDacStateByteValue(DacState dacState) {
79+
if (dacState == NONE)
80+
return 0;
81+
else if (dacState == INITIAL)
82+
return 1;
83+
else if (dacState == INTRODUCTING)
84+
return 2;
85+
else if (dacState == ALLOCATING)
86+
return 3;
87+
else if (dacState == ALLOCATED)
88+
return 4;
89+
else if (dacState == CONFIGURED)
90+
return 5;
91+
else
92+
return 0;
93+
}
94+
95+
DacState getDacStateByByte(uint8_t iDacState) {
96+
if (iDacState == 0)
97+
return NONE;
98+
else if (iDacState == 1)
99+
return INITIAL;
100+
else if (iDacState == 2)
101+
return INTRODUCTING;
102+
else if (iDacState == 3)
103+
return ALLOCATING;
104+
else if (iDacState == 4)
105+
return ALLOCATED;
106+
else if (iDacState == 5)
107+
return CONFIGURED;
108+
else
109+
return NONE;
110+
}
111+
112+
void debugOutThingInfo(ThingInfo *thingInfo) {
113+
#ifdef ENABLE_DEBUG
114+
char buffer[64];
115+
if (thingInfo->dacState == ALLOCATED ||
116+
thingInfo->dacState == CONFIGURED) {
117+
sprintf(buffer, "Thing ID: %s. DAC state: %d. Address: {%d, %d, %d}.",
118+
thingInfo->thingId, getDacStateByteValue(thingInfo->dacState),
119+
thingInfo->address[0], thingInfo->address[1], thingInfo->address[2]);
120+
} else {
121+
sprintf(buffer, "Thing ID: %s. DAC state: %d.",
122+
thingInfo->thingId == NULL ? "NULL" : thingInfo->thingId, getDacStateByteValue(thingInfo->dacState));
123+
}
124+
debugOut(buffer);
125+
#endif
126+
}
127+
128+
void loadThingInfoImpl(ThingInfo *thingInfo) {
129+
uint8_t thingIdSize = EEPROM.read(0);
130+
131+
if (thingIdSize == 0) {
132+
#ifdef ENABLE_DEBUG
133+
Serial.println(F("No thing info in storage. Use initial value."));
134+
#endif
135+
136+
thingInfo->thingId = NULL;
137+
thingInfo->dacState = NONE;
138+
thingInfo->address = NULL;
139+
thingInfo->uplinkChannelBegin = -1;
140+
thingInfo->uplinkChannelEnd = -1;
141+
thingInfo->uplinkAddressHighByte = 0xff;
142+
thingInfo->uplinkAddressLowByte = 0xff;
143+
} else {
144+
int position = 1;
145+
thingInfo->thingId = malloc(sizeof(char) * (thingIdSize + 1));
146+
for (int i = 0; i < thingIdSize; i++) {
147+
*((thingInfo->thingId) + i) = EEPROM.read(i + 1);
148+
}
149+
*((thingInfo->thingId) + thingIdSize) = '\0';
150+
position += thingIdSize;
151+
152+
uint8_t iDacState = EEPROM.read(position);
153+
thingInfo->dacState = getDacStateByByte(iDacState);
154+
position++;
155+
156+
if (thingInfo->dacState == ALLOCATED ||
157+
thingInfo->dacState == CONFIGURED) {
158+
EEPROM.get(position, thingInfo->uplinkChannelBegin);
159+
position += 2;
160+
161+
EEPROM.get(position, thingInfo->uplinkChannelEnd);
162+
position += 2;
163+
164+
thingInfo->uplinkAddressHighByte = EEPROM.read(position);
165+
position++;
166+
thingInfo->uplinkAddressLowByte = EEPROM.read(position);
167+
position++;
168+
169+
thingInfo->address = malloc(SIZE_RADIO_ADDRESS);
170+
readAddressFromEepRom(thingInfo->address, position);
171+
}
172+
}
173+
174+
#ifdef ENABLE_DEBUG
175+
Serial.println(F("ThingInfo loaded."));
176+
#endif
177+
debugOutThingInfo(thingInfo);
178+
}
179+
180+
int writeAddressToEepRom(uint8_t *address, int position) {
181+
for (int i = 0; i < SIZE_RADIO_ADDRESS; i++)
182+
EEPROM.write(position + i , *(address + i));
183+
184+
return position + SIZE_RADIO_ADDRESS;
185+
}
186+
187+
void saveThingInfoImpl(ThingInfo *thingInfo) {
188+
if (thingInfo->thingId == NULL) {
189+
#ifdef ENABLE_DEBUG
190+
Serial.println(F("NULL thing ID. Can't save thing info."));
191+
#endif
192+
return;
193+
}
194+
195+
int thingIdSize = strlen(thingInfo->thingId);
196+
EEPROM.write(0, thingIdSize);
197+
for (int i = 0; i < thingIdSize; i++) {
198+
EEPROM.write(i + 1, *((thingInfo->thingId) + i));
199+
}
200+
201+
int position = 1 + thingIdSize;
202+
EEPROM.write(position, getDacStateByteValue(thingInfo->dacState));
203+
position++;
204+
205+
if (thingInfo->dacState == ALLOCATED ||
206+
thingInfo->dacState == CONFIGURED) {
207+
EEPROM.put(position, thingInfo->uplinkChannelBegin);
208+
position += 2;
209+
EEPROM.put(position, thingInfo->uplinkChannelEnd);
210+
position += 2;
211+
212+
EEPROM.write(position, thingInfo->uplinkAddressHighByte);
213+
position++;
214+
EEPROM.write(position, thingInfo->uplinkAddressLowByte);
215+
position++;
216+
217+
position = writeAddressToEepRom(thingInfo->address, position);
218+
}
219+
220+
#ifdef ENABLE_DEBUG
221+
Serial.println(F("ThingInfo saved."));
222+
#endif
223+
224+
debugOutThingInfo(thingInfo);
225+
}
226+
227+
void resetAll() {
228+
EEPROM.write(EEPROM.length() - 1, 0);
229+
}
230+
231+
void configureMcuBoard(const char *_modelName) {
232+
int modelNameLength = strlen(_modelName);
233+
modelName = malloc(sizeof(char) * (modelNameLength + 1));
234+
strcpy(modelName, _modelName);
235+
236+
#ifdef ENABLE_DEBUG
237+
configureSerial();
238+
Serial.println("Serial has configured.");
239+
setDebugOutputter(debugOutputImpl);
240+
#endif
241+
242+
registerThingInfoLoader(loadThingInfoImpl);
243+
registerThingInfoSaver(saveThingInfoImpl);
244+
registerResetter(resetImpl);
245+
registerTimer(getTimeImpl);
246+
247+
uint16_t eepRomLength = EEPROM.length();
248+
uint8_t lastByteOfEepRom = EEPROM.read(eepRomLength - 1);
249+
uint8_t nextToLastByteOfEepRom = EEPROM.read(eepRomLength - 2);
250+
uint8_t theThirdToLastByteOfEepRom = EEPROM.read(eepRomLength - 3);
251+
252+
if (lastByteOfEepRom != 0xff ||
253+
nextToLastByteOfEepRom != 0xfe ||
254+
theThirdToLastByteOfEepRom != 0xfd) {
255+
#ifdef ENABLE_DEBUG
256+
Serial.println(F("EEPROM not initialized. Initlize it now."));
257+
#endif
258+
initializeEepRom(eepRomLength);
259+
}
260+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef MUD_BOARD_ADAPTATION_H
2+
#define MUD_BOARD_ADAPTATION_H
3+
4+
#include <HardwareSerial.h>
5+
6+
#include "mud_configuration.h"
7+
8+
#ifdef ARDUINO_MICRO
9+
10+
#define SerialUart Serial1
11+
12+
#endif
13+
14+
#ifdef ARDUINO_UNO
15+
16+
#ifdef USE_SOFTWARE_SERIAL
17+
#include <SoftwareSerial.h>
18+
#define SerialUart softwareSerial
19+
#else
20+
#define SerialUart Serial
21+
#endif
22+
23+
#endif
24+
25+
void configureMcuBoard(const char *modelName);
26+
void printToSerialPort(char title[], uint8_t message[], int size);
27+
void resetAll();
28+
29+
#endif
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=mud_as32_ttl_100
2+
version=1.0.0-BETA2
3+
author=Dongger
4+
maintainer=Dongger <48190591@qq.com>
5+
sentence=AS32-TTL-100 mud adaptation
6+
paragraph=Mud adaptation for AS32-TTL-100 LoRa module
7+
category=Communication
8+
url=https://github.com/TheFirstLineOfCode/mud
9+
architectures=*
10+
includes=radio_module_adaptation.h

0 commit comments

Comments
 (0)