A simple example using a display and networking at the same time.

Dependencies:   EALib EthernetInterface mbed-rtos mbed

Committer:
embeddedartists
Date:
Mon Jul 21 09:42:16 2014 +0000
Revision:
0:833824a40ced
Very basic example of using display and networking at the same time.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:833824a40ced 1 /******************************************************************************
embeddedartists 0:833824a40ced 2 * Includes
embeddedartists 0:833824a40ced 3 *****************************************************************************/
embeddedartists 0:833824a40ced 4
embeddedartists 0:833824a40ced 5 #include "mbed.h"
embeddedartists 0:833824a40ced 6
embeddedartists 0:833824a40ced 7 #include "LcdController.h"
embeddedartists 0:833824a40ced 8 #include "EaLcdBoard.h"
embeddedartists 0:833824a40ced 9 #include "sdram.h"
embeddedartists 0:833824a40ced 10
embeddedartists 0:833824a40ced 11 #include "BubbleDemo.h"
embeddedartists 0:833824a40ced 12 #include "EthernetInterface.h"
embeddedartists 0:833824a40ced 13 #include "TCPSocketConnection.h"
embeddedartists 0:833824a40ced 14 #include "cmsis_os.h"
embeddedartists 0:833824a40ced 15
embeddedartists 0:833824a40ced 16
embeddedartists 0:833824a40ced 17 /******************************************************************************
embeddedartists 0:833824a40ced 18 * Typedefs and defines
embeddedartists 0:833824a40ced 19 *****************************************************************************/
embeddedartists 0:833824a40ced 20
embeddedartists 0:833824a40ced 21 #define RESET_FLAG \
embeddedartists 0:833824a40ced 22 do { \
embeddedartists 0:833824a40ced 23 if (abortTest) { \
embeddedartists 0:833824a40ced 24 abortTest = false; \
embeddedartists 0:833824a40ced 25 wait(0.04); \
embeddedartists 0:833824a40ced 26 } \
embeddedartists 0:833824a40ced 27 } while(false)
embeddedartists 0:833824a40ced 28
embeddedartists 0:833824a40ced 29 /******************************************************************************
embeddedartists 0:833824a40ced 30 * Local variables
embeddedartists 0:833824a40ced 31 *****************************************************************************/
embeddedartists 0:833824a40ced 32
embeddedartists 0:833824a40ced 33 static InterruptIn buttonInterrupt(P2_10);
embeddedartists 0:833824a40ced 34 static DigitalOut led(LED1);
embeddedartists 0:833824a40ced 35 static DigitalOut led3(LED3);
embeddedartists 0:833824a40ced 36
embeddedartists 0:833824a40ced 37 static BubbleDemo* bubbleDemo;
embeddedartists 0:833824a40ced 38
embeddedartists 0:833824a40ced 39 /******************************************************************************
embeddedartists 0:833824a40ced 40 * Global variables
embeddedartists 0:833824a40ced 41 *****************************************************************************/
embeddedartists 0:833824a40ced 42
embeddedartists 0:833824a40ced 43 EaLcdBoard lcdBoard(P0_27, P0_28);
embeddedartists 0:833824a40ced 44 bool abortTest = false;
embeddedartists 0:833824a40ced 45
embeddedartists 0:833824a40ced 46 /******************************************************************************
embeddedartists 0:833824a40ced 47 * Interrupt functions
embeddedartists 0:833824a40ced 48 *****************************************************************************/
embeddedartists 0:833824a40ced 49
embeddedartists 0:833824a40ced 50 void trigger() {
embeddedartists 0:833824a40ced 51 abortTest = true;
embeddedartists 0:833824a40ced 52 }
embeddedartists 0:833824a40ced 53
embeddedartists 0:833824a40ced 54 /******************************************************************************
embeddedartists 0:833824a40ced 55 * Main
embeddedartists 0:833824a40ced 56 *****************************************************************************/
embeddedartists 0:833824a40ced 57
embeddedartists 0:833824a40ced 58 void bubble_thread(void const *argument) {
embeddedartists 0:833824a40ced 59 while (true) {
embeddedartists 0:833824a40ced 60 bubbleDemo->run(750, 20);
embeddedartists 0:833824a40ced 61 RESET_FLAG;
embeddedartists 0:833824a40ced 62 }
embeddedartists 0:833824a40ced 63 }
embeddedartists 0:833824a40ced 64 void net_thread(void const *argument) {
embeddedartists 0:833824a40ced 65 EthernetInterface eth;
embeddedartists 0:833824a40ced 66 eth.init(); //Use DHCP
embeddedartists 0:833824a40ced 67 eth.connect();
embeddedartists 0:833824a40ced 68 printf("IP Address is %s\n", eth.getIPAddress());
embeddedartists 0:833824a40ced 69 TCPSocketConnection sock;
embeddedartists 0:833824a40ced 70 while (true) {
embeddedartists 0:833824a40ced 71 led3 = !led3;
embeddedartists 0:833824a40ced 72 sock.connect("mbed.org", 80);
embeddedartists 0:833824a40ced 73 sock.set_blocking(false, 3000);
embeddedartists 0:833824a40ced 74
embeddedartists 0:833824a40ced 75 char http_cmd[] = "GET /media/uploads/donatien/hello.txt HTTP/1.1\r\nHost: %s\r\n\r\n";
embeddedartists 0:833824a40ced 76 printf("sending:\n---\n%s\n---\n", http_cmd);
embeddedartists 0:833824a40ced 77 sock.send(http_cmd, sizeof(http_cmd) - 1);//, 3000);
embeddedartists 0:833824a40ced 78
embeddedartists 0:833824a40ced 79 char in_buf[256];
embeddedartists 0:833824a40ced 80 bool firstIteration = true;
embeddedartists 0:833824a40ced 81 int ret;
embeddedartists 0:833824a40ced 82 do {
embeddedartists 0:833824a40ced 83 printf("reading...\n");
embeddedartists 0:833824a40ced 84 ret = sock.receive(in_buf, 255);//, firstIteration?3000:0);
embeddedartists 0:833824a40ced 85 in_buf[ret] = '\0';
embeddedartists 0:833824a40ced 86
embeddedartists 0:833824a40ced 87 printf("Received %d chars from server: %s\n", ret, in_buf);
embeddedartists 0:833824a40ced 88 firstIteration = false;
embeddedartists 0:833824a40ced 89 } while ( ret == 255 ); // once we get less that full buffer we know all data has been received
embeddedartists 0:833824a40ced 90 // } while ( ret > 0 );
embeddedartists 0:833824a40ced 91
embeddedartists 0:833824a40ced 92 sock.close();
embeddedartists 0:833824a40ced 93
embeddedartists 0:833824a40ced 94 //eth.disconnect();
embeddedartists 0:833824a40ced 95
embeddedartists 0:833824a40ced 96 led3 = !led3;
embeddedartists 0:833824a40ced 97 osDelay(500);
embeddedartists 0:833824a40ced 98 }
embeddedartists 0:833824a40ced 99 }
embeddedartists 0:833824a40ced 100
embeddedartists 0:833824a40ced 101 osThreadDef(bubble_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
embeddedartists 0:833824a40ced 102 osThreadDef(net_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
embeddedartists 0:833824a40ced 103
embeddedartists 0:833824a40ced 104 int main (void) {
embeddedartists 0:833824a40ced 105
embeddedartists 0:833824a40ced 106 EaLcdBoard::Result result;
embeddedartists 0:833824a40ced 107 LcdController::Config lcdCfg;
embeddedartists 0:833824a40ced 108 uint32_t frameBuf1 = (uint32_t) SDRAM_BASE;
embeddedartists 0:833824a40ced 109
embeddedartists 0:833824a40ced 110 printf("Simple example of using network (HTTP GET) and LCD at the same time\n");
embeddedartists 0:833824a40ced 111
embeddedartists 0:833824a40ced 112 // Listen for button presses
embeddedartists 0:833824a40ced 113 buttonInterrupt.mode(PullUp);
embeddedartists 0:833824a40ced 114 buttonInterrupt.fall(&trigger);
embeddedartists 0:833824a40ced 115
embeddedartists 0:833824a40ced 116 do {
embeddedartists 0:833824a40ced 117 // framebuffer is put in SDRAM
embeddedartists 0:833824a40ced 118 if (sdram_init() == 1) {
embeddedartists 0:833824a40ced 119 printf("Failed to initialize SDRAM\n");
embeddedartists 0:833824a40ced 120 break;
embeddedartists 0:833824a40ced 121 }
embeddedartists 0:833824a40ced 122
embeddedartists 0:833824a40ced 123 result = lcdBoard.open(NULL, NULL);
embeddedartists 0:833824a40ced 124 if (result != EaLcdBoard::Ok) {
embeddedartists 0:833824a40ced 125 printf("Failed to open display: %d\n", result);
embeddedartists 0:833824a40ced 126 break;
embeddedartists 0:833824a40ced 127 }
embeddedartists 0:833824a40ced 128
embeddedartists 0:833824a40ced 129 result = lcdBoard.setFrameBuffer(frameBuf1);
embeddedartists 0:833824a40ced 130 if (result != EaLcdBoard::Ok) {
embeddedartists 0:833824a40ced 131 printf("Failed to activate frameBuffer: %d\n", result);
embeddedartists 0:833824a40ced 132 break;
embeddedartists 0:833824a40ced 133 }
embeddedartists 0:833824a40ced 134
embeddedartists 0:833824a40ced 135 result = lcdBoard.getLcdConfig(&lcdCfg);
embeddedartists 0:833824a40ced 136 if (result != EaLcdBoard::Ok) {
embeddedartists 0:833824a40ced 137 printf("Failed to get LCD configuration: %d\n", result);
embeddedartists 0:833824a40ced 138 break;
embeddedartists 0:833824a40ced 139 }
embeddedartists 0:833824a40ced 140
embeddedartists 0:833824a40ced 141 // Prepare 3 consequtive framebuffers (2 will be used for background buffers)
embeddedartists 0:833824a40ced 142 memset((void*)frameBuf1, 0x0, lcdCfg.width*lcdCfg.height*2 *3);
embeddedartists 0:833824a40ced 143
embeddedartists 0:833824a40ced 144 bubbleDemo = new BubbleDemo((uint8_t *)frameBuf1, lcdCfg.width, lcdCfg.height);
embeddedartists 0:833824a40ced 145 osThreadCreate(osThread(bubble_thread), NULL);
embeddedartists 0:833824a40ced 146
embeddedartists 0:833824a40ced 147 osThreadCreate(osThread(net_thread), NULL);
embeddedartists 0:833824a40ced 148
embeddedartists 0:833824a40ced 149 while (true) {
embeddedartists 0:833824a40ced 150 led = 0;
embeddedartists 0:833824a40ced 151 wait(0.1);
embeddedartists 0:833824a40ced 152 led = 1;
embeddedartists 0:833824a40ced 153 wait(0.5);
embeddedartists 0:833824a40ced 154 }
embeddedartists 0:833824a40ced 155 } while(0);
embeddedartists 0:833824a40ced 156
embeddedartists 0:833824a40ced 157 // Blink to indicate error
embeddedartists 0:833824a40ced 158 while (1) {
embeddedartists 0:833824a40ced 159 led = 0;
embeddedartists 0:833824a40ced 160 wait(0.2);
embeddedartists 0:833824a40ced 161 led = 1;
embeddedartists 0:833824a40ced 162 wait(0.2);
embeddedartists 0:833824a40ced 163 }
embeddedartists 0:833824a40ced 164 }