Skip to content

Commit

Permalink
support for the full program memory
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Oct 19, 2015
1 parent dfebea1 commit 533299d
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 78 deletions.
104 changes: 56 additions & 48 deletions examples/test-arduino/build-arduino-library.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,72 @@
import sys
import shutil

#
# make scpi-parser as arduino compatible library
#
scpi_parser_dir = os.path.join(os.path.dirname(__file__), 'scpi-parser')
libscpi_dir = os.path.join(os.path.dirname(__file__), '../../libscpi')

def rm_then_cp(src, dest):
if os.path.exists(dest):
shutil.rmtree(dest)
shutil.copytree(src, dest)

# copy *.h files
rm_then_cp(os.path.join(libscpi_dir, 'inc/scpi'), os.path.join(scpi_parser_dir, 'src/scpi'))
def build_scpi_parser_lib(libscpi_dir, scpi_parser_dir):
'''
Build scpi-parser as arduino compatible library
'''

# copy *.h files
rm_then_cp(os.path.join(libscpi_dir, 'inc/scpi'), os.path.join(scpi_parser_dir, 'src/scpi'))

# modify config.h
config_h_file_path = os.path.join(scpi_parser_dir, 'src/scpi/config.h')
config_h_file = open(config_h_file_path)
tmp_file_path = config_h_file_path + ".tmp"
tmp_file = open(tmp_file_path, "w")
for line in config_h_file:
if line == '#ifdef SCPI_USER_CONFIG\n':
tmp_file.write('// This is automatically added by the build-arduino-library.py\n')
tmp_file.write('#define SCPI_USER_CONFIG 1\n')
tmp_file.write('\n')
tmp_file.write(line)
config_h_file.close()
tmp_file.close()
os.unlink(config_h_file_path)
os.rename(tmp_file_path, config_h_file_path)

# modify config.h
config_h_file_path = os.path.join(scpi_parser_dir, 'src/scpi/config.h')
config_h_file = open(config_h_file_path)
tmp_file_path = config_h_file_path + ".tmp"
tmp_file = open(tmp_file_path, "w")
for line in config_h_file:
if line == '#ifdef SCPI_USER_CONFIG\n':
tmp_file.write('// This is automatically added by the build-arduino-library.py\n')
tmp_file.write('#define SCPI_USER_CONFIG 1\n')
tmp_file.write('\n')
tmp_file.write(line)
config_h_file.close()
tmp_file.close()
os.unlink(config_h_file_path)
os.rename(tmp_file_path, config_h_file_path)
# copy scpi_user_config.h
shutil.copyfile(os.path.join(os.path.dirname(__file__), 'scpi_user_config.h'), os.path.join(scpi_parser_dir, 'src/scpi/scpi_user_config.h'))

# copy scpi_user_config.h
shutil.copyfile(os.path.join(os.path.dirname(__file__), 'scpi_user_config.h'), os.path.join(scpi_parser_dir, 'src/scpi/scpi_user_config.h'))
# copy *.c files
rm_then_cp(os.path.join(libscpi_dir, 'src'), os.path.join(scpi_parser_dir, 'src/impl'))

# copy *.c files
rm_then_cp(os.path.join(libscpi_dir, 'src'), os.path.join(scpi_parser_dir, 'src/impl'))
def copy_lib(src_lib_dir, dst_name):
#
# find arduino libraries directory
#
ARDUINO_LIB_DIR_CANDIDATES = {
'Linux': ['Arduino/libraries/', 'Documents/Arduino/libraries/'],
'Darwin': ['Documents/Arduino/libraries/'],
'Windows': ['Documents\\Arduino\\libraries\\', 'My Documents\\Arduino\\libraries\\']
}

#
# find arduino libraries directory
#
ARDUINO_LIB_DIR_CANDIDATES = {
'Linux': ['Arduino/libraries/', 'Documents/Arduino/libraries/'],
'Darwin': ['Documents/Arduino/libraries/'],
'Windows': ['Documents\\Arduino\\libraries\\', 'My Documents\\Arduino\\libraries\\']
}
home_dir = os.path.expanduser('~')

home_dir = os.path.expanduser('~')
arduino_libs_dir = None

