DISCO-F746NG_LCDTS_CC3000_NTP

Dependencies:   BSP_DISCO_F746NG LCD_DISCO_F746NG NTPClient NVIC_set_all_priorities TS_DISCO_F746NG cc3000_hostdriver_mbedsocket mbed

Fork of DISCO-F746NG_LCDTS_demo by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "cc3000.h"
00003 #include "main.h"
00004 #include "NTPClient.h"
00005 #include "TS_DISCO_F746NG.h"
00006 #include "LCD_DISCO_F746NG.h"
00007 
00008 #define CC3000_IRQ   D3  // (D3)
00009 #define CC3000_EN    D5  // (D5)
00010 #define CC3000_CS    D10 // (D10)
00011 #define CC3000_MOSI  D11 // (D11)
00012 #define CC3000_MISO  D12 // (D12)
00013 #define CC3000_SCLK  D13 // (D13)
00014 
00015 #define SSID         "Prakjaroen"
00016 #define PHRASE       "A4B5C6D7E8F9"
00017 #define SECURITY     WPA2
00018 
00019 #define IP           "192.168.2.165"
00020 #define MASK         "255.255.255.0"
00021 #define GW           "192.168.2.1"
00022 
00023 #define DHCP         1
00024 
00025 #if (DHCP == 1)
00026     #define STATIC_IP    0
00027     #define IP_INIT      DHCP
00028 #else
00029     #define STATIC_IP    1
00030     #define IP_INIT      STATIC_IP
00031 #endif
00032 
00033 using namespace mbed_cc3000;
00034 
00035 #define MY_BOARD MBED_BOARD_EXAMPLE
00036 
00037 /* cc3000 module declaration specific for user's board. Check also init() */
00038 #if (MY_BOARD == WIGO)
00039 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), SSID, PHRASE, SECURITY, false);
00040 Serial pc(USBTX, USBRX);
00041 #elif (MY_BOARD == WIFI_DIPCORTEX)
00042 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), SSID, PHRASE, SECURITY, false);
00043 Serial pc(UART_TX, UART_RX);
00044 #elif (MY_BOARD == MBED_BOARD_EXAMPLE)
00045 cc3000 wifi(CC3000_IRQ, CC3000_EN, CC3000_CS, SPI(CC3000_MOSI, CC3000_MISO, CC3000_SCLK), SSID, PHRASE, SECURITY, false); //SparkFun Board on Arduino pin definitions
00046 //Serial pc(USBTX, USBRX);
00047 //    Serial pc(P4_28, P4_29);
00048 
00049     #if defined(TARGET_NUCLEO_F411RE)
00050     Serial pc(USBTX, USBRX);
00051     #elif defined(TARGET_NUCLEO_F446RE)
00052     Serial pc(USBTX, USBRX);
00053     #elif defined(TARGET_ARCH_PRO)
00054     Serial pc(P4_28, P4_29);
00055     #elif defined(TARGET_ARCH_MAX)
00056     Serial pc(USBTX, USBRX);
00057     Serial bt(D6, D7);  // tx, rx, Arch Max
00058     #elif defined(TARGET_LPC4337)
00059     Serial pc(P2_10, P2_11); // TX, RX
00060     #elif defined(TARGET_DISCO_F746NG)
00061     Serial pc(USBTX, USBRX);
00062     #else
00063     Serial pc(USBTX, USBRX);
00064     #endif
00065 
00066 
00067 
00068 #else
00069 
00070 #endif
00071 
00072 // array to store RM parameters from EEPROM
00073 unsigned char cRMParamsFromEeprom[128];
00074 
00075 // array to store MAC address from EEPROM
00076 unsigned char cMacFromEeprom[6];
00077 
00078 LCD_DISCO_F746NG lcd;
00079 TS_DISCO_F746NG ts;
00080 
00081 int main()
00082 {
00083     uint8_t firmware_ver[2];
00084     signed char mac_status = -1;
00085     unsigned char FW_status = 1;
00086 
00087     init(); // board dependent init
00088     pc.baud(230400);
00089     printf("\r\n--------------------------------------------------------------------------------\r\n");
00090 
00091     printf("Target   : ");
00092 #if defined(TARGET_NUCLEO_F411RE)
00093     printf("NUCLEO F411RE\r\n");
00094 #elif defined(TARGET_NUCLEO_F446RE)
00095     printf("NUCLEO F446RE\r\n");
00096 #elif defined(TARGET_ARCH_PRO)
00097     printf("Seeed Arch PRO\r\n");
00098 #elif defined(TARGET_ARCH_MAX)
00099     printf("Seeed Arch Max\r\n");
00100 #elif defined(TARGET_LPC4337)
00101     printf("LPC4337\r\n");
00102 #elif defined(TARGET_DISCO_F746NG)
00103     printf("DISCO_F746NG\r\n");
00104 #else
00105     printf("Unknown\r\n");
00106 #endif
00107 
00108     printf("CPU Clock: %3.3f MHz\r\n", ((float)SystemCoreClock/1000000.0));
00109 
00110     printf("MAC      : ");
00111     char mac_board[6];
00112     mbed_mac_address(mac_board);
00113     for(int i=0; i<6;i++) {
00114         printf("%02X", mac_board[i]);
00115         if (i < 5) printf(":");
00116     }
00117     printf("\r\n");
00118 
00119 
00120     printf("mbed cc3000 NTP client demo.\r\n");
00121 
00122 #if (DHCP == 1)
00123     printf("Initialize the interface using DHCP...\r\n");
00124     printf("wifi.init() ");
00125     wifi.init();
00126 #else
00127     printf("Initialize the interface using a static IP address...\r\n");
00128     printf("wifi.init(%s, %s, %s) ", IP, MASK, GW);
00129     wifi.init(IP, MASK, GW);
00130 #endif
00131     printf("done.\r\n");
00132 
00133     printf("Read from cc3000...\r\n");
00134         
00135     // Read Firmware Version and MAC Address
00136     FW_status = wifi.read_sp_version(firmware_ver);   // read actual Firmware version
00137     if(FW_status == 0){
00138         printf("Firmware version: %d.%d\r\n", firmware_ver[0], firmware_ver[1]);
00139         mac_status = wifi.get_mac_address(cMacFromEeprom);
00140         if(mac_status == 0){
00141             printf("MAC address     : ");
00142             for(int i = 0; i < 6; i++) {
00143                 printf("%02X", cMacFromEeprom[i]);
00144                 if (i < 5) printf(":");
00145             }
00146             printf("\r\n");
00147         }
00148     } else {
00149         printf("ERROR: CC3000 not found - check connections !\r\n");
00150     }
00151 
00152     // WiFi parameters
00153     printf("WiFi parameters:\r\n");
00154     printf("SSID            : %s\r\n", SSID);
00155     printf("Phrase          : %s\r\n", PHRASE);
00156     printf("Security        : ");
00157     if (SECURITY == 0) printf("NONE");
00158     if (SECURITY == 1) printf("WEP");
00159     if (SECURITY == 2) printf("WPA");
00160     if (SECURITY == 3) printf("WPA2");
00161     printf("\r\n");
00162     
00163     wait(1.0);
00164   
00165     // Connect to WiFi
00166     printf("Bring the interface up...\r\n");
00167     if (wifi.connect() == -1) {
00168         printf("ERROR: Failed to connect. Please verify connection details and try again.\r\n");
00169     }
00170     char *ip     = wifi.getIPAddress();
00171     char *mask   = wifi.getNetworkMask();
00172     char *gate   = wifi.getGateway();
00173     char *mac    = wifi.getMACAddress();
00174     bool conn    = wifi.is_connected();
00175     bool dhcp    = wifi.is_dhcp_configured();
00176     bool enabled = wifi.is_enabled();
00177     
00178     printf("IP              : %s\r\n", ip);
00179     printf("Netmask         : %s\r\n", mask);
00180     printf("Gateway         : %s\r\n", gate);
00181     printf("MAC             : %s\r\n", mac);
00182     printf("Connected       : %d\r\n", conn);
00183     printf("DHCP            : %d\r\n", dhcp);
00184     printf("Enabled         : %d\r\n", enabled);
00185 
00186     wait(1.0);
00187     
00188     
00189     // Read time from server
00190     NTPClient ntp_client;
00191     // NTP Server Parameters
00192 //    char* domain_name = "217.114.59.3";
00193     char* domain_name = "2.nl.pool.ntp.org";
00194 //    char* domain_name = "0.uk.pool.ntp.org";
00195     int   port_number = 123;
00196     time_t ct_time;
00197     char time_buffer[80];    
00198     char time_buffer_old[80];    
00199 
00200     printf("Reading time: ");
00201     if (ntp_client.setTime(domain_name, port_number) == 0) {
00202         printf("Passed.\r\n");
00203         ct_time = time(NULL) + 7200; // Summer time - Convert to Europe/Amsterdam Time
00204         set_time(ct_time);
00205         strftime(time_buffer, 80, "%a %d-%b-%Y %T", localtime(&ct_time));
00206         printf("Time            : %s\r\n", time_buffer);
00207     } else {
00208         printf("FAILED!\r\n");
00209     }
00210 
00211     strcpy(time_buffer_old, "Dummy value");
00212 
00213 
00214     TS_StateTypeDef TS_State;
00215     uint16_t x, y;
00216     uint8_t text[30];
00217     uint8_t status;
00218     uint8_t idx;
00219     uint8_t cleared = 0;
00220     uint8_t prev_nb_touches = 0;
00221 
00222     lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE);
00223     wait(1);
00224 
00225     status = ts.Init(lcd.GetXSize(), lcd.GetYSize());
00226     if (status != TS_OK) {
00227         lcd.Clear(LCD_COLOR_RED);
00228         lcd.SetBackColor(LCD_COLOR_RED);
00229         lcd.SetTextColor(LCD_COLOR_WHITE);
00230         lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);
00231     } else {
00232         lcd.Clear(LCD_COLOR_GREEN);
00233         lcd.SetBackColor(LCD_COLOR_GREEN);
00234         lcd.SetTextColor(LCD_COLOR_WHITE);
00235         lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE);
00236     }
00237 
00238     wait(1);
00239     lcd.SetFont(&Font12);
00240     lcd.SetBackColor(LCD_COLOR_BLUE);
00241     lcd.SetTextColor(LCD_COLOR_WHITE);
00242 
00243     while(1) {
00244 
00245         ct_time = time(NULL);
00246 //        strftime(time_buffer, 80, "%S", localtime(&ct_time));
00247         strftime(time_buffer, 80, "%a %d-%b-%Y %T", localtime(&ct_time));
00248         if (strcmp(time_buffer, time_buffer_old) != 0) {
00249             strcpy(time_buffer_old, time_buffer);
00250             printf("%s\r\n", time_buffer);
00251     lcd.DisplayStringAt(0, LINE(15), (uint8_t *)time_buffer, CENTER_MODE);
00252             strftime(time_buffer, 80, "%T", localtime(&ct_time));
00253             // 21:00:00
00254             if ((time_buffer[3] == '0') && (time_buffer[4] == '0') && 
00255                 (time_buffer[6] == '0') && (time_buffer[7] == '0')) {
00256                 // Sync once per hour
00257                 printf("Reading time: ");
00258                 if (ntp_client.setTime(domain_name, port_number) == 0) {
00259                     printf("Passed.\r\n");
00260                     ct_time = time(NULL) + 7200; // Summer time - Convert to Europe/Amsterdam Time
00261                     set_time(ct_time);
00262                     strftime(time_buffer, 80, "%a %d-%b-%Y %T", localtime(&ct_time));
00263 //                    printf("Time            : %s\r\n", time_buffer);
00264     lcd.DisplayStringAt(0, LINE(15), (uint8_t *)time_buffer, CENTER_MODE);
00265                 } else {
00266                     printf("FAILED!\r\n");
00267                 }
00268             }
00269         }
00270 
00271         ts.GetState(&TS_State);
00272         if (TS_State.touchDetected) {
00273             // Clear lines corresponding to old touches coordinates
00274             if (TS_State.touchDetected < prev_nb_touches) {
00275                 for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) {
00276                     lcd.ClearStringLine(idx);
00277                 }
00278             }
00279             prev_nb_touches = TS_State.touchDetected;
00280 
00281             cleared = 0;
00282 
00283             sprintf((char*)text, "Touches: %d", TS_State.touchDetected);
00284             lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
00285 
00286             for (idx = 0; idx < TS_State.touchDetected; idx++) {
00287                 x = TS_State.touchX[idx];
00288                 y = TS_State.touchY[idx];
00289                 sprintf((char*)text, "Touch %d: x=%d y=%d    ", idx+1, x, y);
00290                 lcd.DisplayStringAt(0, LINE(idx+1), (uint8_t *)&text, LEFT_MODE);
00291             }
00292 
00293             lcd.DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE);
00294         } else {
00295             if (!cleared) {
00296                 lcd.Clear(LCD_COLOR_BLUE);
00297                 sprintf((char*)text, "Touches: 0");
00298                 lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
00299                 cleared = 1;
00300             }
00301         }
00302     }
00303 }