libraries / Bridge / src / Mailbox.cppon commit Added link to project report (97a3ba0)
   1/*
   2  Copyright (c) 2013 Arduino LLC. All right reserved.
   3
   4  This library is free software; you can redistribute it and/or
   5  modify it under the terms of the GNU Lesser General Public
   6  License as published by the Free Software Foundation; either
   7  version 2.1 of the License, or (at your option) any later version.
   8
   9  This library is distributed in the hope that it will be useful,
  10  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12  Lesser General Public License for more details.
  13
  14  You should have received a copy of the GNU Lesser General Public
  15  License along with this library; if not, write to the Free Software
  16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17*/
  18
  19#include <Mailbox.h>
  20
  21unsigned int MailboxClass::readMessage(uint8_t *buff, unsigned int size) {
  22  uint8_t tmp[] = { 'm' };
  23  return bridge.transfer(tmp, 1, buff, size);
  24}
  25
  26void MailboxClass::readMessage(String &str, unsigned int maxLength) {
  27  uint8_t tmp[] = { 'm' };
  28  // XXX: Is there a better way to create the string?
  29  uint8_t buff[maxLength + 1];
  30  int l = bridge.transfer(tmp, 1, buff, maxLength);
  31  buff[l] = 0;
  32  str = (const char *)buff;
  33}
  34
  35void MailboxClass::writeMessage(const uint8_t *buff, unsigned int size) {
  36  uint8_t cmd[] = {'M'};
  37  bridge.transfer(cmd, 1, buff, size, NULL, 0);
  38}
  39
  40void MailboxClass::writeMessage(const String& str) {
  41  writeMessage((uint8_t*) str.c_str(), str.length());
  42}
  43
  44void MailboxClass::writeJSON(const String& str) {
  45  uint8_t cmd[] = {'J'};
  46  bridge.transfer(cmd, 1, (uint8_t*) str.c_str(), str.length(), NULL, 0);
  47}
  48
  49unsigned int MailboxClass::messageAvailable() {
  50  uint8_t tmp[] = {'n'};
  51  uint8_t res[2];
  52  bridge.transfer(tmp, 1, res, 2);
  53  return (res[0] << 8) + res[1];
  54}
  55
  56MailboxClass Mailbox(Bridge);