-1

I have to create an unsigned int array to make an IR remote message. To do this, I have to concatenate the header to the data bits, depending of which key I want to mimic. Here's what I have and the problem:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const unsigned HEADER[34] = {4608, 4398, 655, 1588, 655, 1588, 655, 1588, 655, 472, 655, 472, 655, 472, 655, 472, 655, 472, 655, 1588, 655, 1588, 655, 1588, 655, 472, 655, 472, 655, 472, 655, 472, 655, 472};

unsigned short BTN_VOL_UP = 0xE01F;

void intToBin(unsigned short a, char* buffer) {
    buffer += 15;
    int i = 15;
    for (i; i >= 0; i--) {
        *buffer-- = (a & 1) + '0';
        a >>= 1;
    }
}

void hexToData(unsigned** data, int code) {
  char* strHex = (char*) malloc(16 * sizeof(char));
  intToBin(code, strHex);
  int i = 0;
  for(i; i < 16; i++) {
    char c = strHex[i];
    *(*data)++ = 655;
    if (c == '1') {
      *(*data)++ = 1588;
    } else {
      *(*data)++ = 472;
    }
  }
  (*data) -= 32;
}

void getCode(unsigned** data, short code) {
  int i = 0;
  for (i; i < 34; i++) {
    *(*data)++ = HEADER[i];
  }
  unsigned* bits = (unsigned*) malloc(32 * sizeof(unsigned));
  hexToData(&bits, code);
  i = 0;
  for (i; i < 32; i++) {
    *(*data)++ = bits[i];
  }
  *(*data)++ = 647;
  *(*data)++ = 0;
  (*data) -= 68;
}

And here's the code calling all this:

unsigned* data = (unsigned*) calloc(68, sizeof(unsigned));
getCode(&data, BTN_VOL_UP);

After that, I print the HEADER (using a for to print each value) and the data variable, but that's what I get:

4608 4398 655 1588 655 1588 655 1588 655 472 655 472 655 472 655 472 655 472 655 1588 655 1588 655 1588 655 472 655 472 655 472 655 472 655 472

0 136 1588 655 1588 655 1588 655 472 655 472 655 472 655 472 655 55298 36609 55298 36609 55298 36609 55298 36609 55298 36609 55298 36609 55298 36609 55298 36609 655 655 1588 655 1588 655 1588 655 472 655 472 655 472 655 472 655 472 655 472 655 472 655 472 655 1588 655 1588 655 1588 655 1588 655 1588 647 0 16

The HEADER is correct, but the data is all messed up: the header in the data is completely wrong, the actually data part (the VOL_UP code) is right, but there's an extra value on the end that shouldn't be there: 16. the array should end with ...647, 0}.

Any ideas what's wrong with the code?

EDIT: I found the problem, edited the code to include corrections. Turns out that I was having problem finding the exact location to copy the values, so I decided to manually copy int by int and subtract the pointer at the end. Ugly, but it works.

Rodrigo Castro
  • 1,573
  • 14
  • 38
  • 1
    Instead of ask here, why not just debug it? – Sam Liao Feb 24 '12 at 02:19
  • Because I'm doing this on Arduino, and I don't think I can debug that easily – Rodrigo Castro Feb 24 '12 at 02:24
  • 2
    You can either debug it under windows or linux, it is not device dependent program. – Sam Liao Feb 24 '12 at 02:27
  • You've got an 'off-by-one' error - in this case, maybe a `<=` where you need just `<`. – Jonathan Leffler Feb 24 '12 at 02:32
  • 1
    arsane is right. Nothing in this code is specific to arduino, or any other platform. Throw this on your favorite desktop C compiler, printf it, debug it, step through it. The question "any ideas whats wrong with the code?" is NOT a real question. Before posting here, describe what you've tried, why it failed, and ask a specific question. – abelenky Feb 24 '12 at 02:36

1 Answers1

2

Your code produces following...

4608 4398 655 1588 655 1588 655 1588 655 472 655 472 655 472 655 472 655 472 655 1588 655 1588 655 1588 655 472 655 472 655 472 655 472 655 472 1588 1588 1588 655 655 655 655 655 655 655 655 1588 1588 1588 1588 1588 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 647 0

Seems header is ok. I don't know algorithm for key generation, so I can't tell you if values 34-49 are ok, but you definitely have problem in 50-65 range. Your code never sets them to any value. Usually malloc doesn't initalize memory (it depends on standard library implementation tho), so you will get garbage in 50-65.

  • Thank you, I wasn't getting these results here, but you remembered me that malloc doesn't inicializate anything, so I switched to calloc and found the problem. I don't know exactly what it is compared to the code I wrote here, because I modified it so much I lost track of it. – Rodrigo Castro Feb 24 '12 at 03:05