Skip to main content
added 27 characters in body
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50

.ino is compiled as C++. your test is C code. put the C code in a .c file and the declaration in .h file and include the .h in .ino

I created a test.c, copied the content of your main.c into it and renamed the main function to test().

TheThen I created a test.h with

#ifndef test_h
#define test_h

extern "C" void test(char * buff);

#endif

and in ino setup() I call test();

it compiles. It runs. Output:

2 sections
type=section
section [0]: start_pin=  0, end_pin= 71, red=  0, green=255, blue=  0
section [1]: start_pin=216, end_pin=287, red=255, green= 53, blue=  0

in ino

void setup()
{
  Serial.begin(115200);
  char buff[700];
  test(buff);
  Serial.print(buff);
}

in .c

void test(char* buff)
  {

    static const char *s =
        "{ \"type\": \"section\", \"sections\": [{ \"start_pin\": 0, \"end_pin\": 71, \"colour_red\": 0, \"colour_green\": 255, \"colour_blue\": 0 }, { \"start_pin\": 216, \"end_pin\": 287, \"colour_red\": 255, \"colour_green\": 0, \"colour_blue\": 0 }] }";

    int status = json_read_object(s, json_attrs, NULL); 

  if (status != 0) {
    sprintf(buff, json_error_string(status));
    return;
  }

  int pos = sprintf(buff, "%d sections\n", num_sections);

  pos += sprintf(buff + pos, "type=%s\n", thenode.type);
  for (int i = 0; i < num_sections; ++i) {
    pos +=
        sprintf(buff + pos,
            "section [%u][%d]: start_pin=%3ustart_pin=%3d, end_pin=%3uend_pin=%3d, red=%3ured=%3d, green=%3ugreen=%3d, blue=%3u\n"blue=%3d\n",
            i, thenode.thelight[i].start_pin,
        thenode.thelight[i].end_pin,
            thenode.thelight[i].colour_red,
        thenode.thelight[i].colour_green,
            thenode.thelight[i].colour_blue);
  }
}

}

.ino is compiled as C++. your test is C code. put the C code in a .c file and the declaration in .h file and include the .h in .ino

I created a test.c, copied the content of your main.c into it and renamed the main function to test().

The I created a test.h with

#ifndef test_h
#define test_h

extern "C" void test(char * buff);

#endif

and in ino setup() I call test();

it compiles. It runs. Output:

2 sections
type=section
section [0]: start_pin=  0, end_pin= 71, red=  0, green=255, blue=  0
section [1]: start_pin=216, end_pin=287, red=255, green= 53, blue=  0

in ino

void setup()
{
  Serial.begin(115200);
  char buff[700];
  test(buff);
  Serial.print(buff);
}

in .c

void test(char* buff)
 {

    static const char *s =
        "{ \"type\": \"section\", \"sections\": [{ \"start_pin\": 0, \"end_pin\": 71, \"colour_red\": 0, \"colour_green\": 255, \"colour_blue\": 0 }, { \"start_pin\": 216, \"end_pin\": 287, \"colour_red\": 255, \"colour_green\": 0, \"colour_blue\": 0 }] }";

    int status = json_read_object(s, json_attrs, NULL);
if (status != 0) {
    sprintf(buff, json_error_string(status));
    return;
}

int pos = sprintf(buff, "%d sections\n", num_sections);

pos += sprintf(buff + pos, "type=%s\n", thenode.type);
for (int i = 0; i < num_sections; ++i) {
    pos += sprintf(buff + pos, "section [%u]: start_pin=%3u, end_pin=%3u, red=%3u, green=%3u, blue=%3u\n",
        i, thenode.thelight[i].start_pin,
        thenode.thelight[i].end_pin,
        thenode.thelight[i].colour_red,
        thenode.thelight[i].colour_green,
        thenode.thelight[i].colour_blue);
}

}

.ino is compiled as C++. your test is C code. put the C code in a .c file and the declaration in .h file and include the .h in .ino

I created a test.c, copied the content of your main.c into it and renamed the main function to test().

Then I created a test.h with

#ifndef test_h
#define test_h

extern "C" void test(char * buff);

#endif

and in ino setup() I call test();

it compiles. It runs. Output:

2 sections
type=section
section [0]: start_pin=  0, end_pin= 71, red=  0, green=255, blue=  0
section [1]: start_pin=216, end_pin=287, red=255, green= 53, blue=  0

in ino

void setup()
{
  Serial.begin(115200);
  char buff[700];
  test(buff);
  Serial.print(buff);
}

in .c

void test(char* buff) {

  static const char *s =
      "{ \"type\": \"section\", \"sections\": [{ \"start_pin\": 0, \"end_pin\": 71, \"colour_red\": 0, \"colour_green\": 255, \"colour_blue\": 0 }, { \"start_pin\": 216, \"end_pin\": 287, \"colour_red\": 255, \"colour_green\": 0, \"colour_blue\": 0 }] }";

  int status = json_read_object(s, json_attrs, NULL); 

  if (status != 0) {
    sprintf(buff, json_error_string(status));
    return;
  }

  int pos = sprintf(buff, "%d sections\n", num_sections);

  pos += sprintf(buff + pos, "type=%s\n", thenode.type);
  for (int i = 0; i < num_sections; ++i) {
    pos +=
        sprintf(buff + pos,
            "section [%d]: start_pin=%3d, end_pin=%3d, red=%3d, green=%3d, blue=%3d\n",
            i, thenode.thelight[i].start_pin, thenode.thelight[i].end_pin,
            thenode.thelight[i].colour_red, thenode.thelight[i].colour_green,
            thenode.thelight[i].colour_blue);
  }
}
added 27 characters in body
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50