arduino_libs_dir = None
candidates = ARDUINO_LIB_DIR_CANDIDATES.get(platform.system())
if candidates:
for candidate_dir in ARDUINO_LIB_DIR_CANDIDATES.get(platform.system()):
arduino_libs_dir = os.path.join(home_dir, candidate_dir)
if os.path.exists(arduino_libs_dir):
break

candidates = ARDUINO_LIB_DIR_CANDIDATES.get(platform.system())
if candidates:
for candidate_dir in ARDUINO_LIB_DIR_CANDIDATES.get(platform.system()):
arduino_libs_dir = os.path.join(home_dir, candidate_dir)
if os.path.exists(arduino_libs_dir):
break
if arduino_libs_dir:
# copy arduino scpi-parser library to the arduino libraries folder
rm_then_cp(src_lib_dir, os.path.join(arduino_libs_dir, dst_name))
return True
else:
print("Arduino libraries directory not found!")
return False

if arduino_libs_dir:
# copy arduino scpi-parser library to the arduino libraries folder
rm_then_cp(scpi_parser_dir, os.path.join(arduino_libs_dir, 'scpi-parser'))
else:
print("Arduino libraries directory not found!")
if __name__ == "__main__":
libscpi_dir = os.path.join(os.path.dirname(__file__), '../../libscpi')
scpi_parser_dir = os.path.join(os.path.dirname(__file__), 'scpi-parser')
build_scpi_parser_lib(libscpi_dir, scpi_parser_dir)
copy_lib(scpi_parser_dir, 'scpi-parser')
54 changes: 37 additions & 17 deletions examples/test-arduino/scpi-def.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "scpi/scpi.h"
#include "scpi-def.h"

