_Very_ simple wrapper to a WiFly module connected via serial port.
wifly.cpp@0:b5ee41a1ca65, 2011-06-16 (annotated)
- Committer:
- tylerwilson
- Date:
- Thu Jun 16 22:05:55 2011 +0000
- Revision:
- 0:b5ee41a1ca65
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tylerwilson | 0:b5ee41a1ca65 | 1 | // |
tylerwilson | 0:b5ee41a1ca65 | 2 | // Wrapper for the WiFly module, using the serial port |
tylerwilson | 0:b5ee41a1ca65 | 3 | // |
tylerwilson | 0:b5ee41a1ca65 | 4 | // Copyright 2010 Pulse-Robotics, Inc. |
tylerwilson | 0:b5ee41a1ca65 | 5 | // John Sosoka, Tyler Wilson |
tylerwilson | 0:b5ee41a1ca65 | 6 | // |
tylerwilson | 0:b5ee41a1ca65 | 7 | |
tylerwilson | 0:b5ee41a1ca65 | 8 | #include "wifly.h" |
tylerwilson | 0:b5ee41a1ca65 | 9 | |
tylerwilson | 0:b5ee41a1ca65 | 10 | |
tylerwilson | 0:b5ee41a1ca65 | 11 | WiFly::WiFly(PinName tx, PinName rx) |
tylerwilson | 0:b5ee41a1ca65 | 12 | : port(0) |
tylerwilson | 0:b5ee41a1ca65 | 13 | { |
tylerwilson | 0:b5ee41a1ca65 | 14 | port = new Serial(tx, rx); |
tylerwilson | 0:b5ee41a1ca65 | 15 | |
tylerwilson | 0:b5ee41a1ca65 | 16 | if (port) |
tylerwilson | 0:b5ee41a1ca65 | 17 | { |
tylerwilson | 0:b5ee41a1ca65 | 18 | port->baud(9600); |
tylerwilson | 0:b5ee41a1ca65 | 19 | } |
tylerwilson | 0:b5ee41a1ca65 | 20 | } |
tylerwilson | 0:b5ee41a1ca65 | 21 | |
tylerwilson | 0:b5ee41a1ca65 | 22 | void WiFly::commandMode() const |
tylerwilson | 0:b5ee41a1ca65 | 23 | { |
tylerwilson | 0:b5ee41a1ca65 | 24 | if (port) |
tylerwilson | 0:b5ee41a1ca65 | 25 | { |
tylerwilson | 0:b5ee41a1ca65 | 26 | port->printf("$$$"); |
tylerwilson | 0:b5ee41a1ca65 | 27 | // wait for 250ms |
tylerwilson | 0:b5ee41a1ca65 | 28 | |
tylerwilson | 0:b5ee41a1ca65 | 29 | char result[4]; |
tylerwilson | 0:b5ee41a1ca65 | 30 | port->scanf("%c%c%c", result[0], result[1], result[2]); |
tylerwilson | 0:b5ee41a1ca65 | 31 | result[3] = 0; |
tylerwilson | 0:b5ee41a1ca65 | 32 | |
tylerwilson | 0:b5ee41a1ca65 | 33 | if (strcmp(result, "CMD") != 0) |
tylerwilson | 0:b5ee41a1ca65 | 34 | { |
tylerwilson | 0:b5ee41a1ca65 | 35 | // not the expected reply |
tylerwilson | 0:b5ee41a1ca65 | 36 | } |
tylerwilson | 0:b5ee41a1ca65 | 37 | } |
tylerwilson | 0:b5ee41a1ca65 | 38 | } |
tylerwilson | 0:b5ee41a1ca65 | 39 | |
tylerwilson | 0:b5ee41a1ca65 | 40 | void WiFly::dataMode() const |
tylerwilson | 0:b5ee41a1ca65 | 41 | { |
tylerwilson | 0:b5ee41a1ca65 | 42 | if (port) |
tylerwilson | 0:b5ee41a1ca65 | 43 | { |
tylerwilson | 0:b5ee41a1ca65 | 44 | port->printf("exit\r"); |
tylerwilson | 0:b5ee41a1ca65 | 45 | |
tylerwilson | 0:b5ee41a1ca65 | 46 | char result[5]; |
tylerwilson | 0:b5ee41a1ca65 | 47 | port->scanf("%c%c%c%c", result[0], result[1], result[2], result[3]); |
tylerwilson | 0:b5ee41a1ca65 | 48 | result[4] = 0; |
tylerwilson | 0:b5ee41a1ca65 | 49 | |
tylerwilson | 0:b5ee41a1ca65 | 50 | if (strcmp(result, "EXIT") != 0) |
tylerwilson | 0:b5ee41a1ca65 | 51 | { |
tylerwilson | 0:b5ee41a1ca65 | 52 | // not the expected reply |
tylerwilson | 0:b5ee41a1ca65 | 53 | } |
tylerwilson | 0:b5ee41a1ca65 | 54 | } |
tylerwilson | 0:b5ee41a1ca65 | 55 | } |
tylerwilson | 0:b5ee41a1ca65 | 56 | |
tylerwilson | 0:b5ee41a1ca65 | 57 | |
tylerwilson | 0:b5ee41a1ca65 | 58 | // Serial methods |
tylerwilson | 0:b5ee41a1ca65 | 59 | void WiFly::baud(int baudrate) |
tylerwilson | 0:b5ee41a1ca65 | 60 | { |
tylerwilson | 0:b5ee41a1ca65 | 61 | port->baud(baudrate); |
tylerwilson | 0:b5ee41a1ca65 | 62 | } |
tylerwilson | 0:b5ee41a1ca65 | 63 | |
tylerwilson | 0:b5ee41a1ca65 | 64 | int WiFly::getc() |
tylerwilson | 0:b5ee41a1ca65 | 65 | { |
tylerwilson | 0:b5ee41a1ca65 | 66 | return port->getc(); |
tylerwilson | 0:b5ee41a1ca65 | 67 | } |
tylerwilson | 0:b5ee41a1ca65 | 68 | |
tylerwilson | 0:b5ee41a1ca65 | 69 | void WiFly::putc(int c) |
tylerwilson | 0:b5ee41a1ca65 | 70 | { |
tylerwilson | 0:b5ee41a1ca65 | 71 | port->putc(c); |
tylerwilson | 0:b5ee41a1ca65 | 72 | } |
tylerwilson | 0:b5ee41a1ca65 | 73 | |
tylerwilson | 0:b5ee41a1ca65 | 74 | int WiFly::readable() |
tylerwilson | 0:b5ee41a1ca65 | 75 | { |
tylerwilson | 0:b5ee41a1ca65 | 76 | return port->readable(); |
tylerwilson | 0:b5ee41a1ca65 | 77 | } |
tylerwilson | 0:b5ee41a1ca65 | 78 |