.ino is compiled as C++. your test is C code. put the C code in a .c file and the declaration in .h file and include the .h in .ino

I created a test.c, copied the content of your main.c into it and renamed the main function to test().

The I created a test.h with

#ifndef test_h
#define test_h

extern "C" void test(char * buff);

#endif

and in ino setup() I call test();

it compiles. It runs, but the printed data are gibberish. Output:

2 sections
type=section
section [0]: start_pin=  0, end_pin= 71, red=  0, green=255, blue=  0
section [1]: start_pin=216, end_pin=287, red=255, green= 53, blue=  0

in ino

void setup()
{
  Serial.begin(115200);
  char buff[700];
  test(buff);
  Serial.print(buff);
}

in .c

void test(char* buff)
{

    static const char *s =
        "{ \"type\": \"section\", \"sections\": [{ \"start_pin\": 0, \"end_pin\": 71, \"colour_red\": 0, \"colour_green\": 255, \"colour_blue\": 0 }, { \"start_pin\": 216, \"end_pin\": 287, \"colour_red\": 255, \"colour_green\": 0, \"colour_blue\": 0 }] }";

    int status = json_read_object(s, json_attrs, NULL);
if (status != 0) {
    sprintf(buff, json_error_string(status));
    return;
}

int pos = sprintf(buff, "%d sections\n", num_sections);

pos += sprintf(buff + pos, "type=%s\n", thenode.type);
for (int i = 0; i < num_sections; ++i) {
    pos += sprintf(buff + pos, "section [%u]: start_pin=%3u, end_pin=%3u, red=%3u, green=%3u, blue=%3u\n",
        i, thenode.thelight[i].start_pin,
        thenode.thelight[i].end_pin,
        thenode.thelight[i].colour_red,
        thenode.thelight[i].colour_green,
        thenode.thelight[i].colour_blue);
}

}

.ino is compiled as C++. your test is C code. put the C code in a .c file and the declaration in .h file and include the .h in .ino

I created a test.c, copied the content of your main.c into it and renamed the main function to test().

The I created a test.h with

#ifndef test_h
#define test_h

extern "C" void test();

#endif

and in ino setup() I call test();

it compiles. It runs, but the printed data are gibberish.

.ino is compiled as C++. your test is C code. put the C code in a .c file and the declaration in .h file and include the .h in .ino

I created a test.c, copied the content of your main.c into it and renamed the main function to test().

The I created a test.h with

#ifndef test_h
#define test_h

extern "C" void test(char * buff);

#endif

and in ino setup() I call test();

it compiles. It runs. Output:

2 sections
type=section
section [0]: start_pin=  0, end_pin= 71, red=  0, green=255, blue=  0
section [1]: start_pin=216, end_pin=287, red=255, green= 53, blue=  0

in ino

void setup()
{
  Serial.begin(115200);
  char buff[700];
  test(buff);
  Serial.print(buff);
}

in .c

void test(char* buff)
{

    static const char *s =
        "{ \"type\": \"section\", \"sections\": [{ \"start_pin\": 0, \"end_pin\": 71, \"colour_red\": 0, \"colour_green\": 255, \"colour_blue\": 0 }, { \"start_pin\": 216, \"end_pin\": 287, \"colour_red\": 255, \"colour_green\": 0, \"colour_blue\": 0 }] }";

    int status = json_read_object(s, json_attrs, NULL);
if (status != 0) {
    sprintf(buff, json_error_string(status));
    return;
}

int pos = sprintf(buff, "%d sections\n", num_sections);

pos += sprintf(buff + pos, "type=%s\n", thenode.type);
for (int i = 0; i < num_sections; ++i) {
    pos += sprintf(buff + pos, "section [%u]: start_pin=%3u, end_pin=%3u, red=%3u, green=%3u, blue=%3u\n",
        i, thenode.thelight[i].start_pin,
        thenode.thelight[i].end_pin,
        thenode.thelight[i].colour_red,
        thenode.thelight[i].colour_green,
        thenode.thelight[i].colour_blue);
}

}

added 27 characters in body
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50

.ino is compiled as C++. your test is C code. put the C code in a .c file and the declaration in .h file and include the .h in .ino

I created a test.c, copied the content of your main.c into it and renamed the main function to test().

The I created a test.h with

#ifndef test_h
#define test_h

extern "C" void test();

#endif

and in ino setup() I call test();

it compiles. It runs, but the printed data are gibberish.

.ino is compiled as C++. your test is C code. put the C code in a .c file and the declaration in .h file and include the .h in .ino

I created a test.c, copied the content of your main.c into it and renamed the main function to test().

The I created a test.h with

#ifndef test_h
#define test_h

extern "C" void test();

#endif

and in ino setup() I call test();

it compiles. It runs.

.ino is compiled as C++. your test is C code. put the C code in a .c file and the declaration in .h file and include the .h in .ino

I created a test.c, copied the content of your main.c into it and renamed the main function to test().

The I created a test.h with

#ifndef test_h
#define test_h

extern "C" void test();

#endif

and in ino setup() I call test();

it compiles. It runs, but the printed data are gibberish.

deleted 13 characters in body
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50
Loading
added 313 characters in body
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50
Loading
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50
Loading