Machine Cloud
MachineCloud.cpp@1:f5eafb42e9e4, 2013-08-15 (annotated)
- Committer:
- ddollar
- Date:
- Thu Aug 15 23:54:25 2013 +0000
- Revision:
- 1:f5eafb42e9e4
- Parent:
- 0:260d7796eb25
- Child:
- 2:3fb7c06efa1e
models
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ddollar | 0:260d7796eb25 | 1 | #include <string.h> |
ddollar | 0:260d7796eb25 | 2 | #include "MachineCloud.h" |
ddollar | 0:260d7796eb25 | 3 | |
ddollar | 1:f5eafb42e9e4 | 4 | MachineCloud::MachineCloud(char *model) { |
ddollar | 1:f5eafb42e9e4 | 5 | init(model, 0x1000); |
ddollar | 0:260d7796eb25 | 6 | } |
ddollar | 0:260d7796eb25 | 7 | |
ddollar | 1:f5eafb42e9e4 | 8 | MachineCloud::MachineCloud(char *model, uint16_t router) { |
ddollar | 1:f5eafb42e9e4 | 9 | init(model, router); |
ddollar | 0:260d7796eb25 | 10 | } |
ddollar | 0:260d7796eb25 | 11 | |
ddollar | 1:f5eafb42e9e4 | 12 | void MachineCloud::init(char *model, uint16_t router) { |
ddollar | 1:f5eafb42e9e4 | 13 | m_model = model; |
ddollar | 0:260d7796eb25 | 14 | m_router = router; |
ddollar | 0:260d7796eb25 | 15 | } |
ddollar | 0:260d7796eb25 | 16 | |
ddollar | 0:260d7796eb25 | 17 | int MachineCloud::connect() { |
ddollar | 0:260d7796eb25 | 18 | m_xbee = new XBee(p9, p10); |
ddollar | 1:f5eafb42e9e4 | 19 | send("model", m_model); |
ddollar | 0:260d7796eb25 | 20 | return 0; |
ddollar | 0:260d7796eb25 | 21 | } |
ddollar | 0:260d7796eb25 | 22 | |
ddollar | 0:260d7796eb25 | 23 | int MachineCloud::disconnect() { |
ddollar | 0:260d7796eb25 | 24 | m_xbee = NULL; |
ddollar | 0:260d7796eb25 | 25 | return 0; |
ddollar | 0:260d7796eb25 | 26 | } |
ddollar | 0:260d7796eb25 | 27 | |
ddollar | 0:260d7796eb25 | 28 | int MachineCloud::on_receive(MachineCloudCallback *function) { |
ddollar | 0:260d7796eb25 | 29 | m_receiverFunction = function; |
ddollar | 0:260d7796eb25 | 30 | m_receiverObject = NULL; |
ddollar | 0:260d7796eb25 | 31 | return 0; |
ddollar | 0:260d7796eb25 | 32 | } |
ddollar | 0:260d7796eb25 | 33 | |
ddollar | 0:260d7796eb25 | 34 | int MachineCloud::on_receive(MachineCloudReceiver *object) { |
ddollar | 0:260d7796eb25 | 35 | m_receiverFunction = NULL; |
ddollar | 0:260d7796eb25 | 36 | m_receiverObject = object; |
ddollar | 0:260d7796eb25 | 37 | return 0; |
ddollar | 0:260d7796eb25 | 38 | } |
ddollar | 0:260d7796eb25 | 39 | |
ddollar | 0:260d7796eb25 | 40 | int MachineCloud::send(char *key, char *value) { |
ddollar | 0:260d7796eb25 | 41 | m_lock.lock(); |
ddollar | 0:260d7796eb25 | 42 | |
ddollar | 0:260d7796eb25 | 43 | int size = strlen(key) + strlen(value) + 1; |
ddollar | 0:260d7796eb25 | 44 | char *message = (char *)malloc(size); |
ddollar | 0:260d7796eb25 | 45 | sprintf(message, "%s=%s", key, value); |
ddollar | 0:260d7796eb25 | 46 | |
ddollar | 0:260d7796eb25 | 47 | uint8_t payload[size]; |
ddollar | 0:260d7796eb25 | 48 | for (int i=0; i<size; i++) { |
ddollar | 0:260d7796eb25 | 49 | payload[i] = message[i]; |
ddollar | 0:260d7796eb25 | 50 | } |
ddollar | 0:260d7796eb25 | 51 | |
ddollar | 0:260d7796eb25 | 52 | free(message); |
ddollar | 0:260d7796eb25 | 53 | |
ddollar | 0:260d7796eb25 | 54 | TxStatusResponse txStatus = TxStatusResponse(); |
ddollar | 0:260d7796eb25 | 55 | Tx16Request tx = Tx16Request(m_router, payload, sizeof(payload)); |
ddollar | 0:260d7796eb25 | 56 | m_xbee->send(tx); |
ddollar | 0:260d7796eb25 | 57 | |
ddollar | 1:f5eafb42e9e4 | 58 | m_lock.unlock(); |
ddollar | 1:f5eafb42e9e4 | 59 | |
ddollar | 0:260d7796eb25 | 60 | return 0; |
ddollar | 0:260d7796eb25 | 61 | } |
ddollar | 0:260d7796eb25 | 62 | |
ddollar | 0:260d7796eb25 | 63 | void MachineCloud::receive(char *key, char *value) { |
ddollar | 0:260d7796eb25 | 64 | if (m_receiverFunction) (*m_receiverFunction)(key, value); |
ddollar | 0:260d7796eb25 | 65 | if (m_receiverObject) m_receiverObject->receive(key, value); |
ddollar | 0:260d7796eb25 | 66 | } |
ddollar | 0:260d7796eb25 | 67 | |
ddollar | 0:260d7796eb25 | 68 | Rx16Response rx16 = Rx16Response(); |
ddollar | 0:260d7796eb25 | 69 | |
ddollar | 0:260d7796eb25 | 70 | int MachineCloud::loop() { |
ddollar | 0:260d7796eb25 | 71 | m_xbee->readPacket(); |
ddollar | 0:260d7796eb25 | 72 | |
ddollar | 0:260d7796eb25 | 73 | if (m_xbee->getResponse().isAvailable()) { |
ddollar | 0:260d7796eb25 | 74 | switch (m_xbee->getResponse().getApiId()) { |
ddollar | 0:260d7796eb25 | 75 | case RX_16_RESPONSE: |
ddollar | 0:260d7796eb25 | 76 | Rx16Response rx16 = Rx16Response(); |
ddollar | 0:260d7796eb25 | 77 | m_xbee->getResponse().getRx16Response(rx16); |
ddollar | 0:260d7796eb25 | 78 | char *data = (char *)malloc(rx16.getDataLength() + 1); |
ddollar | 0:260d7796eb25 | 79 | for (int i=0; i<rx16.getDataLength(); i++) { |
ddollar | 0:260d7796eb25 | 80 | data[i] = rx16.getData(i); |
ddollar | 0:260d7796eb25 | 81 | } |
ddollar | 0:260d7796eb25 | 82 | data[rx16.getDataLength()] = 0; |
ddollar | 0:260d7796eb25 | 83 | char *key = strtok(data, "="); |
ddollar | 0:260d7796eb25 | 84 | char *val = strtok(NULL, "="); |
ddollar | 0:260d7796eb25 | 85 | receive(key, val); |
ddollar | 0:260d7796eb25 | 86 | free(data); |
ddollar | 0:260d7796eb25 | 87 | } |
ddollar | 0:260d7796eb25 | 88 | } |
ddollar | 0:260d7796eb25 | 89 | |
ddollar | 0:260d7796eb25 | 90 | |
ddollar | 0:260d7796eb25 | 91 | return 0; |
ddollar | 0:260d7796eb25 | 92 | } |