A WiFiDipCortex based robot. Control is via sockets over WiFi. See also: https://github.com/mfurseman/robo-android
Dependencies: Motordriver USBDevice cc3000_hostdriver_mbedsocket_hacked mbed
main.cpp@1:b66a2d756c8a, 2014-10-21 (annotated)
- Committer:
- mfurseman
- Date:
- Tue Oct 21 14:03:50 2014 +0000
- Revision:
- 1:b66a2d756c8a
- Parent:
- 0:993d6b65e255
- Child:
- 2:50c151183047
Tidied up working code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfurseman | 0:993d6b65e255 | 1 | #include "mbed.h" |
mfurseman | 0:993d6b65e255 | 2 | #include "cc3000.h" |
mfurseman | 0:993d6b65e255 | 3 | #include "USBSerial.h" |
mfurseman | 0:993d6b65e255 | 4 | |
mfurseman | 1:b66a2d756c8a | 5 | |
mfurseman | 0:993d6b65e255 | 6 | /* Quickly change debug flag to remove blocking serial code */ |
mfurseman | 0:993d6b65e255 | 7 | #define DEBUG |
mfurseman | 0:993d6b65e255 | 8 | #ifdef DEBUG |
mfurseman | 0:993d6b65e255 | 9 | USBSerial serial; |
mfurseman | 0:993d6b65e255 | 10 | #define debug(x, ...) serial.printf(x, ##__VA_ARGS__); |
mfurseman | 0:993d6b65e255 | 11 | #else |
mfurseman | 0:993d6b65e255 | 12 | #define debug(x, ...) |
mfurseman | 0:993d6b65e255 | 13 | #endif |
mfurseman | 0:993d6b65e255 | 14 | |
mfurseman | 1:b66a2d756c8a | 15 | |
mfurseman | 0:993d6b65e255 | 16 | using namespace mbed_cc3000; |
mfurseman | 0:993d6b65e255 | 17 | |
mfurseman | 1:b66a2d756c8a | 18 | |
mfurseman | 0:993d6b65e255 | 19 | /* On board LED */ |
mfurseman | 0:993d6b65e255 | 20 | DigitalOut led(P0_1); |
mfurseman | 0:993d6b65e255 | 21 | |
mfurseman | 0:993d6b65e255 | 22 | /* Serial library for WiFi module */ |
mfurseman | 0:993d6b65e255 | 23 | cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37)); |
mfurseman | 0:993d6b65e255 | 24 | |
mfurseman | 0:993d6b65e255 | 25 | /* Struct to hold connection data */ |
mfurseman | 0:993d6b65e255 | 26 | tNetappIpconfigRetArgs ipinfo; |
mfurseman | 0:993d6b65e255 | 27 | |
mfurseman | 0:993d6b65e255 | 28 | |
mfurseman | 1:b66a2d756c8a | 29 | /* Prints CC3000 connection info */ |
mfurseman | 0:993d6b65e255 | 30 | void printConnectionInfo() { |
mfurseman | 0:993d6b65e255 | 31 | if (( wifi.is_enabled() ) && ( wifi.is_dhcp_configured() )) { |
mfurseman | 0:993d6b65e255 | 32 | wifi.get_ip_config(&ipinfo); |
mfurseman | 0:993d6b65e255 | 33 | } |
mfurseman | 0:993d6b65e255 | 34 | debug("\r\n"); |
mfurseman | 0:993d6b65e255 | 35 | if (! wifi.is_enabled() ) { |
mfurseman | 0:993d6b65e255 | 36 | debug("CC3000 Disabled\r\n"); |
mfurseman | 0:993d6b65e255 | 37 | } |
mfurseman | 0:993d6b65e255 | 38 | else if ( wifi.is_dhcp_configured() ) { |
mfurseman | 0:993d6b65e255 | 39 | debug("SSID : %-33s|\r\n", ipinfo.uaSSID); |
mfurseman | 0:993d6b65e255 | 40 | debug("IP : %-35s|\r\n", wifi.getIPAddress()); |
mfurseman | 0:993d6b65e255 | 41 | } |
mfurseman | 0:993d6b65e255 | 42 | else if ( wifi.is_connected() ) { |
mfurseman | 0:993d6b65e255 | 43 | debug("Connecting, waiting for DHCP\r\n"); |
mfurseman | 0:993d6b65e255 | 44 | } |
mfurseman | 0:993d6b65e255 | 45 | else { |
mfurseman | 0:993d6b65e255 | 46 | debug("Not Connected\r\n"); |
mfurseman | 0:993d6b65e255 | 47 | } |
mfurseman | 0:993d6b65e255 | 48 | } |
mfurseman | 0:993d6b65e255 | 49 | |
mfurseman | 1:b66a2d756c8a | 50 | |
mfurseman | 1:b66a2d756c8a | 51 | /* WiFi DipCortex board setup */ |
mfurseman | 1:b66a2d756c8a | 52 | void init() { |
mfurseman | 0:993d6b65e255 | 53 | NVIC_SetPriority(SSP1_IRQn, 0x0); |
mfurseman | 0:993d6b65e255 | 54 | NVIC_SetPriority(PIN_INT0_IRQn, 0x1); |
mfurseman | 0:993d6b65e255 | 55 | |
mfurseman | 0:993d6b65e255 | 56 | // SysTick set to lower priority than Wi-Fi SPI bus interrupt |
mfurseman | 0:993d6b65e255 | 57 | NVIC_SetPriority(SysTick_IRQn, 0x2); |
mfurseman | 0:993d6b65e255 | 58 | |
mfurseman | 0:993d6b65e255 | 59 | // Enable RAM1 |
mfurseman | 0:993d6b65e255 | 60 | LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26); |
mfurseman | 1:b66a2d756c8a | 61 | |
mfurseman | 1:b66a2d756c8a | 62 | // This may be neccassary for CC3000 |
mfurseman | 0:993d6b65e255 | 63 | wait(1); |
mfurseman | 1:b66a2d756c8a | 64 | } |
mfurseman | 1:b66a2d756c8a | 65 | |
mfurseman | 0:993d6b65e255 | 66 | |
mfurseman | 1:b66a2d756c8a | 67 | /* Connects WiFi assuming existing SmartConfig */ |
mfurseman | 1:b66a2d756c8a | 68 | void connectWifi() { |
mfurseman | 0:993d6b65e255 | 69 | wifi.start(0); |
mfurseman | 0:993d6b65e255 | 70 | wait_ms(750); |
mfurseman | 1:b66a2d756c8a | 71 | wifi._wlan.ioctl_set_connection_policy(0, 0, 1); |
mfurseman | 1:b66a2d756c8a | 72 | // TODO: Timeout and switch on smart config here |
mfurseman | 1:b66a2d756c8a | 73 | } |
mfurseman | 0:993d6b65e255 | 74 | |
mfurseman | 1:b66a2d756c8a | 75 | |
mfurseman | 1:b66a2d756c8a | 76 | int main(void) { |
mfurseman | 1:b66a2d756c8a | 77 | init(); |
mfurseman | 1:b66a2d756c8a | 78 | connectWifi(); |
mfurseman | 0:993d6b65e255 | 79 | |
mfurseman | 0:993d6b65e255 | 80 | while(1) { |
mfurseman | 0:993d6b65e255 | 81 | printConnectionInfo(); |
mfurseman | 0:993d6b65e255 | 82 | debug("test message\r\n"); |
mfurseman | 0:993d6b65e255 | 83 | led = !led; |
mfurseman | 0:993d6b65e255 | 84 | wait(1); |
mfurseman | 0:993d6b65e255 | 85 | } |
mfurseman | 0:993d6b65e255 | 86 | } |