Skip to content

Commit ef95aec

Browse files
Gerhard-Viennakrzmaz
authored andcommitted
split ssi part into header and cpp file
1 parent 0c4d38d commit ef95aec

File tree

4 files changed

+120
-110
lines changed

4 files changed

+120
-110
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ file(RENAME fsdata.c my_fsdata.c)
2020

2121
add_executable(${PROGRAM_NAME}
2222
main.cpp
23+
ssi.cpp
2324
cgi.cpp
2425
)
2526
target_compile_definitions(${PROGRAM_NAME} PRIVATE

src/cgi.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ cgi_handler_basic(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
2323
{
2424
int i=0;
2525

26-
/* We use this handler only for one page request ("/leds.cgi")
26+
/* We use this handler for one page request only: "/leds.cgi"
2727
* and it is at position 0 in the tCGI array (see above).
2828
* So iIndex should be 0.
2929
*/
30-
LWIP_ASSERT("cgi_handler_basic called with wrong index", iIndex == 0);
30+
printf("cgi_handler_basic called with index %d\n", iIndex);
3131

3232
/* All leds off */
3333
Led_Off(LED1);
@@ -70,11 +70,11 @@ cgi_handler_extended(int iIndex, int iNumParams, char *pcParam[], char *pcValue[
7070
{
7171
int i=0;
7272

73-
/* We use this handler only for one request ("/leds_ext.cgi")
73+
/* We use this handler for one page request only: "/leds_ext.cgi"
7474
* and it is at position 1 in the tCGI array (see above).
7575
* So iIndex should be 1.
7676
*/
77-
LWIP_ASSERT("cgi_handler_basic called with wrong index", iIndex == 1);
77+
printf("cgi_handler_extended called with index %d\n", iIndex);
7878

7979
/* All leds off */
8080
Led_Off(LED1);

src/ssi.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "hardware/adc.h"
2+
#include "lwip/apps/httpd.h"
3+
#include "pico/cyw43_arch.h"
4+
#include "lwipopts.h"
5+
#include "ssi.h"
6+
#include "cgi.h"
7+
8+
9+
// max length of the tags defaults to be 8 chars
10+
// LWIP_HTTPD_MAX_TAG_NAME_LEN
11+
const char * __not_in_flash("httpd") ssi_example_tags[] = {
12+
"Hello", // 0
13+
"counter", // 1
14+
"GPIO", // 2
15+
"state1", // 3
16+
"state2", // 4
17+
"state3", // 5
18+
"state4", // 6
19+
"bg1", // 7
20+
"bg2", // 8
21+
"bg3", // 9
22+
"bg4" // 10
23+
};
24+
25+
u16_t __time_critical_func(ssi_handler)(int iIndex, char *pcInsert, int iInsertLen)
26+
{
27+
size_t printed;
28+
switch (iIndex) {
29+
case 0: /* "Hello" */
30+
printed = snprintf(pcInsert, iInsertLen, "Hello user number %d!", rand());
31+
break;
32+
case 1: /* "counter" */
33+
{
34+
static int counter;
35+
counter++;
36+
printed = snprintf(pcInsert, iInsertLen, "%d", counter);
37+
}
38+
break;
39+
case 2: /* "GPIO" */
40+
{
41+
const float voltage = adc_read() * 3.3f / (1 << 12);
42+
printed = snprintf(pcInsert, iInsertLen, "%f", voltage);
43+
}
44+
break;
45+
case 3: /* "state1" */
46+
case 4: /* "state2" */
47+
case 5: /* "state3" */
48+
case 6: /* "state4" */
49+
{
50+
bool state;
51+
if(iIndex == 3)
52+
state = gpio_get(LED1);
53+
else if(iIndex == 4)
54+
state = gpio_get(LED2);
55+
else if(iIndex == 5)
56+
state = gpio_get(LED3);
57+
else if(iIndex == 6)
58+
state = gpio_get(LED4);
59+
60+
if(state)
61+
printed = snprintf(pcInsert, iInsertLen, "checked");
62+
else
63+
printed = snprintf(pcInsert, iInsertLen, " ");
64+
}
65+
break;
66+
67+
case 7: /* "bg1" */
68+
case 8: /* "bg2" */
69+
case 9: /* "bg3" */
70+
case 10: /* "bg4" */
71+
{
72+
bool state;
73+
if(iIndex == 7)
74+
state = gpio_get(LED1);
75+
else if(iIndex == 8)
76+
state = gpio_get(LED2);
77+
else if(iIndex == 9)
78+
state = gpio_get(LED3);
79+
else if(iIndex == 10)
80+
state = gpio_get(LED4);
81+
82+
if(state)
83+
printed = snprintf(pcInsert, iInsertLen, "\"background-color:green;\"");
84+
else
85+
printed = snprintf(pcInsert, iInsertLen, "\"background-color:red;\"");
86+
}
87+
break;
88+
default: /* unknown tag */
89+
printed = 0;
90+
break;
91+
}
92+
LWIP_ASSERT("sane length", printed <= 0xFFFF);
93+
return (u16_t)printed;
94+
}
95+
96+
void ssi_init()
97+
{
98+
adc_init();
99+
adc_gpio_init(26);
100+
adc_select_input(0);
101+
size_t i;
102+
for (i = 0; i < LWIP_ARRAYSIZE(ssi_example_tags); i++) {
103+
LWIP_ASSERT("tag too long for LWIP_HTTPD_MAX_TAG_NAME_LEN",
104+
strlen(ssi_example_tags[i]) <= LWIP_HTTPD_MAX_TAG_NAME_LEN);
105+
}
106+
107+
http_set_ssi_handler(ssi_handler,
108+
ssi_example_tags, LWIP_ARRAYSIZE(ssi_example_tags)
109+
);
110+
}

src/ssi.h

Lines changed: 5 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,7 @@
1-
#pragma once
1+
#ifndef __SSI_H__
2+
#define __SSI_H__
23

3-
#include "hardware/adc.h"
4-
#include "lwip/apps/httpd.h"
5-
#include "pico/cyw43_arch.h"
4+
u16_t __time_critical_func(ssi_handler)(int iIndex, char *pcInsert, int iInsertLen);
5+
void ssi_init();
66

7-
#include "lwipopts.h"
8-
9-
// max length of the tags defaults to be 8 chars
10-
// LWIP_HTTPD_MAX_TAG_NAME_LEN
11-
const char * __not_in_flash("httpd") ssi_example_tags[] = {
12-
"Hello", // 0
13-
"counter", // 1
14-
"GPIO", // 2
15-
"state1", // 3
16-
"state2", // 4
17-
"state3", // 5
18-
"state4", // 6
19-
"bg1", // 7
20-
"bg2", // 8
21-
"bg3", // 9
22-
"bg4" // 10
23-
};
24-
25-
u16_t __time_critical_func(ssi_handler)(int iIndex, char *pcInsert, int iInsertLen) {
26-
size_t printed;
27-
switch (iIndex) {
28-
case 0: /* "Hello" */
29-
printed = snprintf(pcInsert, iInsertLen, "Hello user number %d!", rand());
30-
break;
31-
case 1: /* "counter" */
32-
{
33-
static int counter;
34-
counter++;
35-
printed = snprintf(pcInsert, iInsertLen, "%d", counter);
36-
}
37-
break;
38-
case 2: /* "GPIO" */
39-
{
40-
const float voltage = adc_read() * 3.3f / (1 << 12);
41-
printed = snprintf(pcInsert, iInsertLen, "%f", voltage);
42-
}
43-
break;
44-
case 3: /* "state1" */
45-
case 4: /* "state2" */
46-
case 5: /* "state3" */
47-
case 6: /* "state4" */
48-
{
49-
bool state;
50-
if(iIndex == 3)
51-
state = gpio_get(LED1);
52-
else if(iIndex == 4)
53-
state = gpio_get(LED2);
54-
else if(iIndex == 5)
55-
state = gpio_get(LED3);
56-
else if(iIndex == 6)
57-
state = gpio_get(LED4);
58-
59-
if(state)
60-
printed = snprintf(pcInsert, iInsertLen, "checked");
61-
else
62-
printed = snprintf(pcInsert, iInsertLen, " ");
63-
}
64-
break;
65-
66-
case 7: /* "bg1" */
67-
case 8: /* "bg2" */
68-
case 9: /* "bg3" */
69-
case 10: /* "bg4" */
70-
{
71-
bool state;
72-
if(iIndex == 7)
73-
state = gpio_get(LED1);
74-
else if(iIndex == 8)
75-
state = gpio_get(LED2);
76-
else if(iIndex == 9)
77-
state = gpio_get(LED3);
78-
else if(iIndex == 10)
79-
state = gpio_get(LED4);
80-
81-
if(state)
82-
printed = snprintf(pcInsert, iInsertLen, "\"background-color:green;\"");
83-
else
84-
printed = snprintf(pcInsert, iInsertLen, "\"background-color:red;\"");
85-
}
86-
break;
87-
default: /* unknown tag */
88-
printed = 0;
89-
break;
90-
}
91-
LWIP_ASSERT("sane length", printed <= 0xFFFF);
92-
return (u16_t)printed;
93-
}
94-
95-
void ssi_init() {
96-
adc_init();
97-
adc_gpio_init(26);
98-
adc_select_input(0);
99-
size_t i;
100-
for (i = 0; i < LWIP_ARRAYSIZE(ssi_example_tags); i++) {
101-
LWIP_ASSERT("tag too long for LWIP_HTTPD_MAX_TAG_NAME_LEN",
102-
strlen(ssi_example_tags[i]) <= LWIP_HTTPD_MAX_TAG_NAME_LEN);
103-
}
104-
105-
http_set_ssi_handler(ssi_handler,
106-
ssi_example_tags, LWIP_ARRAYSIZE(ssi_example_tags)
107-
);
108-
}
7+
#endif // __SSI_H__

0 commit comments

Comments
 (0)