Hover!( Microchip MGC3130 ) library. Now, it is development version. http://www.hoverlabs.co/#hover https://www.switch-science.com/catalog/2124/
MjHoverBase.cpp@5:bd26aba87b9b, 2015-04-03 (annotated)
- Committer:
- matsujirushi
- Date:
- Fri Apr 03 12:51:03 2015 +0000
- Revision:
- 5:bd26aba87b9b
- Parent:
- 3:b03009537d66
- Child:
- 6:8d68ac1bab97
MjHover derived MjHoverBase.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
matsujirushi | 3:b03009537d66 | 1 | #include "MjHover.h" |
matsujirushi | 3:b03009537d66 | 2 | #include <vector> |
matsujirushi | 3:b03009537d66 | 3 | using namespace std; |
matsujirushi | 3:b03009537d66 | 4 | |
matsujirushi | 3:b03009537d66 | 5 | namespace matsujirushi { |
matsujirushi | 3:b03009537d66 | 6 | |
matsujirushi | 3:b03009537d66 | 7 | MjHoverBase::MjHoverBase(I2C* i2c, uint8_t address, DigitalInOut* ts_n, DigitalInOut* reset_n) |
matsujirushi | 3:b03009537d66 | 8 | { |
matsujirushi | 3:b03009537d66 | 9 | this->i2c = i2c; |
matsujirushi | 3:b03009537d66 | 10 | this->address = address & 0xfe; |
matsujirushi | 3:b03009537d66 | 11 | this->ts_n = ts_n; |
matsujirushi | 3:b03009537d66 | 12 | this->reset_n = reset_n; |
matsujirushi | 3:b03009537d66 | 13 | } |
matsujirushi | 3:b03009537d66 | 14 | |
matsujirushi | 3:b03009537d66 | 15 | void MjHoverBase::Reset() |
matsujirushi | 3:b03009537d66 | 16 | { |
matsujirushi | 3:b03009537d66 | 17 | this->ts_n->input(); |
matsujirushi | 3:b03009537d66 | 18 | this->reset_n->output(); |
matsujirushi | 3:b03009537d66 | 19 | this->reset_n->write(0); |
matsujirushi | 5:bd26aba87b9b | 20 | wait_us(100); |
matsujirushi | 3:b03009537d66 | 21 | this->reset_n->input(); |
matsujirushi | 5:bd26aba87b9b | 22 | wait_ms(1000); |
matsujirushi | 3:b03009537d66 | 23 | } |
matsujirushi | 3:b03009537d66 | 24 | |
matsujirushi | 3:b03009537d66 | 25 | bool MjHoverBase::WriteMessage(uint8_t id, const uint8_t* payload, uint8_t payloadSize) |
matsujirushi | 3:b03009537d66 | 26 | { |
matsujirushi | 3:b03009537d66 | 27 | if (4 + payloadSize > 255) |
matsujirushi | 3:b03009537d66 | 28 | { |
matsujirushi | 3:b03009537d66 | 29 | return false; |
matsujirushi | 3:b03009537d66 | 30 | } |
matsujirushi | 3:b03009537d66 | 31 | |
matsujirushi | 3:b03009537d66 | 32 | vector<uint8_t> message(4 + payloadSize); |
matsujirushi | 3:b03009537d66 | 33 | message[0] = 4 + payloadSize; // Msg. Size |
matsujirushi | 3:b03009537d66 | 34 | message[1] = 0; // Flags |
matsujirushi | 3:b03009537d66 | 35 | message[2] = 0; // Seq. |
matsujirushi | 3:b03009537d66 | 36 | message[3] = id; // ID |
matsujirushi | 3:b03009537d66 | 37 | memcpy(&message[4], payload, payloadSize); |
matsujirushi | 3:b03009537d66 | 38 | |
matsujirushi | 3:b03009537d66 | 39 | if (this->i2c->write(this->address, (const char*)&message[0], message.size()) != 0) |
matsujirushi | 3:b03009537d66 | 40 | { |
matsujirushi | 3:b03009537d66 | 41 | return false; |
matsujirushi | 3:b03009537d66 | 42 | } |
matsujirushi | 3:b03009537d66 | 43 | |
matsujirushi | 3:b03009537d66 | 44 | return true; |
matsujirushi | 3:b03009537d66 | 45 | } |
matsujirushi | 3:b03009537d66 | 46 | |
matsujirushi | 3:b03009537d66 | 47 | bool MjHoverBase::ReadMessage(uint8_t* id, uint8_t* payload, uint8_t payloadSize, uint8_t* readPayloadSize) |
matsujirushi | 3:b03009537d66 | 48 | { |
matsujirushi | 3:b03009537d66 | 49 | if (!this->DataAvailable()) |
matsujirushi | 3:b03009537d66 | 50 | { |
matsujirushi | 3:b03009537d66 | 51 | return false; |
matsujirushi | 3:b03009537d66 | 52 | } |
matsujirushi | 3:b03009537d66 | 53 | |
matsujirushi | 5:bd26aba87b9b | 54 | this->BeginRead(); |
matsujirushi | 5:bd26aba87b9b | 55 | |
matsujirushi | 3:b03009537d66 | 56 | this->i2c->start(); |
matsujirushi | 3:b03009537d66 | 57 | |
matsujirushi | 3:b03009537d66 | 58 | this->i2c->write(this->address | 1); |
matsujirushi | 3:b03009537d66 | 59 | |
matsujirushi | 3:b03009537d66 | 60 | uint8_t messageSize = this->i2c->read(1); |
matsujirushi | 3:b03009537d66 | 61 | vector<uint8_t> message(messageSize); |
matsujirushi | 3:b03009537d66 | 62 | message[0] = messageSize; |
matsujirushi | 3:b03009537d66 | 63 | uint8_t* messagePtr = &message[1]; |
matsujirushi | 3:b03009537d66 | 64 | for (int i = 1; i < messageSize; i++) |
matsujirushi | 3:b03009537d66 | 65 | { |
matsujirushi | 3:b03009537d66 | 66 | *messagePtr++ = this->i2c->read(i < messageSize - 1 ? 1 : 0); |
matsujirushi | 3:b03009537d66 | 67 | } |
matsujirushi | 3:b03009537d66 | 68 | |
matsujirushi | 3:b03009537d66 | 69 | this->i2c->stop(); |
matsujirushi | 3:b03009537d66 | 70 | |
matsujirushi | 5:bd26aba87b9b | 71 | this->EndRead(); |
matsujirushi | 5:bd26aba87b9b | 72 | |
matsujirushi | 3:b03009537d66 | 73 | if (payloadSize + 4 < messageSize) |
matsujirushi | 3:b03009537d66 | 74 | { |
matsujirushi | 3:b03009537d66 | 75 | return false; |
matsujirushi | 3:b03009537d66 | 76 | } |
matsujirushi | 3:b03009537d66 | 77 | |
matsujirushi | 3:b03009537d66 | 78 | *id = message[3]; |
matsujirushi | 3:b03009537d66 | 79 | memcpy(payload, &message[4], messageSize - 4); |
matsujirushi | 3:b03009537d66 | 80 | *readPayloadSize = messageSize - 4; |
matsujirushi | 3:b03009537d66 | 81 | |
matsujirushi | 3:b03009537d66 | 82 | return true; |
matsujirushi | 3:b03009537d66 | 83 | } |
matsujirushi | 3:b03009537d66 | 84 | |
matsujirushi | 3:b03009537d66 | 85 | } // namespace matsujirushi |