Skip to content

Commit fbc6cda

Browse files
author
n.danilyuk
committed
Added AIO api
1 parent 41141af commit fbc6cda

File tree

9 files changed

+243
-4
lines changed

9 files changed

+243
-4
lines changed

src/SCPI_RP.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ void SCPIRedPitaya::initUARTStream(Stream *serial) {
2828
g_base_io = u;
2929
system.setInterface(u);
3030
dio.setInterface(u);
31+
aio.setInterface(u);
3132
}

src/SCPI_RP.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <Stream.h>
1616
#include <stdint.h>
1717

18+
#include "scpi/scpi_rp_aio.h"
1819
#include "scpi/scpi_rp_dio.h"
1920
#include "scpi/scpi_rp_system.h"
2021

@@ -36,6 +37,7 @@ class SCPIRedPitaya {
3637

3738
SCPISystem system;
3839
SCPIDio dio;
40+
SCPIAio aio;
3941
};
4042

4143
} // namespace scpi_rp

src/aio/aio.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include "aio.h"
2+
3+
#include <stdint.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
using namespace scpi_rp;
9+
10+
bool scpi_rp::setAIORst(BaseIO *io) {
11+
constexpr char cmd[] = "ANALOG:RST\r\n";
12+
if (!io->writeStr(cmd)) {
13+
io->writeCommandSeparator();
14+
return false;
15+
}
16+
return true;
17+
}
18+
19+
bool scpi_rp::setAIOValue(BaseIO *io, EAIOPin pin, float value) {
20+
auto writeState = [&](float value) {
21+
char buffer[10];
22+
snprintf(buffer, sizeof(buffer), ",%f", value);
23+
if (!io->writeStr(buffer)) {
24+
io->writeStr(SCPI_COMMAND_SEPARATOR);
25+
return false;
26+
}
27+
return io->writeStr(SCPI_COMMAND_SEPARATOR);
28+
};
29+
30+
if (pin < AOUT_0 || pin > AOUT_3) return false;
31+
if (value < 0 || value > 1.8f) return false;
32+
33+
constexpr char cmd[] = "ANALOG:PIN ";
34+
char buffer[3];
35+
if (!io->writeStr(cmd)) {
36+
io->writeStr(SCPI_COMMAND_SEPARATOR);
37+
return false;
38+
}
39+
40+
if (!io->writeStr("AOUT")) {
41+
io->writeStr(SCPI_COMMAND_SEPARATOR);
42+
return false;
43+
}
44+
itoa(pin, buffer, 10);
45+
if (!io->writeStr(buffer)) {
46+
io->writeStr(SCPI_COMMAND_SEPARATOR);
47+
return false;
48+
}
49+
return writeState(value);
50+
}
51+
52+
bool scpi_rp::getAIOValue(BaseIO *io, EAIOPin pin, float *_value) {
53+
auto readValue = [&]() {
54+
auto value = io->read();
55+
if (value.isValid) {
56+
*_value = atof(value.value);
57+
io->flushCommand(value.next_value);
58+
return true;
59+
}
60+
return false;
61+
};
62+
63+
char buffer[3];
64+
constexpr char cmd[] = "ANALOG:PIN? ";
65+
if (!io->writeStr(cmd)) {
66+
io->writeStr(SCPI_COMMAND_SEPARATOR);
67+
return false;
68+
}
69+
70+
if (pin >= AIN_0 && pin <= AIN_3) {
71+
if (!io->writeStr("AIN")) {
72+
io->writeStr(SCPI_COMMAND_SEPARATOR);
73+
return false;
74+
}
75+
itoa(pin, buffer, 10);
76+
if (!io->writeStr(buffer)) {
77+
io->writeStr(SCPI_COMMAND_SEPARATOR);
78+
return false;
79+
}
80+
io->writeStr(SCPI_COMMAND_SEPARATOR);
81+
return readValue();
82+
}
83+
84+
if (pin >= AOUT_0 && pin <= AOUT_3) {
85+
if (!io->writeStr("AOUT")) {
86+
io->writeStr(SCPI_COMMAND_SEPARATOR);
87+
return false;
88+
}
89+
itoa(pin - AOUT_0, buffer, 10);
90+
if (!io->writeStr(buffer)) {
91+
io->writeStr(SCPI_COMMAND_SEPARATOR);
92+
return false;
93+
}
94+
io->writeStr(SCPI_COMMAND_SEPARATOR);
95+
return readValue();
96+
}
97+
return false;
98+
}

src/aio/aio.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef AIO_H
2+
#define AIO_H
3+
4+
#include "aio_enums.h"
5+
#include "common/base_io.h"
6+
#include "common/structs.h"
7+
8+
namespace scpi_rp {
9+
10+
bool setAIORst(BaseIO *io);
11+
bool setAIOValue(BaseIO *io, EAIOPin pin, float value);
12+
bool getAIOValue(BaseIO *io, EAIOPin pin, float *value);
13+
14+
} // namespace scpi_rp
15+
16+
#endif

