Skip to content

Commit aaebbec

Browse files
Add a custom Gate example
1 parent 14ea748 commit aaebbec

File tree

9 files changed

+396
-0
lines changed

9 files changed

+396
-0
lines changed

examples/projects/product/README.md

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.pio/
2+
.vscode/
3+
.gitignore
4+
README.md
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM python:3 as builder
2+
3+
LABEL maintainer="Benjamin Christau <[email protected]>" \
4+
app_name="gate_wscom"
5+
6+
ENV PLATFORMIO_CORE_DIR="/home"
7+
8+
WORKDIR /home/app
9+
10+
COPY . .
11+
12+
RUN pip install --upgrade pip setuptools wheel platformio && \
13+
rm -rf /root/.cache/pip
14+
15+
RUN platformio run \
16+
--environment native \
17+
-d ./examples/projects/native/gate_wscom
18+
19+
CMD [ "./examples/projects/native/gate_wscom/.pio/build/native/program"]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<a href="https://luos.io"><img src="https://uploads-ssl.webflow.com/601a78a2b5d030260a40b7ad/603e0cc45afbb50963aa85f2_Gif%20noir%20rect.gif" alt="Luos logo" title="Luos" align="right" height="100" /></a>
2+
3+
![](https://github.com/Luos-io/luos_engine/actions/workflows/build.yml/badge.svg)
4+
[![](https://img.shields.io/github/license/Luos-io/luos_engine)](https://github.com/Luos-io/luos_engine/blob/master/LICENSE)
5+
6+
[![](https://img.shields.io/badge/Luos-Documentation-34A3B4)](https://www.luos.io/docs/)
7+
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/luos/library/luos_engine.svg)](https://registry.platformio.org/libraries/luos_engine/luos_engine)
8+
9+
[![](https://img.shields.io/discord/902486791658041364?label=Discord&logo=discord&style=social)](http://bit.ly/JoinLuosDiscord)
10+
[![](https://img.shields.io/badge/LinkedIn-Share-0077B5?style=social&logo=linkedin)](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fgithub.com%2Fluos-io)
11+
12+
# Custom Gate project example :bulb:
13+
14+
This project demonstrate how to make a custom gate with custom Json conversion.
15+
16+
## How to compile the code :computer:
17+
18+
1. Download and install [Platformio](https://platformio.org/platformio-ide)
19+
2. Open this folder into Platformio
20+
3. Build (Platformio will do the rest)
21+
22+
## Don't hesitate to read [our documentation](https://www.luos.io/docs/), or to post your questions/issues on the [Luos' Discord](http://bit.ly/JoinLuosDiscord). :books:
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
2+
3+
/******************************************************************************
4+
* @file node_config.h
5+
* @brief This file allow you to use standard preprocessor definitions to
6+
* configure your project, Luos and Luos HAL libraries
7+
*
8+
* # Introduction
9+
* This file is for the luos user. You may here configure your project and
10+
* define your custom Luos service and custom Luos command for your product
11+
*
12+
* Luos libraries offer a minimal standard configuration to optimize
13+
* memory usage. In some case you have to modify standard value to fit
14+
* with your need concerning among of data transiting through the network
15+
* or network speed for example
16+
*
17+
* Luos libraries can be use with a lot a MCU family. Luos compagny give you
18+
* a default configuration, for specific MCU family, in robus_hal_config.h.
19+
* This configuration can be modify here to fit with you design by
20+
* preprocessor definitions of MCU Hardware needs
21+
*
22+
* # Usage
23+
* This file should be place a the root folder of your project and include
24+
* where build flag preprocessor definitions are define in your IDE
25+
* -include node_config.h
26+
*
27+
* @author Luos
28+
* @version 0.0.0
29+
******************************************************************************/
30+
#ifndef _NODE_CONFIG_H_
31+
#define _NODE_CONFIG_H_
32+
33+
/*******************************************************************************
34+
* PROJECT DEFINITION
35+
*******************************************************************************/
36+
37+
/*******************************************************************************
38+
* LUOS LIBRARY DEFINITION
39+
*******************************************************************************
40+
* Define | Default Value | Description
41+
* :---------------------|------------------------------------------------------
42+
* MAX_LOCAL_SERVICE_NUMBER | 5 | Service number in the node
43+
* MAX_NODE_NUMBER | 20 | Node number in the device
44+
* MAX_SERVICE_NUMBER | 20 | Service number in the device
45+
* MSG_BUFFER_SIZE | 3*SIZE_MSG_MAX (405 Bytes) | Size in byte of the Luos buffer TX and RX
46+
* MAX_MSG_NB | 2*MAX_LOCAL_SERVICE_NUMBER | Message number in Luos buffer
47+
* NBR_PORT | 2 | PTP Branch number Max 8
48+
* NBR_RETRY | 10 | Send Retry number in case of NACK or collision
49+
******************************************************************************/
50+
#define MAX_LOCAL_SERVICE_NUMBER 2
51+
#define MAX_LOCAL_PROFILE_NUMBER 1
52+
#define MAX_MSG_NB 200
53+
#define MSG_BUFFER_SIZE 8192
54+
55+
/*******************************************************************************
56+
* LUOS HAL LIBRARY DEFINITION
57+
*******************************************************************************
58+
* Define | Description
59+
* :-----------------------|-----------------------------------------------
60+
* MCUFREQ | Put your the MCU frequency (value in Hz)
61+
* TIMERDIV | Timer divider clock (see your clock configuration)
62+
* USE_CRC_HW | define to 0 if there is no Module CRC in your MCU
63+
* USE_TX_IT | define to 1 to not use DMA transfers for Luos Tx
64+
*
65+
* PORT_CLOCK_ENABLE | Enable clock for port
66+
* PTPx | A,B,C,D etc. PTP Branch Pin/Port/IRQ
67+
* TX_LOCK_DETECT | Disable by default use if not busy flag in USART Pin/Port/IRQ
68+
* RX_EN | Rx enable for driver RS485 always on Pin/Port
69+
* TX_EN | Tx enable for driver RS485 Pin/Port
70+
* COM_TX | Tx USART Com Pin/Port/Alternate
71+
* COM_RX | Rx USART Com Pin/Port/Alternate
72+
* PINOUT_IRQHANDLER | Callback function for Pin IRQ handler
73+
74+
* ROBUS_COM_CLOCK_ENABLE | Enable clock for USART
75+
* ROBUS_COM | USART number
76+
* ROBUS_COM_IRQ | USART IRQ number
77+
* ROBUS_COM_IRQHANDLER | Callback function for USART IRQ handler
78+
79+
* ROBUS_DMA_CLOCK_ENABLE | Enable clock for DMA
80+
* ROBUS_DMA | DMA number
81+
* ROBUS_DMA_CHANNEL | DMA channel (depending on MCU DMA may need special config)
82+
83+
* ROBUS_TIMER_CLOCK_ENABLE | Enable clock for Timer
84+
* ROBUS_TIMER | Timer number
85+
* ROBUS_TIMER_IRQ | Timer IRQ number
86+
* ROBUS_TIMER_IRQHANDLER | Callback function for Timer IRQ handler
87+
******************************************************************************/
88+
89+
/*******************************************************************************
90+
* FLASH CONFIGURATION FOR APP WITH BOOTLOADER
91+
********************************************************************************
92+
* Define | Default Value | Description
93+
* :---------------------|------------------------------------------------------
94+
* BOOT_START_ADDRESS | FLASH_BASE = 0x8000000 | Start address of Bootloader in flash
95+
* SHARED_MEMORY_ADDRESS | 0x0800C000 | Start address of shared memory to save boot flag
96+
* APP_START_ADDRESS | 0x0800C800 | Start address of application with bootloader
97+
* APP_END_ADDRESS | FLASH_BANK1_END=0x0801FFFF | End address of application with bootloader
98+
******************************************************************************/
99+
100+
/*******************************************************************************
101+
* GATE SERIAL COM DEFINITION
102+
*******************************************************************************
103+
* Define | Default Value | Description
104+
* :-------------------------|------------------------------------------------------
105+
* GATE_BUFF_SIZE | 1024 | Json receive buffer size
106+
* PIPE_RX_BUFFER_SIZE | 1024 | Receive pipe buffer size
107+
* PIPE_TX_BUFFER_SIZE | 2048 | Transmit pipe buffer size
108+
* INIT_TIME | 150 | Wait init time before first detection
109+
******************************************************************************/
110+
#define GATE_BUFF_SIZE 65000
111+
#define PIPE_RX_BUFFER_SIZE 65000
112+
#define PIPE_TX_BUFFER_SIZE 65000
113+
#define SERIAL_RX_BUFFER_SIZE 65000
114+
#define INIT_TIME 150
115+
#define GATE_REFRESH_TIME_S 0.05f
116+
#define SERIAL_PORT "/dev/cu.usbmodem1202" // "/dev/cu.usbserial-D308N897"
117+
118+
#endif /* _NODE_CONFIG_H_ */
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
[platformio]
11+
default_envs = native_serial
12+
13+
[env:native_serial]
14+
lib_ldf_mode =off
15+
lib_extra_dirs =
16+
$PROJECT_DIR/../../../../tool_services/
17+
$PROJECT_DIR/../../../../../
18+
$PROJECT_DIR/../../../../network/
19+
platform = native
20+
lib_deps =
21+
luos_engine@^3.1.0
22+
serial_network
23+
Pipe
24+
Gate
25+
build_unflags = -Os
26+
build_flags =
27+
-I inc
28+
-I ../
29+
-include node_config.h
30+
-O1
31+
-lpthread
32+
-lm
33+
-D LUOSHAL=NATIVE
34+
-D GATEFORMAT=TinyJSON
35+
-D PIPEMODE=WS
36+
-D PIPEHAL=native
37+
-D PIPE_WS_SERVER_ADDR=\"ws://localhost:9342\" ; Watch out you need to escape the " using \
38+
build_type = debug
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include "custom-json.h"
2+
#include "product_config.h"
3+
#include <stdio.h>
4+
5+
// This function are called by the gate conversion functions in case of an unknown type.
6+
// This functions allow you to easily add and manage your custom type and commands.
7+
8+
// This function is called by the gate to convert a service type into a string.
9+
// This is typically used in the end of detection to create a Json representing the device routing table.
10+
// The name of the type will be used by pyluos to create the right object.
11+
const char *Convert_CustomStringFromType(luos_type_t type)
12+
{
13+
if (type == POINT_2D)
14+
{
15+
return "point_2D";
16+
}
17+
return NULL;
18+
}
19+
20+
// This function is called by the gate to convert a piece of Json into a message.
21+
// This is typically used when a Json is received by the gate with an unknown property.
22+
// You can use it to compose your own message out of the Json data and send it to the right service.
23+
void Convert_CustomJsonToMsg(service_t *service, uint16_t target_id, char *property, const json_t *jobj, char *json_str)
24+
{
25+
msg_t msg;
26+
msg.header.target_mode = IDACK;
27+
msg.header.target = target_id;
28+
// Target linear position 2D
29+
if (property && !strcmp(property, "linear_pos_2D"))
30+
{
31+
// Check the size of the array. If we have more than one data, this mean that this data is a binary size of a trajectory. If we have 2 data, this is an unique point.
32+
json_t const *item = json_getChild(jobj);
33+
if (json_getSibling(item) != NULL)
34+
{
35+
// We only have one point in this data
36+
pos_2d_t pos;
37+
pos.x = (uint16_t)json_getInteger(item);
38+
item = json_getSibling(item);
39+
pos.y = (uint16_t)json_getInteger(item);
40+
// Create the message
41+
msg.header.cmd = LINEAR_POSITION_2D;
42+
msg.header.size = sizeof(pos_2d_t);
43+
memcpy(msg.data, &pos, sizeof(pos_2d_t));
44+
// Send the message
45+
Luos_SendMsg(service, &msg);
46+
}
47+
else
48+
{
49+
int i = 0;
50+
// This is a binary
51+
int size = (int)json_getInteger(item);
52+
// Find the first \r of the current json_str
53+
for (i = 0; i < GATE_BUFF_SIZE; i++)
54+
{
55+
if (json_str[i] == '\n')
56+
{
57+
i++;
58+
break;
59+
}
60+
}
61+
if (i < GATE_BUFF_SIZE - 1)
62+
{
63+
// Create the message
64+
msg.header.cmd = LINEAR_POSITION_2D;
65+
Luos_SendData(service, &msg, &json_str[i], (unsigned int)size);
66+
}
67+
}
68+
return;
69+
}
70+
if (property && !strcmp(property, "buffer_mode"))
71+
{
72+
msg.data[0] = (char)json_getInteger(jobj);
73+
msg.header.cmd = BUFFER_MODE;
74+
msg.header.size = sizeof(char);
75+
Luos_SendMsg(service, &msg);
76+
return;
77+
}
78+
}
79+
80+
// This function is called by the gate to convert a message into a piece of Json.
81+
// This is typically used when a message is received by the gate with an unknown command.
82+
// You can use it to compose your own piece of Json out of the message data.
83+
void Convert_CustomMsgToJson(msg_t *msg, char *data)
84+
{
85+
if (msg->header.cmd == LINEAR_POSITION_2D)
86+
{
87+
// This is our custom message, so we can convert it to JSON
88+
// In this case we will don't need it but I did the code for the sake of the example.
89+
if (msg->header.size == sizeof(pos_2d_t))
90+
{
91+
// Size ok, now fill the struct from msg data
92+
pos_2d_t pos;
93+
memcpy(&pos, msg->data, msg->header.size);
94+
// create the Json content
95+
sprintf(data, "\"linear_pos_2D\":[%2d,%2d],", pos.x, pos.y);
96+
}
97+
}
98+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "luos_engine.h"
2+
#include "serial_network.h"
3+
#include "pipe.h"
4+
#include "gate.h"
5+
#include <pthread.h>
6+
7+
#ifndef WIN32
8+
#include <execinfo.h>
9+
#include <signal.h>
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <unistd.h>
13+
#endif
14+
15+
void *Gate_Pipe_LoopThread(void *vargp)
16+
{
17+
while (1)
18+
{
19+
Pipe_Loop();
20+
Gate_Loop();
21+
}
22+
return NULL;
23+
}
24+
#ifndef _WIN32
25+
void handler(int sig)
26+
{
27+
void *array[10];
28+
size_t size;
29+
30+
// get void*'s for all entries on the stack
31+
size = backtrace(array, 10);
32+
33+
// print out all the frames to stderr
34+
fprintf(stderr, "Error: signal %d:\n", sig);
35+
backtrace_symbols_fd(array, size, STDERR_FILENO);
36+
exit(1);
37+
}
38+
#endif
39+
40+
int main(void)
41+
{
42+
#ifndef _WIN32
43+
signal(SIGSEGV, handler); // install our handler
44+
#endif
45+
Luos_Init();
46+
Serial_Init();
47+
Pipe_Init();
48+
Gate_Init();
49+
// Create a thread to convert messages into Json and steam them using Websocket
50+
// pthread_t thread_id;
51+
// pthread_create(&thread_id, NULL, Gate_Pipe_LoopThread, NULL);
52+
while (1)
53+
{
54+
Luos_Loop();
55+
Serial_Loop();
56+
Pipe_Loop();
57+
Gate_Loop();
58+
}
59+
}

0 commit comments

Comments
 (0)