Test program with networking part in RTOS-thread and SD in RTOS-thread

Dependencies:   fat mbed mbed-rtos EthernetInterface SDFileSystem

Fork of Network-RTOS by Jaap Vermaas

main.cpp

Committer:
tuxic
Date:
2012-07-22
Revision:
2:7ba5e4b86b43
Parent:
1:5a71a8a0463b

File content as of revision 2:7ba5e4b86b43:

#include "mbed.h"
#include "EthernetInterface.h"
#include "SDFileSystem.h"
#include "rtos.h"
#include "cmsis_os.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);

void led2_thread(void const *argument) {
    while (true) {
        led2 = !led2;
        osDelay(1000);
    }
}
void net_thread(void const *argument) {
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n", eth.getIPAddress());
    TCPSocket sock;
    while (true) {
        led3 = !led3;
        sock.connect("mbed.org", 80);

        char http_cmd[] = "GET /media/uploads/donatien/hello.txt HTTP/1.1\r\nHost: %s\r\n\r\n";
        sock.send(http_cmd, sizeof(http_cmd) - 1, 3000);

        char in_buf[256];
        bool firstIteration = true;
        int ret;
        do {
            ret = sock.receive(in_buf, 255, firstIteration?3000:0);
            in_buf[ret] = '\0';

            printf("Received %d chars from server: %s\n", ret, in_buf);
            firstIteration = false;
        } while ( ret > 0 );

        sock.close();

        //eth.disconnect();

        led3 = !led3;
        osDelay(500);
    }
}
void sd_thread(void const *argument) {
    SDFileSystem sd(p5, p6, p7, p8, "sd");
    FILE *f = fopen("/sd/out.txt", "w");
    for (int i=0; i<30; i++) {
        fprintf(f, "%d\n", i);
        printf("%d\n\r", i);
    }
    printf("closing\n");
    fclose(f);
    
    while (true) {
        led2 = !led2;
        Thread::wait(1000);
    }
}
osThreadDef(led2_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
osThreadDef(net_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
osThreadDef(sd_thread, osPriorityNormal, (int)(DEFAULT_STACK_SIZE * 2.25));

int main() {
    osThreadCreate(osThread(led2_thread), NULL);
    osThreadCreate(osThread(net_thread), NULL);
    osThreadCreate(osThread(sd_thread), NULL);
    
    while (true) {
        led1 = !led1;
        osDelay(500);
    }
}