Lab3_Surveillance

Dependencies:   GPS mbed

Committer:
gtg846r
Date:
Thu Oct 13 17:42:27 2011 +0000
Revision:
2:ba06c987a111
Parent:
0:2af1e48a285d
Revision #3...Includes phone number input

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gtg846r 0:2af1e48a285d 1 /**
gtg846r 0:2af1e48a285d 2 * =============================================================================
gtg846r 0:2af1e48a285d 3 * LS-Y201 device driver class (Version 0.0.1)
gtg846r 0:2af1e48a285d 4 * Reference documents: LinkSprite JPEG Color Camera Serial UART Interface
gtg846r 0:2af1e48a285d 5 * January 2010
gtg846r 0:2af1e48a285d 6 * =============================================================================
gtg846r 0:2af1e48a285d 7 * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems)
gtg846r 0:2af1e48a285d 8 *
gtg846r 0:2af1e48a285d 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
gtg846r 0:2af1e48a285d 10 * of this software and associated documentation files (the "Software"), to deal
gtg846r 0:2af1e48a285d 11 * in the Software without restriction, including without limitation the rights
gtg846r 0:2af1e48a285d 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
gtg846r 0:2af1e48a285d 13 * copies of the Software, and to permit persons to whom the Software is
gtg846r 0:2af1e48a285d 14 * furnished to do so, subject to the following conditions:
gtg846r 0:2af1e48a285d 15 *
gtg846r 0:2af1e48a285d 16 * The above copyright notice and this permission notice shall be included in
gtg846r 0:2af1e48a285d 17 * all copies or substantial portions of the Software.
gtg846r 0:2af1e48a285d 18 *
gtg846r 0:2af1e48a285d 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
gtg846r 0:2af1e48a285d 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
gtg846r 0:2af1e48a285d 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
gtg846r 0:2af1e48a285d 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
gtg846r 0:2af1e48a285d 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gtg846r 0:2af1e48a285d 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
gtg846r 0:2af1e48a285d 25 * THE SOFTWARE.
gtg846r 0:2af1e48a285d 26 * =============================================================================
gtg846r 0:2af1e48a285d 27 */
gtg846r 0:2af1e48a285d 28
gtg846r 0:2af1e48a285d 29 #include "mbed.h"
gtg846r 0:2af1e48a285d 30 #include "SerialBuffered.h"
gtg846r 0:2af1e48a285d 31
gtg846r 0:2af1e48a285d 32 /**
gtg846r 0:2af1e48a285d 33 * Create a buffered serial class.
gtg846r 0:2af1e48a285d 34 *
gtg846r 0:2af1e48a285d 35 * @param tx A pin for transmit.
gtg846r 0:2af1e48a285d 36 * @param rx A pin for receive.
gtg846r 0:2af1e48a285d 37 */
gtg846r 0:2af1e48a285d 38 SerialBuffered::SerialBuffered(PinName tx, PinName rx) : Serial(tx, rx) {
gtg846r 0:2af1e48a285d 39 indexContentStart = 0;
gtg846r 0:2af1e48a285d 40 indexContentEnd = 0;
gtg846r 0:2af1e48a285d 41 timeout = 1;
gtg846r 0:2af1e48a285d 42 attach(this, &SerialBuffered::handleInterrupt);
gtg846r 0:2af1e48a285d 43 }
gtg846r 0:2af1e48a285d 44
gtg846r 0:2af1e48a285d 45 /**
gtg846r 0:2af1e48a285d 46 * Destroy.
gtg846r 0:2af1e48a285d 47 */
gtg846r 0:2af1e48a285d 48 SerialBuffered::~SerialBuffered() {
gtg846r 0:2af1e48a285d 49 }
gtg846r 0:2af1e48a285d 50
gtg846r 0:2af1e48a285d 51 /**
gtg846r 0:2af1e48a285d 52 * Set timeout for getc().
gtg846r 0:2af1e48a285d 53 *
gtg846r 0:2af1e48a285d 54 * @param ms milliseconds. (-1:Disable timeout)
gtg846r 0:2af1e48a285d 55 */
gtg846r 0:2af1e48a285d 56 void SerialBuffered::setTimeout(int ms) {
gtg846r 0:2af1e48a285d 57 timeout = ms;
gtg846r 0:2af1e48a285d 58 }
gtg846r 0:2af1e48a285d 59
gtg846r 0:2af1e48a285d 60 /**
gtg846r 0:2af1e48a285d 61 * Read requested bytes.
gtg846r 0:2af1e48a285d 62 *
gtg846r 0:2af1e48a285d 63 * @param bytes A pointer to a buffer.
gtg846r 0:2af1e48a285d 64 * @param requested Length.
gtg846r 0:2af1e48a285d 65 *
gtg846r 0:2af1e48a285d 66 * @return Readed byte length.
gtg846r 0:2af1e48a285d 67 */
gtg846r 0:2af1e48a285d 68 size_t SerialBuffered::readBytes(uint8_t *bytes, size_t requested) {
gtg846r 0:2af1e48a285d 69 int i = 0;
gtg846r 0:2af1e48a285d 70 while (i < requested) {
gtg846r 0:2af1e48a285d 71 int c = getc();
gtg846r 0:2af1e48a285d 72 if (c < 0) {
gtg846r 0:2af1e48a285d 73 break;
gtg846r 0:2af1e48a285d 74 }
gtg846r 0:2af1e48a285d 75 bytes[i] = c;
gtg846r 0:2af1e48a285d 76 i++;
gtg846r 0:2af1e48a285d 77 }
gtg846r 0:2af1e48a285d 78 return i;
gtg846r 0:2af1e48a285d 79 }
gtg846r 0:2af1e48a285d 80
gtg846r 0:2af1e48a285d 81 /**
gtg846r 0:2af1e48a285d 82 * Get a character.
gtg846r 0:2af1e48a285d 83 *
gtg846r 0:2af1e48a285d 84 * @return A character. (-1:timeout)
gtg846r 0:2af1e48a285d 85 */
gtg846r 0:2af1e48a285d 86 int SerialBuffered::getc() {
gtg846r 0:2af1e48a285d 87 timer.reset();
gtg846r 0:2af1e48a285d 88 timer.start();
gtg846r 0:2af1e48a285d 89 while (indexContentStart == indexContentEnd) {
gtg846r 0:2af1e48a285d 90 wait_ms(1);
gtg846r 0:2af1e48a285d 91 if ((timeout > 0) && (timer.read_ms() > timeout)) {
gtg846r 0:2af1e48a285d 92 /*
gtg846r 0:2af1e48a285d 93 * Timeout occured.
gtg846r 0:2af1e48a285d 94 */
gtg846r 0:2af1e48a285d 95 // printf("Timeout occured.\n");
gtg846r 0:2af1e48a285d 96 return EOF;
gtg846r 0:2af1e48a285d 97 }
gtg846r 0:2af1e48a285d 98 }
gtg846r 0:2af1e48a285d 99 timer.stop();
gtg846r 0:2af1e48a285d 100
gtg846r 0:2af1e48a285d 101 uint8_t result = buffer[indexContentStart++];
gtg846r 0:2af1e48a285d 102 indexContentStart = indexContentStart % BUFFERSIZE;
gtg846r 0:2af1e48a285d 103
gtg846r 0:2af1e48a285d 104 return result;
gtg846r 0:2af1e48a285d 105 }
gtg846r 0:2af1e48a285d 106
gtg846r 0:2af1e48a285d 107 /**
gtg846r 0:2af1e48a285d 108 * Returns 1 if there is a character available to read, 0 otherwise.
gtg846r 0:2af1e48a285d 109 */
gtg846r 0:2af1e48a285d 110 int SerialBuffered::readable() {
gtg846r 0:2af1e48a285d 111 return indexContentStart != indexContentEnd;
gtg846r 0:2af1e48a285d 112 }
gtg846r 0:2af1e48a285d 113
gtg846r 0:2af1e48a285d 114 void SerialBuffered::handleInterrupt() {
gtg846r 0:2af1e48a285d 115 while (Serial::readable()) {
gtg846r 0:2af1e48a285d 116 if (indexContentStart == ((indexContentEnd + 1) % BUFFERSIZE)) {
gtg846r 0:2af1e48a285d 117 /*
gtg846r 0:2af1e48a285d 118 * Buffer overrun occured.
gtg846r 0:2af1e48a285d 119 */
gtg846r 0:2af1e48a285d 120 // printf("Buffer overrun occured.\n");
gtg846r 0:2af1e48a285d 121 Serial::getc();
gtg846r 0:2af1e48a285d 122 } else {
gtg846r 0:2af1e48a285d 123 buffer[indexContentEnd++] = Serial::getc();
gtg846r 0:2af1e48a285d 124 indexContentEnd = indexContentEnd % BUFFERSIZE;
gtg846r 0:2af1e48a285d 125 }
gtg846r 0:2af1e48a285d 126 }
gtg846r 0:2af1e48a285d 127 }