11 years, 3 months ago.

getting desperate

I can't get the following to work. Can anyone make some recommendations? One person recommended removing the parenthesis in the line " uart.attach(&uart_interrupt(), Serial::RxIrq); '" but all this does is ignore the function.

The original code was recommended by Christian.

#include "mbed.h"

char buffer[8]; //??tells the buffer to size for an 8 byte word
char function1[] = {0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xA1, 0xC2, 0x00}; // Stop
char function2[] = {0xF9, 0xFF, 0xBF, 0xFF, 0xFF, 0xA1, 0x42, 0x00}; //Zoom In
char function3[] = {0xF9, 0xFF, 0x7F, 0xFF, 0xFF, 0xA1, 0xC2, 0x00}; //Zoom Out
char function4[] = {0xF9, 0xFF, 0xE8, 0x99, 0xDF, 0xA1, 0x66, 0x00}; //Pan Right


Serial pc(USBTX, USBRX); //Not sure if this is needed
Serial uart(p9, p10); //assign uart to pin 9,10


DigitalOut led1(LED1); //Zoom In active
DigitalOut led2(LED2); //Zoom Out active
DigitalOut led3(LED3); //Pan Right active
DigitalOut led4(LED4); //

int i = 0; //interupt for variable i. Is this in the right location? Most examples show this after int main()

void uart_interrupt() //
{

    if(uart.readable()) {         // check availability
        buffer[i] = uart.getc();   // copy the uart input to buffer
        i++;                         // increment buffer value
    }
}


int main() //main program
{
    uart.baud(2400); //set Uart baud rate
    uart.attach(&uart_interrupt(), Serial::RxIrq); // at this point error "expression must be an Ivalue or function designator (error 158)
    while(true) { //???when a request fills the buffer???
        if(!memcmp(buffer, function2, 8)) { //led1 lit if Zoom In active
            led1 = 1;
            i = 0;                       // doing this prevents buffer overflow, i.e. you reset i, to point to buffer[0] again.
        }
        if(!memcmp(buffer, function3, 8)) { //led2 lit if Zoom Out active
            led2 = 1;
            i = 0;
        }
        if(!memcmp(buffer, function4, 8)) { //led 3 lit if Pan Right is active
            led3 = 1;
            i = 0;
        }
        if(!memcmp(buffer, function1, 8)) { //Stop turns off all other functions
            led1 = 0;
            led2 = 0;
            led3 = 0;
            i = 0;
        }
    }
}

1 Answer

11 years, 3 months ago.

You need to remove those parenthesis, otherwise it simply will never compile. It doesnt ignore the function, it makes it work properly.

Anyway I removed them, and then your code works fine. See the code:

#include "mbed.h"
 
char buffer[8]; //??tells the buffer to size for an 8 byte word
char function1[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; // Stop
char function2[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i'}; //Zoom In
char function3[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'j'}; //Zoom Out
char function4[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'k'}; //Pan Right
 
 
Serial pc(USBTX, USBRX); //Not sure if this is needed
Serial uart(USBTX, USBRX); //assign uart to pin 9,10
 
 
DigitalOut led1(LED1); //Zoom In active
DigitalOut led2(LED2); //Zoom Out active
DigitalOut led3(LED3); //Pan Right active
DigitalOut led4(LED4); //
 
int i = 0; //interupt for variable i. Is this in the right location? Most examples show this after int main()
 
void uart_interrupt() //
{
    if (i>7)
        error("Didnt find match");
    if(uart.readable()) {         // check availability
        buffer[i] = uart.getc();   // copy the uart input to buffer
        i++;                         // increment buffer value
    }
}

void clearBuffer() {
    for (int i = 0; i<8; i++)
        buffer[i] = 0;
}
 
 
int main() //main program
{
    clearBuffer();
    uart.baud(9600); //set Uart baud rate
    uart.attach(&uart_interrupt, Serial::RxIrq); // at this point error "expression must be an Ivalue or function designator (error 158)
    while(true) { //???when a request fills the buffer???
        if(!memcmp(buffer, function2, 8)) { //led1 lit if Zoom In active
            led1 = 1;
            i = 0;                       // doing this prevents buffer overflow, i.e. you reset i, to point to buffer[0] again.
            clearBuffer();
        }
        if(!memcmp(buffer, function3, 8)) { //led2 lit if Zoom Out active
            led2 = 1;
            i = 0;
            clearBuffer();
        }
        if(!memcmp(buffer, function4, 8)) { //led 3 lit if Pan Right is active
            led3 = 1;
            i = 0;
            clearBuffer();
        }
        if(!memcmp(buffer, function1, 8)) { //Stop turns off all other functions
            led1 = 0;
            led2 = 0;
            led3 = 0;
            i = 0;
            clearBuffer();
        }
    }
}

Some notes: I did change some stuff: baudrate, pins of uart serial and the required sequence, so I could easily test it myself. Aditionally I added the clearbuffer function, right now once it has found a match, it will keep calling the functions, so I made sure it only calls them once. Finally if it receives a wrong sequency it will result in a bufferoverflow since the counter is never reset, so I added an error condition for that, obviously you want something nicer for a real program.

Accepted Answer