src/aio/aio_enums.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef AIO_ENUMS_H
2+
#define AIO_ENUMS_H
3+
4+
namespace scpi_rp {
5+
6+
/**
7+
Structure with return values ​​from the server
8+
*/
9+
enum EAIOPin {
10+
AIN_0 = 0, // Analog input 0
11+
AIN_1 = 1, // Analog input 1
12+
AIN_2 = 2, // Analog input 2
13+
AIN_3 = 3, // Analog input 3
14+
AOUT_0 = 4, // Analog output 0
15+
AOUT_1 = 5, // Analog output 1
16+
AOUT_2 = 6, // Analog output 2
17+
AOUT_3 = 7 // Analog output 3
18+
};
19+
20+
} // namespace scpi_rp
21+
22+
#endif

src/dio/dio.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ bool scpi_rp::getDIODir(BaseIO *io, EDIOPin pin, EDIODir *dir) {
170170
io->writeCommandSeparator();
171171
return readDir();
172172
}
173+
return false;
173174
}
174175

175176
bool scpi_rp::setDIOState(BaseIO *io, EDIOPin pin, bool state) {
@@ -309,4 +310,5 @@ bool scpi_rp::getDIOState(BaseIO *io, EDIOPin pin, bool *state) {
309310
io->writeStr(SCPI_COMMAND_SEPARATOR);
310311
return readDir();
311312
}
313+
return false;
312314
}

src/scpi/scpi_rp_aio.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*!
2+
* @file scpi_rp_aio.cpp
3+
*
4+
* This is a library for interacting with Red Pitaya boards via SCPI server
5+
*
6+
*
7+
* Written by Red Pitaya Industries.
8+
*
9+
* MIT license, all text above must be included in any redistribution
10+
*/
11+
12+
#include "scpi/scpi_rp_aio.h"
13+
14+
#include "aio/aio.h"
15+
#include "common/base_io.h"
16+
17+
using namespace scpi_rp;
18+
19+
void SCPIAio::setInterface(BaseIO *io) { m_io = io; }
20+
21+
bool SCPIAio::rst() {
22+
if (m_io == nullptr) return false;
23+
return setAIORst(m_io);
24+
}
25+
26+
bool SCPIAio::state(EAIOPin pin, float value) {
27+
if (m_io == nullptr) return false;
28+
return setAIOValue(m_io, pin, value);
29+
}
30+
31+
bool SCPIAio::stateQ(EAIOPin pin, float *value) {
32+
if (m_io == nullptr) return false;
33+
return getAIOValue(m_io, pin, value);
34+
}

src/scpi/scpi_rp_aio.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*!
2+
* @file scpi_rp_aio.h
3+
*
4+
* This is a library for interacting with Red Pitaya boards via SCPI server
5+
*
6+
*
7+
* Written by Red Pitaya Industries.
8+
*
9+
* MIT license, all text above must be included in any redistribution
10+
*/
11+
12+
#ifndef SCPI_RP_AIO_H
13+
#define SCPI_RP_AIO_H
14+
15+
#include <stdint.h>
16+
17+
#include "aio/aio_enums.h"
18+
#include "common/base_io.h"
19+
20+
namespace scpi_rp {
21+
/*!
22+
* Class that stores state and functions for SCPI server
23+
*/
24+
class SCPIAio {
25+
public:
26+
SCPIAio(){};
27+
~SCPIAio(){};
28+
29+
/*!
30+
* Sets analog outputs to default values (0 V).
31+
* @return Returns true if the command was called successfully, returns false
32+
* for any other problems.
33+
*/
34+
bool rst();
35+
36+
/*!
37+
* Set the analog voltage on the slow analog outputs. The voltage range of
38+
* slow analog outputs is: 0 - 1.8V
39+
* @param pin Pin number from the list.
40+
* @param value Voltage from 0-1.8V.
41+
* @return Returns true if the command was called successfully, returns false
42+
* for any other problems.
43+
*/
44+
bool state(EAIOPin pin, float value);
45+
46+
/*!
47+
* Get state of digital inputs and outputs.
48+
* @param pin Pin number from the list.
49+
* @param value Return voltage from 0-3.3V.
50+
* @return Returns true if the command was called successfully, returns false
51+
* for any other problems.
52+
*/
53+
bool stateQ(EAIOPin pin, float *value);
54+
55+
friend class SCPIRedPitaya;
56+
57+
private:
58+
void setInterface(BaseIO *io);
59+
BaseIO *m_io = nullptr;
60+
};
61+
62+
} // namespace scpi_rp
63+
64+
#endif

src/system/system.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ bool scpi_rp::getSYSTime(BaseIO *io, uint8_t *hour, uint8_t *min,
6767
}
6868
auto value = io->read();
6969
if (value.isValid) {
70-
uint32_t x, y, z;
71-
int ret = sscanf(value.value, "%u:%u:%u", &x, &y, &z);
70+
int x, y, z;
71+
int ret = sscanf(value.value, "%d:%d:%d", &x, &y, &z);
7272
io->flushCommand(value.next_value);
7373
*hour = x;
7474
*min = y;
@@ -115,8 +115,8 @@ bool scpi_rp::getSYSDate(BaseIO *io, uint16_t *year, uint8_t *month,
115115
}
116116
auto value = io->read();
117117
if (value.isValid) {
118-
uint32_t x, y, z;
119-
int ret = sscanf(value.value, "%u-%u-%u", &x, &y, &z);
118+
int x, y, z;
119+
int ret = sscanf(value.value, "%d-%d-%d", &x, &y, &z);
120120
io->flushCommand(value.next_value);
121121
*year = x;
122122
*month = y;

0 commit comments

Comments
 (0)