Skip to content

Commit a62b120

Browse files
committed
Made available use of timers and chatAT using defines, added commands, modified server (chatAt) to fix compilation problem
1 parent 84594b8 commit a62b120

File tree

6 files changed

+272
-59
lines changed

6 files changed

+272
-59
lines changed

extras/functions_generator_firmware/SKr3_waves/SKr3_waves.ino

Lines changed: 129 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,64 @@
2323
#include "led_range.h"
2424
#include "analogWave.h"
2525

26-
#define ANRES 9
26+
/* __________________________ CONFIGURATION DEFINES _________________________ */
27+
// #define USE_FIZ
28+
// #define USE_TIME
29+
#define USE_DEBUG_CODE
30+
31+
32+
/* _____________________________ OTHERS DEFINES _____________________________ */
33+
34+
#define ANRES 9
35+
#define RES 100
36+
#define SERIAL_AT Serial
37+
38+
/* ________TIMER_____________________________________________________________ */
39+
40+
#ifdef USE_TIMER
41+
#include "FspTimer.h"
42+
43+
static FspTimer timer;
44+
45+
/* -------------------------------------------------------------------------- */
46+
void timer_callback(timer_callback_args_t *arg) {
47+
/* -------------------------------------------------------------------------- */
48+
Serial.println("Timer 1 callback");
49+
}
50+
51+
/* -------------------------------------------------------------------------- */
52+
void set_up_timer() {
53+
/* -------------------------------------------------------------------------- */
54+
55+
uint8_t type;
56+
int8_t num = FspTimer::get_available_timer(type);
57+
58+
59+
if(num >= 0) {
60+
Serial.print("Timer Available ");
61+
Serial.println(type);
62+
timer.begin(TIMER_MODE_PERIODIC, type, num, 1,50 , timer_callback);
63+
timer.setup_overflow_irq();
64+
if(timer.open()) {
65+
Serial.println("FspTimer opened");
66+
}
67+
if(timer.start()) {
68+
Serial.println("FspTimer started");
69+
}
70+
}
71+
}
72+
73+
74+
#endif
75+
76+
/* ________CHAT AT___________________________________________________________ */
77+
78+
#ifdef USE_CHAT_AT
79+
#include "chATWrapper.h"
80+
static CAtWrapper at(&SERIAL_AT);
81+
82+
#endif
2783

28-
#define RES 100
2984

3085
// Firmware version
3186
uint8_t version[2]={0,6};
@@ -104,8 +159,22 @@ void comm_request(){
104159
}
105160

106161