#if USE_64K_PROGMEM_FOR_CMD_LIST
Expand All @@ -47,7 +46,7 @@
scpi_result_t DMM_MeasureVoltageDcQ(scpi_t * context) {
scpi_number_t param1, param2;
char bf[15];
fprintf(stderr, "meas:volt:dc\r\n"); // debug command name
SerialPrintf("meas:volt:dc"); // debug command name

// read first parameter if present
if (!SCPI_ParamNumber(context, scpi_special_numbers_def, &param1, false)) {
Expand All @@ -61,11 +60,11 @@ scpi_result_t DMM_MeasureVoltageDcQ(scpi_t * context) {


SCPI_NumberToStr(context, scpi_special_numbers_def, &param1, bf, 15);
fprintf(stderr, "\tP1=%s\r\n", bf);
SerialPrintf("\tP1=%s", bf);


SCPI_NumberToStr(context, scpi_special_numbers_def, &param2, bf, 15);
fprintf(stderr, "\tP2=%s\r\n", bf);
SerialPrintf("\tP2=%s", bf);

SCPI_ResultDouble(context, 0);

Expand All @@ -75,7 +74,7 @@ scpi_result_t DMM_MeasureVoltageDcQ(scpi_t * context) {
scpi_result_t DMM_MeasureVoltageAcQ(scpi_t * context) {
scpi_number_t param1, param2;
char bf[15];
fprintf(stderr, "meas:volt:ac\r\n"); // debug command name
SerialPrintf("meas:volt:ac"); // debug command name

// read first parameter if present
if (!SCPI_ParamNumber(context, scpi_special_numbers_def, &param1, false)) {
Expand All @@ -89,11 +88,11 @@ scpi_result_t DMM_MeasureVoltageAcQ(scpi_t * context) {


SCPI_NumberToStr(context, scpi_special_numbers_def, &param1, bf, 15);
fprintf(stderr, "\tP1=%s\r\n", bf);
SerialPrintf("\tP1=%s", bf);


SCPI_NumberToStr(context, scpi_special_numbers_def, &param2, bf, 15);
fprintf(stderr, "\tP2=%s\r\n", bf);
SerialPrintf("\tP2=%s", bf);

SCPI_ResultDouble(context, 0);

Expand All @@ -102,7 +101,7 @@ scpi_result_t DMM_MeasureVoltageAcQ(scpi_t * context) {

scpi_result_t DMM_ConfigureVoltageDc(scpi_t * context) {
double param1, param2;
fprintf(stderr, "conf:volt:dc\r\n"); // debug command name
SerialPrintf("conf:volt:dc"); // debug command name

// read first parameter if present
if (!SCPI_ParamDouble(context, &param1, true)) {
Expand All @@ -114,22 +113,22 @@ scpi_result_t DMM_ConfigureVoltageDc(scpi_t * context) {
// do something, if parameter not present
}

fprintf(stderr, "\tP1=%lf\r\n", param1);
fprintf(stderr, "\tP2=%lf\r\n", param2);
SerialPrintf("\tP1=%lf", param1);
SerialPrintf("\tP2=%lf", param2);

return SCPI_RES_OK;
}

scpi_result_t TEST_Bool(scpi_t * context) {
scpi_bool_t param1;
fprintf(stderr, "TEST:BOOL\r\n"); // debug command name
SerialPrintf("TEST:BOOL"); // debug command name

// read first parameter if present
if (!SCPI_ParamBool(context, &param1, true)) {
return SCPI_RES_ERR;
}

fprintf(stderr, "\tP1=%d\r\n", param1);
SerialPrintf("\tP1=%d", param1);

return SCPI_RES_OK;
}
Expand All @@ -151,7 +150,7 @@ scpi_result_t TEST_ChoiceQ(scpi_t * context) {
}

SCPI_ChoiceToName(trigger_source, param, &name);
fprintf(stderr, "\tP1=%s (%ld)\r\n", name, (long int) param);
SerialPrintf("\tP1=%s (%ld)", name, (long int) param);

SCPI_ResultInt32(context, param);

Expand All @@ -160,7 +159,7 @@ scpi_result_t TEST_ChoiceQ(scpi_t * context) {

scpi_result_t TEST_Numbers(scpi_t * context) {

fprintf(stderr, "RAW CMD %.*s\r\n", (int) context->param_list.cmd_raw.length, context->param_list.cmd_raw.data);
SerialPrintf("RAW CMD %.*s", (int) context->param_list.cmd_raw.length, context->param_list.cmd_raw.data);

return SCPI_RES_OK;
}
Expand All @@ -173,7 +172,7 @@ scpi_result_t TEST_Text(scpi_t * context) {
buffer[0] = '\0';
}

fprintf(stderr, "TEXT: ***%s***\r\n", buffer);
SerialPrintf("TEXT: ***%s***", buffer);

return SCPI_RES_OK;
}
Expand Down Expand Up @@ -287,6 +286,16 @@ static const scpi_command_t scpi_commands[] PROGMEM = {
SCPI_CMD_LIST_END
};

#elif USE_FULL_PROGMEM_FOR_CMD_LIST

#define SCPI_COMMAND(P, C) P
static const char scpi_command_patterns[] PROGMEM = SCPI_COMMANDS;
#define SCPI_COMMAND(P, C) {(const char *)(sizeof(P) - 1), C},
static const scpi_command_t scpi_commands[] PROGMEM = {
SCPI_COMMANDS
SCPI_CMD_LIST_END
};

#else

#define SCPI_COMMAND(P, C) {P, C},
Expand All @@ -297,7 +306,6 @@ static const scpi_command_t scpi_commands[] = {

#endif


static scpi_interface_t scpi_interface = {
/* error */ SCPI_Error,
/* write */ SCPI_Write,
Expand All @@ -312,8 +320,13 @@ static char scpi_input_buffer[SCPI_INPUT_BUFFER_LENGTH];
static scpi_reg_val_t scpi_regs[SCPI_REG_COUNT];


scpi_t scpi_context = {
static scpi_t scpi_context = {
#if USE_FULL_PROGMEM_FOR_CMD_LIST
/* cmdlist */ 0,
/* cmdpatterns */ 0,
#else
/* cmdlist */ scpi_commands,
#endif
/* buffer */
{ /* length */ SCPI_INPUT_BUFFER_LENGTH, /* position */ 0, /* data */ scpi_input_buffer},
/* param_list */
Expand All @@ -337,3 +350,10 @@ scpi_t scpi_context = {
{"MANUFACTURE", "INSTR2013", NULL, "01-02"},
};

scpi_t *SCPI_GetContext() {
#if USE_FULL_PROGMEM_FOR_CMD_LIST
scpi_context.cmdlist = pgm_get_far_address(scpi_commands);
scpi_context.cmdpatterns = pgm_get_far_address(scpi_command_patterns);
#endif
return &scpi_context;
}
4 changes: 3 additions & 1 deletion examples/test-arduino/scpi-def.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

#include "scpi/scpi.h"

extern scpi_t scpi_context;
void SerialPrintf(const char *format, ...);

scpi_t *SCPI_GetContext();

size_t SCPI_Write(scpi_t * context, const char * data, size_t len);
int SCPI_Error(scpi_t * context, int_fast16_t err);
Expand Down
1 change: 1 addition & 0 deletions examples/test-arduino/scpi_user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern "C" {

#define USE_COMMAND_TAGS 0
#define USE_64K_PROGMEM_FOR_CMD_LIST 0
#define USE_FULL_PROGMEM_FOR_CMD_LIST 1
#define SCPI_MAX_CMD_PATTERN_SIZE 128

// strtoull is not defined on some arduino boards
Expand Down
6 changes: 4 additions & 2 deletions examples/test-arduino/test-arduino.ino
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ void setup() {
while (!Serial);
Serial.println("serial com ready");

SCPI_Init(&scpi_context);
scpi_t *context = SCPI_GetContext();

SCPI_Init(context);

#define TEST_SCPI_INPUT(cmd) result = SCPI_Input(&scpi_context, cmd, strlen(cmd))
#define TEST_SCPI_INPUT(cmd) result = SCPI_Input(context, cmd, strlen(cmd))

TEST_SCPI_INPUT("*CLS\r\n");
TEST_SCPI_INPUT("*RST\r\n");
Expand Down
4 changes: 4 additions & 0 deletions libscpi/inc/scpi/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ extern "C" {
#define USE_64K_PROGMEM_FOR_CMD_LIST 0
#endif

#ifndef USE_FULL_PROGMEM_FOR_CMD_LIST
#define USE_FULL_PROGMEM_FOR_CMD_LIST 0
#endif

#ifndef USE_DEPRECATED_FUNCTIONS
#define USE_DEPRECATED_FUNCTIONS 1
#endif
Expand Down
18 changes: 11 additions & 7 deletions libscpi/inc/scpi/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
#include <stdint.h>
#include "scpi/config.h"

#if USE_FULL_PROGMEM_FOR_CMD_LIST
#include <avr/pgmspace.h>
#endif

#if HAVE_STDBOOL
#include <stdbool.h>
#endif
Expand Down Expand Up @@ -113,11 +117,7 @@ extern "C" {

typedef struct _scpi_command_t scpi_command_t;

#if USE_COMMAND_TAGS
#define SCPI_CMD_LIST_END {NULL, NULL, 0}
#else
#define SCPI_CMD_LIST_END {NULL, NULL}
#endif
#define SCPI_CMD_LIST_END {}

/* scpi interface */
typedef struct _scpi_t scpi_t;
Expand Down Expand Up @@ -262,7 +262,7 @@ extern "C" {
const scpi_command_t * cmd;
lex_state_t lex_state;
scpi_const_buffer_t cmd_raw;
#if USE_64K_PROGMEM_FOR_CMD_LIST
#if USE_64K_PROGMEM_FOR_CMD_LIST || USE_FULL_PROGMEM_FOR_CMD_LIST
scpi_command_t cmd_s;
char cmd_pattern_s[SCPI_MAX_CMD_PATTERN_SIZE + 1];
#endif
Expand Down Expand Up @@ -297,9 +297,13 @@ extern "C" {
scpi_command_callback_t reset;
};


struct _scpi_t {
#if USE_FULL_PROGMEM_FOR_CMD_LIST
uint_farptr_t cmdlist;
uint_farptr_t cmdpatterns;
#else
const scpi_command_t * cmdlist;
#endif
scpi_buffer_t buffer;
scpi_param_list_t param_list;
scpi_interface_t * interface;
Expand Down
Loading

0 comments on commit 533299d

Please sign in to comment.