Pages: [1]   Go Down
Author Topic: Beginning Android ADK with Arduino (M Bohmer) example question  (Read 670 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am puzzled by the second and third of the three constant definitions at the beginning of the HelloWorld sketch in chap 2, listing 2.7 of "Beginning Android ADK with Arduino" by Mario Bohmer which are Hex, for some reason, while the first is decimal. Why?

In the book's text he writes about messages being 3-byte long byte arrays e.g., command - target - value, but confusingly has a diagram with, for instance, the command byte labelled with 0xF ???! However, I understand one byte = 8 bits, 0-255 but 0xF is Hex for 15???

Sorry I am thick - please explain. No way to contact author



Code:
#include <Max3421e.h>
#include <Usb.h>
#include <AndroidAccessory.h>

#define ARRAY_SIZE 25
#define COMMAND_TEXT 0xF
#define TARGET_DEFAULT 0xF

AndroidAccessory acc("Manufacturer",
     "Model",
     "Description",
     "Version",
     "URI",
     "Serial");

char hello[ARRAY_SIZE] = {'H','e','l','l','o',' ',
'W','o','r','l','d',' ', 'f', 'r', 'o', 'm', ' ',
'A', 'r', 'd', 'u', 'i', 'n', 'o', '!'};

byte rcvmsg[255];
byte sntmsg[3 + ARRAY_SIZE];

void setup() {
  Serial.begin(115200);
  acc.powerOn();
}

void loop() {
  if (acc.isConnected()) {
    //read the sent text message into the byte array
    int len = acc.read(rcvmsg, sizeof(rcvmsg), 1);
    if (len > 0) {
      if (rcvmsg[0] == COMMAND_TEXT) {
        if (rcvmsg[1] == TARGET_DEFAULT){
          //get the textLength from the checksum byte
  byte textLength = rcvmsg[2];
          int textEndIndex = 3 + textLength;
          //print each character to the serial output
          for(int x = 3; x < textEndIndex; x++) {
            Serial.print((char)rcvmsg[x]);
            delay(250);
          }
          Serial.println();
          delay(250);
        }
      }
    }
   
    sntmsg[0] = COMMAND_TEXT;
    sntmsg[1] = TARGET_DEFAULT;
    sntmsg[2] = ARRAY_SIZE;
    for(int x = 0; x < ARRAY_SIZE; x++) {
      sntmsg[3 + x] = hello[x];
    }
    acc.write(sntmsg, 3 + ARRAY_SIZE);
    delay(250);
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 616
Posts: 49444
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Why?
The value stored in memory will be binary. It doesn't matter whether the constant used in the source file is decimal, octal, hex, or binary.

Some places where the string is used make more sense in decimal. Some in binary. Some in hex.

In this sketch, the first value represents array sizes, so decimal makes more sense, since most people have 10 fingers.

The other two values are stuff that the Android app is supposed to send. The documentation probable specified the value in hex, so this program represented the value in hex.

If the documentation said that the program was sending a 0xF, and this program looked for 15, that would be confusing, wouldn't it? Even though they are the same value.
Logged

Helsinki
Offline Offline
Newbie
*
Karma: 0
Posts: 31
A librarydude
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I would like to ask how do you feel about that book, Mario Böhmer: Beginning Android ADK with Arduino ( APress, 2012, ISBN 978-1430241973) in general? I'm starting Android+Arduino tinkering and some help, like a book would be very welcome.

On Amazon a reviewer says some of the example code won't run on new Android :-P Also a few reviewers say that code examples are hard to follow because important stuff has been omitted, and maybe the Amazon Kindle ebook version is technically broken.
Logged

Pages: [1]   Go Up
Jump to: