My fork of the blinky project to support ethernet and twitter

Dependencies:   EthernetInterface HTTPClient HTU21D OAuth4Tw SDFileSystem SPI_TFT_ILI9341 SerialShell mbed-rtos mbed-src

Fork of mbed_blinky by Mbed

main.cpp

Committer:
vpcola
Date:
2015-04-29
Revision:
7:aafb9225f866
Parent:
4:81cea7a352b0
Child:
8:e3fb74a4772c

File content as of revision 7:aafb9225f866:

#include "mbed.h"
#include "rtos.h"
#include "EthernetInterface.h"
#include "Arial12x12.h"
#include "Arial24x23.h"
#include "SPI_TFT_ILI9341.h"
#include "SDFileSystem.h"
#include "Shell.h"

DigitalOut myled(LED1);
//EthernetInterface eth;
Serial pc(p9,p10);

SDFileSystem sd(p5,p6,p7,p8,"sd"); // mosi, miso, sck, cs 
SPI_TFT_ILI9341 TFT(p11,p12,p13,p15, p16, p17 ); // mosi, miso, sck, cs, eset, dc 
DigitalOut lcdOn(p14);

#define SHELL_STACK_SIZ 1024
// Pre-allocate the shell's stack (on global mem)
unsigned char shellStack[SHELL_STACK_SIZ];
Shell shell(&pc);

// Local Commands
/**
 *  \brief Gets the amount of free memory
 *  \param none
 *  \return none
 **/
static void cmd_mem(Stream * chp, int argc, char * argv[])
{
   // In order to get free mem within RTOS
   // we need to get the main thread's stack pointer
   // and subtract it with the top of the heap
   // ------+-------------------+   Last Address of RAM (INITIAL_SP)
   //       | Scheduler Stack   |
   //       +-------------------+
   //       | Main Thread Stack |
   //       |         |         |
   //       |         v         |
   //       +-------------------+ <- bottom_of_stack/__get_MSP()
   // RAM   |                   | 
   //       |  Available RAM    |  
   //       |                   |  
   //       +-------------------+ <- top_of_heap
   //       |         ^         |
   //       |         |         |
   //       |       Heap        |
   //       +-------------------+ <- __end__ / HEAP_START (linker defined var)
   //       | ZI                |
   //       +-------------------+
   //       | ZI: Shell Stack   |
   //       +-------------------+
   //       | ZI: Idle Stack    |
   //       +-------------------+
   //       | ZI: Timer Stack   |
   //       +-------------------+
   //       | RW                |  
   // ------+===================+  First Address of RAM
   //       |                   |
   // Flash |                   |
   //

   uint32_t bottom_of_stack = __get_MSP();
   char     * top_of_heap =  (char *) malloc(sizeof(char));
   uint32_t diff = bottom_of_stack - (uint32_t) top_of_heap;

   free((void *) top_of_heap);
   chp->printf("Available Memory : %d bytes\r\n",
        diff);
}

/**
 *  \brief List Directories and files 
 *  \param none
 *  \return int
 **/
static void cmd_ls(Stream * chp, int argc, char * argv[])
{
   DIR * dp;
   struct dirent * dirp;
   char dirroot[256];
   
   if (argc >= 1)
       sprintf(dirroot, "/sd/%s", argv[0]);
   else
       sprintf(dirroot, "/sd");
   
   chp->printf("Listing directory [%s]\r\n", dirroot);
   
   dp = opendir(dirroot);           
   while((dirp = readdir(dp)) != NULL)
   {
       chp->printf("\t%s\r\n", dirp->d_name);
   }
   closedir(dp);
}

static void cmd_load(Stream * chp, int argc, char * argv[])
{
   char filename[256];
   
   if (argc != 1)
   {
       chp->printf("load <bitmapfile>\r\n");
       return;
   }
   
   sprintf(filename, "/sd/%s", argv[0]);
       // Load a bitmap startup file
   int err = TFT.BMP_16(0,0, filename);
   if (err != 1) TFT.printf(" - Err: %d", err); 
}


/**
 * \brief Initialize LCD
 * \param none
 * \return void
 **/
void init_LCD()
{
    pc.printf("Initializing LCD Screen ...\r\n");

    // Turn on the LCD
    lcdOn = 1;

    TFT.claim(stdout);  
    TFT.set_orientation(1);
    TFT.background(Black);    // set background to black
    TFT.foreground(White);    // set chars to white
    TFT.cls();                // clear the screen


    TFT.set_font((unsigned char*) Arial12x12);
    TFT.locate(0,0);

    printf("Hello World of EMBED!\n");

}


void led1_thread(void const *args) {
    while (true) {
        myled = !myled;
        Thread::wait(1000);
    }
}

int main() {
    Thread * thread;
    pc.baud(115200);

    pc.printf("\r\nStarting Mbed ...\r\n");
 //   pc.printf("Inititalizing ethernet ....\r\n");
 //   eth.init(); // Use DHCP
 //   eth.connect();
 //   pc.printf("IP Address is %s\n", eth.getIPAddress());

    // Initialize the LCD
    init_LCD();
    
    // After initializing the ethernet interface
    // run it in its own thread
    thread = new Thread(led1_thread);

    // Start the shell
    pc.printf("Starting debug shell ...\r\n");
    shell.addCommand("ls", cmd_ls);
    //shell.addCommand("load", cmd_load);
    shell.addCommand("mem", cmd_mem);
    shell.start(osPriorityNormal, SHELL_STACK_SIZ, shellStack);
    
    // Do something logical here
    // other than looping
    while(1) {
        wait(0.2);
    }
    
    thread->terminate();
    delete thread;
}