Skip to content

Commit 634ba78

Browse files
committed
use thread
1 parent 955f8b6 commit 634ba78

File tree

2 files changed

+86
-84
lines changed

2 files changed

+86
-84
lines changed

examples/utility/Provisioning_2.0/OptaFactoryTest.cpp

Lines changed: 86 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
#ifdef ARDUINO_OPTA
1010
#include "OptaFactoryTest.h"
11-
11+
#include <Arduino.h>
12+
#include <mbed.h>
1213

1314
#define VID_FINDER 0x35D1
1415
#define VID_ARDUINO 0x2341
@@ -44,8 +45,86 @@ const uint8_t n_input[N_ANALOG_INPUTS] = {A0, A1, A2, A3, A4, A5, A6, A7};
4445
const uint8_t n_led[N_LED] = {LEDR, LEDG, LEDB, 154u, 155u, 157u, 153u};
4546
float v_input[N_ANALOG_INPUTS];
4647

48+
static rtos::Thread threadRs485;
49+
volatile uint32_t rs485_pulse = 0;
50+
volatile bool rs485_test_done = false;
51+
4752
OptaBoardInfo* boardInfo();
4853

54+
void rs485IRQ() {
55+
rs485_pulse++;
56+
}
57+
58+
void rs485Manage() {
59+
while(1) {
60+
delay(1000);
61+
bool rs485_ok = false;
62+
uint32_t t_rs485_pulse = 0;
63+
digitalWrite(MY_RS485_RE_PIN, HIGH);
64+
digitalWrite(MY_RS485_DE_PIN, HIGH);
65+
delay(10);
66+
67+
for(uint32_t i = 0; i < N_PULSE; i++)
68+
{
69+
digitalWrite(MY_RS485_TX_PIN, HIGH);
70+
delay(1);
71+
digitalWrite(MY_RS485_TX_PIN, LOW);
72+
delay(1);
73+
}
74+
delay(10);
75+
digitalWrite(MY_RS485_DE_PIN, LOW);
76+
digitalWrite(MY_RS485_RE_PIN, LOW);
77+
78+
/* Search start of incoming transmission */
79+
rs485_pulse = 0;
80+
while((rs485_pulse == 0) && (t_rs485_pulse < 200))
81+
{
82+
t_rs485_pulse++;
83+
delay(1);
84+
}
85+
86+
rs485_pulse = 0;
87+
88+
if(t_rs485_pulse < 200)
89+
{
90+
/* Receive data */
91+
t_rs485_pulse = 0;
92+
uint32_t rs485_pulse_old = 0;
93+
for(t_rs485_pulse = 0; t_rs485_pulse < 20; t_rs485_pulse++)
94+
{
95+
if(rs485_pulse_old != rs485_pulse)
96+
{
97+
rs485_pulse_old = rs485_pulse;
98+
t_rs485_pulse = 0;
99+
}
100+
delay(10);
101+
}
102+
103+
/* End of receiving */
104+
if(rs485_pulse > 0)
105+
{
106+
if((rs485_pulse == N_PULSE) || (rs485_pulse == N_PULSE + 1))
107+
{
108+
Serial.println("RS485 check OK");
109+
rs485_ok = true;
110+
}
111+
}
112+
}
113+
114+
if(rs485_ok == true)
115+
{
116+
digitalWrite(RL1, HIGH);
117+
digitalWrite(LED1_SYS, HIGH);
118+
delay(1000);
119+
digitalWrite(RL1, LOW);
120+
digitalWrite(LED1_SYS, LOW);
121+
rs485_test_done = true;
122+
while(1);
123+
}
124+
125+
}
126+
}
127+
49128
void OptaFactoryTestClass::begin() {
50129
pinMode(LED1_SYS, OUTPUT);
51130
pinMode(LED2_SYS, OUTPUT);
@@ -161,11 +240,10 @@ void OptaFactoryTestClass::optaIDTest() {
161240
digitalWrite(MY_RS485_RE_PIN, LOW);
162241

163242
pinMode(MY_RS485_RX_PIN, INPUT);
164-
attachInterrupt(digitalPinToInterrupt(MY_RS485_RX_PIN), rs485Rcv, FALLING);
165-
_nextBoardInfoPrint = millis();
166-
_nextRS485Run = millis() + 2000;
243+
attachInterrupt(digitalPinToInterrupt(MY_RS485_RX_PIN), rs485IRQ, FALLING);
244+
threadRs485.start(rs485Manage);
167245
}
168-
246+
_nextBoardInfoPrint = millis();
169247
}
170248

