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