Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ESDC2014 by
compass.cpp@4:a377ecb9364f, 2014-07-04 (annotated)
- Committer:
- terryLAI
- Date:
- Fri Jul 04 13:05:15 2014 +0000
- Revision:
- 4:a377ecb9364f
- Parent:
- 3:4306d042af6f
test;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TonyYI | 0:3417ca0a36c0 | 1 | #include "compass.h" |
| TonyYI | 3:4306d042af6f | 2 | |
| terryLAI | 4:a377ecb9364f | 3 | Compass::Compass(MySerial _compassSerial) |
| terryLAI | 4:a377ecb9364f | 4 | { |
| terryLAI | 4:a377ecb9364f | 5 | printf(" _compassSerial %p...\r\n",_compassSerial); |
| terryLAI | 4:a377ecb9364f | 6 | this->_compassSerial = _compassSerial; |
| terryLAI | 4:a377ecb9364f | 7 | temp=_compassSerial; |
| terryLAI | 4:a377ecb9364f | 8 | printf(" this->_compassSerial %p...\r\n",this->_compassSerial); |
| terryLAI | 4:a377ecb9364f | 9 | buffer = new uint8_t [DATA_BUFFER_SIZE]; |
| terryLAI | 4:a377ecb9364f | 10 | _degree = 0; |
| terryLAI | 4:a377ecb9364f | 11 | _MSB = 0; |
| terryLAI | 4:a377ecb9364f | 12 | _LSB = 0; |
| terryLAI | 4:a377ecb9364f | 13 | _in = _out = 0; |
| terryLAI | 4:a377ecb9364f | 14 | |
| terryLAI | 4:a377ecb9364f | 15 | printf(" this->_compassSerial %p...\r\n",this->_compassSerial); |
| TonyYI | 3:4306d042af6f | 16 | |
| terryLAI | 4:a377ecb9364f | 17 | |
| terryLAI | 4:a377ecb9364f | 18 | if(_compassSerial->writeable()) |
| terryLAI | 4:a377ecb9364f | 19 | { |
| terryLAI | 4:a377ecb9364f | 20 | printf("_writable()...\r\n"); |
| terryLAI | 4:a377ecb9364f | 21 | this->_compassSerial->putc('1'); |
| terryLAI | 4:a377ecb9364f | 22 | } |
| terryLAI | 4:a377ecb9364f | 23 | printf("_MSB = 1 wrote...\r\n"); |
| terryLAI | 4:a377ecb9364f | 24 | if(_compassSerial->writeable()) |
| terryLAI | 4:a377ecb9364f | 25 | { |
| terryLAI | 4:a377ecb9364f | 26 | printf("_writable()...\r\n"); |
| terryLAI | 4:a377ecb9364f | 27 | this->_compassSerial->putc('2'); |
| terryLAI | 4:a377ecb9364f | 28 | } |
| terryLAI | 4:a377ecb9364f | 29 | printf("_LSB = 2 wrote...\r\n"); |
| terryLAI | 4:a377ecb9364f | 30 | wait(0.1); |
| terryLAI | 4:a377ecb9364f | 31 | |
| TonyYI | 0:3417ca0a36c0 | 32 | } |
| TonyYI | 0:3417ca0a36c0 | 33 | |
| terryLAI | 4:a377ecb9364f | 34 | Compass::~Compass() |
| TonyYI | 0:3417ca0a36c0 | 35 | { |
| terryLAI | 4:a377ecb9364f | 36 | delete[] buffer; |
| TonyYI | 0:3417ca0a36c0 | 37 | } |
| TonyYI | 0:3417ca0a36c0 | 38 | |
| terryLAI | 4:a377ecb9364f | 39 | void Compass::putToBuffer(uint8_t _x) |
| TonyYI | 0:3417ca0a36c0 | 40 | { |
| terryLAI | 4:a377ecb9364f | 41 | buffer[_in++] = _x; |
| terryLAI | 4:a377ecb9364f | 42 | if(_in == DATA_BUFFER_SIZE-1) |
| TonyYI | 3:4306d042af6f | 43 | { |
| terryLAI | 4:a377ecb9364f | 44 | _in &= 0x00; |
| TonyYI | 3:4306d042af6f | 45 | } |
| TonyYI | 0:3417ca0a36c0 | 46 | } |
| TonyYI | 0:3417ca0a36c0 | 47 | |
| terryLAI | 4:a377ecb9364f | 48 | void Compass::clearBuffer() |
| TonyYI | 0:3417ca0a36c0 | 49 | { |
| terryLAI | 4:a377ecb9364f | 50 | _in = _out = 0; |
| terryLAI | 1:cbec1283a16a | 51 | } |
| terryLAI | 1:cbec1283a16a | 52 | |
| terryLAI | 4:a377ecb9364f | 53 | int Compass::read() |
| TonyYI | 3:4306d042af6f | 54 | { |
| terryLAI | 4:a377ecb9364f | 55 | printf("Entering Compass::read()...\r\n"); |
| terryLAI | 4:a377ecb9364f | 56 | printf(" this->_compassSerial %p...\r\n",this->_compassSerial); |
| terryLAI | 4:a377ecb9364f | 57 | printf(" temp %p...\r\n",temp); |
| terryLAI | 4:a377ecb9364f | 58 | |
| terryLAI | 4:a377ecb9364f | 59 | printf("_writable()?...\r\n"); |
| terryLAI | 4:a377ecb9364f | 60 | if(_compassSerial->writeable()) |
| terryLAI | 4:a377ecb9364f | 61 | { |
| terryLAI | 4:a377ecb9364f | 62 | printf("can write..\r\n"); |
| terryLAI | 4:a377ecb9364f | 63 | } |
| terryLAI | 4:a377ecb9364f | 64 | |
| terryLAI | 4:a377ecb9364f | 65 | printf("readable()?...\r\n"); |
| terryLAI | 4:a377ecb9364f | 66 | if(_compassSerial->writeable()) |
| terryLAI | 4:a377ecb9364f | 67 | { |
| terryLAI | 4:a377ecb9364f | 68 | printf("can read..\r\n"); |
| terryLAI | 4:a377ecb9364f | 69 | } |
| terryLAI | 4:a377ecb9364f | 70 | |
| terryLAI | 4:a377ecb9364f | 71 | stop(); |
| terryLAI | 4:a377ecb9364f | 72 | clearBuffer(); |
| terryLAI | 4:a377ecb9364f | 73 | |
| terryLAI | 4:a377ecb9364f | 74 | |
| terryLAI | 4:a377ecb9364f | 75 | printf("0\r\n"); |
| terryLAI | 4:a377ecb9364f | 76 | resume(); |
| terryLAI | 4:a377ecb9364f | 77 | if(buffer[_out++] == ACK_RESUME_MSB && buffer[_out++] == ACK_RESUME_LSB) |
| terryLAI | 4:a377ecb9364f | 78 | { |
| terryLAI | 4:a377ecb9364f | 79 | } |
| TonyYI | 3:4306d042af6f | 80 | else |
| terryLAI | 4:a377ecb9364f | 81 | { |
| terryLAI | 4:a377ecb9364f | 82 | clearBuffer(); |
| terryLAI | 4:a377ecb9364f | 83 | return -1; |
| terryLAI | 4:a377ecb9364f | 84 | } |
| terryLAI | 4:a377ecb9364f | 85 | |
| terryLAI | 4:a377ecb9364f | 86 | printf("1\r\n"); |
| terryLAI | 4:a377ecb9364f | 87 | run(); |
| terryLAI | 4:a377ecb9364f | 88 | if(buffer[_out++] == ACK_RUN_MSB && buffer[_out++] == ACK_RUN_LSB) |
| terryLAI | 4:a377ecb9364f | 89 | { |
| terryLAI | 4:a377ecb9364f | 90 | } |
| terryLAI | 4:a377ecb9364f | 91 | else |
| terryLAI | 4:a377ecb9364f | 92 | { |
| terryLAI | 4:a377ecb9364f | 93 | clearBuffer(); |
| terryLAI | 4:a377ecb9364f | 94 | return -1; |
| terryLAI | 4:a377ecb9364f | 95 | } |
| TonyYI | 3:4306d042af6f | 96 | |
| terryLAI | 4:a377ecb9364f | 97 | printf("2\r\n"); |
| terryLAI | 4:a377ecb9364f | 98 | _MSB = buffer[_out++]; |
| terryLAI | 4:a377ecb9364f | 99 | _LSB = buffer[_out++]; |
| terryLAI | 4:a377ecb9364f | 100 | printf("_MSB: %x, _LSB: %x\r\n", _MSB, _LSB); |
| terryLAI | 4:a377ecb9364f | 101 | |
| terryLAI | 4:a377ecb9364f | 102 | printf("3\r\n"); |
| terryLAI | 4:a377ecb9364f | 103 | stop(); |
| terryLAI | 4:a377ecb9364f | 104 | if(buffer[_in - 1] == ACK_STOP_MSB && buffer[_in] == ACK_STOP_LSB) |
| terryLAI | 4:a377ecb9364f | 105 | { |
| terryLAI | 4:a377ecb9364f | 106 | } |
| terryLAI | 4:a377ecb9364f | 107 | else |
| terryLAI | 4:a377ecb9364f | 108 | { |
| terryLAI | 4:a377ecb9364f | 109 | return -1; |
| terryLAI | 4:a377ecb9364f | 110 | } |
| terryLAI | 4:a377ecb9364f | 111 | _out = _in; |
| terryLAI | 4:a377ecb9364f | 112 | clearBuffer(); |
| terryLAI | 4:a377ecb9364f | 113 | _degree=0; |
| terryLAI | 4:a377ecb9364f | 114 | printf("Exiting Compass::read()...\r\n"); |
| terryLAI | 4:a377ecb9364f | 115 | return _degree; |
| TonyYI | 3:4306d042af6f | 116 | } |
| terryLAI | 4:a377ecb9364f | 117 | |
| terryLAI | 4:a377ecb9364f | 118 | void Compass::run() |
| terryLAI | 4:a377ecb9364f | 119 | { |
| terryLAI | 4:a377ecb9364f | 120 | printf("Entering Compass::run()...\r\n"); |
| terryLAI | 4:a377ecb9364f | 121 | write2Bytes(RUN_MSB, RUN_LSB); |
| terryLAI | 4:a377ecb9364f | 122 | } |
| terryLAI | 4:a377ecb9364f | 123 | |
| terryLAI | 4:a377ecb9364f | 124 | void Compass::stop() |
| terryLAI | 4:a377ecb9364f | 125 | { |
| terryLAI | 4:a377ecb9364f | 126 | printf("Entering Compass::stop()...\r\n"); |
| terryLAI | 4:a377ecb9364f | 127 | write2Bytes(STOP_MSB, STOP_LSB); |
| terryLAI | 4:a377ecb9364f | 128 | } |
| terryLAI | 4:a377ecb9364f | 129 | |
| terryLAI | 4:a377ecb9364f | 130 | void Compass::resume() |
| terryLAI | 4:a377ecb9364f | 131 | { |
| terryLAI | 4:a377ecb9364f | 132 | printf("Entering Compass::resume()...\r\n"); |
| terryLAI | 4:a377ecb9364f | 133 | write2Bytes(RESUME_MSB, RESUME_LSB); |
| terryLAI | 4:a377ecb9364f | 134 | } |
| terryLAI | 4:a377ecb9364f | 135 | |
| terryLAI | 4:a377ecb9364f | 136 | void Compass::reset() |
| terryLAI | 4:a377ecb9364f | 137 | { |
| terryLAI | 4:a377ecb9364f | 138 | printf("Entering Compass::reset()...\r\n"); |
| terryLAI | 4:a377ecb9364f | 139 | write2Bytes(RST_MSB, RST_LSB); |
| terryLAI | 4:a377ecb9364f | 140 | } |
| terryLAI | 4:a377ecb9364f | 141 | |
| terryLAI | 4:a377ecb9364f | 142 | void Compass::write2Bytes(char msb, char lsb) |
| terryLAI | 4:a377ecb9364f | 143 | { |
| terryLAI | 4:a377ecb9364f | 144 | printf("before writable...\r\n"); |
| terryLAI | 4:a377ecb9364f | 145 | if(_compassSerial->writeable()) |
| terryLAI | 4:a377ecb9364f | 146 | { |
| terryLAI | 4:a377ecb9364f | 147 | this->_compassSerial->putc(msb); |
| terryLAI | 4:a377ecb9364f | 148 | } |
| terryLAI | 4:a377ecb9364f | 149 | printf("_MSB = %x wrote...\r\n", msb); |
| terryLAI | 4:a377ecb9364f | 150 | if(_compassSerial->writeable()) |
| terryLAI | 4:a377ecb9364f | 151 | { |
| terryLAI | 4:a377ecb9364f | 152 | this->_compassSerial->putc(lsb); |
| terryLAI | 4:a377ecb9364f | 153 | } |
| terryLAI | 4:a377ecb9364f | 154 | printf("_LSB = %x wrote...\r\n", lsb); |
| terryLAI | 4:a377ecb9364f | 155 | wait(0.1); |
| terryLAI | 4:a377ecb9364f | 156 | } |