171249
bool OptaFactoryTestClass::poll() {
@@ -190,11 +268,11 @@ bool OptaFactoryTestClass::poll() {
190268
printModel();
191269
}
192270

193-
if(_nextRS485Run < millis() && _info->_board_functionalities.rs485 == 1 && _rs485_test_done == false)
271+
if(rs485_test_done == true)
194272
{
195-
_nextRS485Run = millis() + 1000;
196-
rs485Manage();
273+
_test_running = false;
197274
}
275+
198276
return _test_running;
199277
}
200278

@@ -302,73 +380,6 @@ void OptaFactoryTestClass::inputManage(void)
302380
}
303381
}
304382

305-
void OptaFactoryTestClass::rs485Manage() {
306-
bool rs485_ok = false;
307-
uint32_t t_rs485_pulse = 0;
308-
digitalWrite(MY_RS485_RE_PIN, HIGH);
309-
digitalWrite(MY_RS485_DE_PIN, HIGH);
310-
delay(10);
311-
312-
for(uint32_t i = 0; i < N_PULSE; i++)
313-
{
314-
digitalWrite(MY_RS485_TX_PIN, HIGH);
315-
delay(1);
316-
digitalWrite(MY_RS485_TX_PIN, LOW);
317-
delay(1);
318-
}
319-
delay(10);
320-
digitalWrite(MY_RS485_DE_PIN, LOW);
321-
digitalWrite(MY_RS485_RE_PIN, LOW);
322-
323-
/* Search start of incoming transmission */
324-
_rs485_pulse = 0;
325-
while((_rs485_pulse == 0) && (t_rs485_pulse < 200))
326-
{
327-
t_rs485_pulse++;
328-
delay(1);
329-
}
330-
331-
_rs485_pulse = 0;
332-
333-
if(t_rs485_pulse < 200)
334-
{
335-
/* Receive data */
336-
t_rs485_pulse = 0;
337-
uint32_t rs485_pulse_old = 0;
338-
for(t_rs485_pulse = 0; t_rs485_pulse < 20; t_rs485_pulse++)
339-
{
340-
if(rs485_pulse_old != _rs485_pulse)
341-
{
342-
rs485_pulse_old = _rs485_pulse;
343-
t_rs485_pulse = 0;
344-
}
345-
delay(10);
346-
}
347-
348-
/* End of receiving */
349-
if(_rs485_pulse > 0)
350-
{
351-
if((_rs485_pulse == N_PULSE) || (_rs485_pulse == N_PULSE + 1))
352-
{
353-
Serial.println("RS485 check OK");
354-
rs485_ok = true;
355-
}
356-
}
357-
}
358-
359-
if(rs485_ok == true)
360-
{
361-
digitalWrite(RL1, HIGH);
362-
digitalWrite(LED1_SYS, HIGH);
363-
delay(1000);
364-
digitalWrite(RL1, LOW);
365-
digitalWrite(LED1_SYS, LOW);
366-
_rs485_test_done = true;
367-
_test_running = false;
368-
}
369-
}
370-
371-
372383
void OptaFactoryTestClass::printInfo() {
373384
Serial.print("\n");
374385
Serial.print("**********************************\n");
@@ -484,10 +495,6 @@ void OptaFactoryTestClass::printModel(void)
484495
Serial.println(" <<<\n");
485496
}
486497

487-
void OptaFactoryTestClass::rs485Rcv() {
488-
_rs485_pulse++;
489-
}
490-
491498
OptaFactoryTestClass OptaFactoryTest;
492499

493500
#endif

examples/utility/Provisioning_2.0/OptaFactoryTest.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,14 @@ class OptaFactoryTestClass {
1616
private:
1717
void ledManage();
1818
void inputManage();
19-
void rs485Manage();
2019
void printInfo();
2120
void printModel();
22-
static void rs485Rcv();
23-
static inline uint32_t _rs485_pulse = 0;
2421
bool _all_on = false;
25-
bool _rs485_test_done = false;
2622
bool _test_running = false;
2723
OptaBoardInfo *_info;
2824
uint32_t _ms10 = 0;
2925
uint32_t _ms100 = 0;
3026
uint32_t _nextBoardInfoPrint = 0;
31-
uint32_t _nextRS485Run = 0;
3227
};
3328

3429
extern OptaFactoryTestClass OptaFactoryTest;

0 commit comments

Comments
 (0)