162+
163+
107164
void setup() {
108165
Serial.begin(115200);
166+
167+
#ifdef USE_DEBUG_CODE
168+
delay(3000);
169+
Serial.println("STARTED SETUP");
170+
#endif
171+
172+
#ifdef USE_TIMER
173+
set_up_timer();
174+
#endif
175+
176+
177+
109178
generate_wave();
110179
pinMode(PH_1,INPUT);
111180
pinMode(PH_2,INPUT);
@@ -125,63 +194,72 @@ void setup() {
125194
rephase=0;
126195
time_rephase=millis();
127196

128-
/* 20230707 keep resolution to 0-511 also if core now is correct
197+
198+
#ifdef USE_ADC_FIX_IN_SCIENCE_KIT_REV_3_0
199+
/* 20230707 keep resolution to 0-511 also if core now is correct
129200
and goes from 1-1023 */
130-
analogReadResolution(ANRES);
201+
analogReadResolution(ANRES);
202+
#endif
131203
}
132204

133205

134206

135207
void loop() {
136-
frequency_1.refresh();
137-
frequency_2.refresh();
138-
range.update();
139-
140-
freq1=frequency_1.getGaugeValue()*range.getRange1();
141-
freq2=frequency_2.getGaugeValue()*range.getRange2();
142-
143-
phase1=analogRead(PH_1)/step;
144-
if (phase1>(RES+10)){
145-
phase1=0;
146-
}
147-
148-
phase2=analogRead(PH_2)/step;
149-
if (phase2>RES){
150-
phase2=0;
151-
}
152-
153-
if (freq1!=previous_frequency_1){
154-
wave1.freq(freq1);
155-
previous_frequency_1=freq1;
156-
wave1.sync(wave2);
157-
}
158-
159-
if (freq2!=previous_frequency_2){
160-
wave2.freq(freq2);
161-
previous_frequency_2=freq2;
162-
wave2.sync(wave1);
163-
}
164-
165-
if (millis()-time_rephase>500){
166-
if (phase1!=previous_phase_1){
167-
wave1.offset(phase1/2);
168-
previous_phase_1=phase1;
169-
if (phase2==0){
170-
wave1.sync(wave2);
208+
#ifdef USE_DEBUG_CODE
209+
//Serial.println("Msin loop running");
210+
#endif
211+
212+
213+
frequency_1.refresh();
214+
frequency_2.refresh();
215+
range.update();
216+
217+
freq1=frequency_1.getGaugeValue()*range.getRange1();
218+
freq2=frequency_2.getGaugeValue()*range.getRange2();
219+
220+
phase1=analogRead(PH_1)/step;
221+
if (phase1>(RES+10)){
222+
phase1=0;
223+
}
224+
225+
phase2=analogRead(PH_2)/step;
226+
if (phase2>RES){
227+
phase2=0;
228+
}
229+
230+
if (freq1!=previous_frequency_1){
231+
wave1.freq(freq1);
232+
previous_frequency_1=freq1;
233+
wave1.sync(wave2);
234+
}
235+
236+
if (freq2!=previous_frequency_2){
237+
wave2.freq(freq2);
238+
previous_frequency_2=freq2;
239+
wave2.sync(wave1);
240+
}
241+
242+
if (millis()-time_rephase>500){
243+
if (phase1!=previous_phase_1){
244+
wave1.offset(phase1/2);
245+
previous_phase_1=phase1;
246+
if (phase2==0){
247+
wave1.sync(wave2);
248+
}
171249
}
172-
}
173-
if (phase2!=previous_phase_2){
174-
wave2.offset(phase2/2);
175-
previous_phase_2=phase2;
176-
if (phase1==0){
177-
wave2.sync(wave1);
250+
if (phase2!=previous_phase_2){
251+
wave2.offset(phase2/2);
252+
previous_phase_2=phase2;
253+
if (phase1==0){
254+
wave2.sync(wave1);
255+
}
178256
}
179-
}
180-
time_rephase=millis();
181-
}
182-
183-
257+
time_rephase=millis();
258+
}
184259

260+
#ifdef USE_CHAT_AT
261+
at.run();
262+
#endif
185263

186-
delay(1);
264+
delay(1);
187265
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "chATWrapper.h"
2+
#include "chAt_cmds.h"
3+
4+
5+
6+
/* -------------------------------------------------------------------------- */
7+
int CAtWrapper::run() {
8+
/* -------------------------------------------------------------------------- */
9+
at_srv.run();
10+
}
11+
12+
13+
14+
/* -------------------------------------------------------------------------- */
15+
CAtWrapper::CAtWrapper(arduino::HardwareSerial *s) : mode(0) {
16+
/* -------------------------------------------------------------------------- */
17+
/* set up serial */
18+
serial = s;
19+
20+
/* set up chatAt server callbacks */
21+
at_srv.set_io_callback({
22+
.callback_io_read = [this](auto buf, auto len) {
23+
if (!serial->available()) {
24+
yield();
25+
return (unsigned int)0;
26+
}
27+
return serial->readBytes(buf, min((unsigned int)serial->available(), len));
28+
},
29+
.callback_io_write = [this](auto buf, auto len) {
30+
return serial->write(buf, len);
31+
},
32+
});
33+
34+
at_srv.set_command_callback([this](chAT::Server & srv, const std::string & command) {
35+
auto it = command_table.find(command);
36+
37+
if (it == command_table.end()) {
38+
return chAT::CommandStatus::ERROR;
39+
}
40+
else {
41+
return it->second(srv, srv.parser());
42+
}
43+
});
44+
45+
add_cmds();
46+
47+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef CH_AT_WRAPPER_H
2+
#define CH_AT_WRAPPER_H
3+
4+
#include "Arduino.h"
5+
#include "chAT.hpp"
6+
7+
using namespace SudoMaker;
8+
9+
class CAtWrapper {
10+
private:
11+
void add_cmds();
12+
std::unordered_map<std::string, std::function<chAT::CommandStatus(chAT::Server&, chAT::ATParser&)>> command_table;
13+
chAT::Server at_srv;
14+
arduino::HardwareSerial *serial;
15+
16+
int mode;
17+
18+
public:
19+
CAtWrapper(arduino::HardwareSerial *s);
20+
CAtWrapper() = delete ;
21+
int run();
22+
};
23+
24+
25+
26+
27+
28+
#endif
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef CHAT_COMMANDS_H
2+
#define CHAT_COMMANDS_H
3+
4+
#include "chATWrapper.h"
5+
#include "commands.h"
6+
7+
void CAtWrapper::add_cmds() {
8+
9+
/* ....................................................................... */
10+
command_table[_ENTER_PRODUCTION_MODE] = [this](auto & srv, auto & parser) {
11+
/* ....................................................................... */
12+
switch (parser.cmd_mode) {
13+
case chAT::CommandMode::Write: {
14+
Serial.println("A");
15+
16+
if (parser.args.size() != 1) {
17+
return chAT::CommandStatus::ERROR;
18+
}
19+
20+
auto &production_code = parser.args[0];
21+
Serial.println(production_code.c_str());
22+
if(production_code == _PRODUCTION_CODE_MODE) {
23+
return chAT::CommandStatus::OK;
24+
}
25+
26+
27+
return chAT::CommandStatus::ERROR;
28+
}
29+
default:
30+
return chAT::CommandStatus::ERROR;
31+
}
32+
return chAT::CommandStatus::OK;
33+
};
34+
35+
}
36+
37+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef SCIENCE_KIT_COMMANDS_H
2+
#define SCIENCE_KIT_COMMANDS_H
3+
4+
#define _AT "AT"
5+
#define _ENDL "\r\n"
6+
7+
#define _PRODUCTION_CODE_MODE "583279539534"
8+
9+
#define _ENTER_PRODUCTION_MODE "+PRODMODE"
10+
#define _FREQ_1_LED_SET "+LED1SET"
11+
#define _FREQ_1_LED_RESET "+LED1RESET"
12+
#define _FREQ_2_LED_SET "+LED2SET"
13+
#define _FREQ_2_LED_RESET "+LED2RESET"
14+
#define _RANGE_1_LED_SET "+RLED1SET"
15+
#define _RANGE_1_LED_RESET "+RLED1RESET"
16+
#define _RANGE_2_LED_SET "+RLED2SET"
17+
#define _RANGE_2_LED_RESET "+RLED2RESET"
18+
19+
20+
21+
#endif

extras/functions_generator_firmware/SKr3_waves/server.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,19 @@ namespace SudoMaker::chAT {
195195
}
196196

197197
void write_str(std::string str) {
198-
write_raw(data_holder{
199-
.position = 0,
200-
.holder = std::move(str),
201-
});
198+
data_holder dh;
199+
dh.position = 0;
200+
dh.holder = std::move(str);
201+
202+
write_raw(dh);
202203
}
203204

204205
void write_vec8(std::vector<uint8_t> vec8) {
205-
write_raw(data_holder{
206-
.position = 0,
207-
.holder = std::move(vec8),
208-
});
206+
data_holder dh;
207+
dh.position = 0;
208+
dh.holder = std::move(vec8);
209+
210+
write_raw(dh);
209211
}
210212

211213
void write_error() {

0 commit comments

Comments
 (0)