Question

I am trying to use the "Redbear BLE" shield with an "Arduino Uno" and get the analog input data logged to an iOS app so that the data input changes the background color.

I am using the iOS BLE simple controls example "Here". I am new to objective C and struggling with the correct code in terms of calling the correct NSLog and the variables for self.view.backgroundcolor

This is area I believe I want to add my backgroundcolor change :

       // When data is comming, this will be called
    -(void) bleDidReceiveData:(unsigned char *)data length:(int)length

{
    NSLog(@"Length: %d", length);

    // parse data, all commands are in 3-byte
    for (int i = 0; i < length; i+=3)
    {
        NSLog(@"0x%02X, 0x%02X, 0x%02X", data[i], data[i+1], data[i+2]);

        if (data[i] == 0x0A)
        {
            if (data[i+1] == 0x01)
                swDigitalIn.on = true;
            else
                swDigitalIn.on = false;
        }
        else if (data[i] == 0x0B)
        {
            UInt16 Value;

            Value = data[i+2] | data[i+1] << 8;
            lblAnalogIn.text = [NSString stringWithFormat:@"%d", Value];

        }        
    }
}

This is what i have been trying but I don't think i am on the correct lines :

  NSData *data = [[NSData alloc] initWithBytes:buf length:3];
    if (swAnalogIn > 0.0) {
        NSLog(@"0");
        self.view.backgroundColor = [UIColor *data];
        previousValue = currentValue;
        currentValue = 0;
    }
[ble write:data]; 

or this.

NSData *data = [[NSData alloc] UIColor colorWithRed:buf.floatValue / 1024.0f
                           green:buf.floatValue / 1024.0f
                            blue:buf.floatValue / 1024.0f
                           alpha:1.0f];
     }
(UIColor *data) {
    self.view.backgroundColor = data;

};
[ble write:data];
}

Any help or pointing in the right direction would be greatly received thanks.

Was it helpful?

Solution

I think you have two problems. One is easy - how to create a colour and set it as your background, but this depends on solving the first, trickier, problem, how to map your analog value to a colour.

This code -

else if (data[i] == 0x0B) {
            UInt16 Value;

            Value = data[i+2] | data[i+1] << 8;
}

gets you the analog value as a 16 bit unsigned integer in Value. Colors are typically created using a 8 bit triple (either RGB or HSB) plus a alpha or transparency value (but lets ignore that) - so you have to decide how to map a 16 bit value onto a 24 bit value. The best way to do this depends on exactly what you are trying to achieve and what the range of your input is (i.e. depending on your analog input source you may not get the full range from 0-65535).

One simple method is to map the high 8 bits to Hue (colour) and the low 8 bits to saturation (intensity) with a fixed brightness. To add a bit of complexity the UIColor initialisation methods take floats in the range 0-1, so we need to divide 255 to scale the result

CGFloat hue=(Value >>8)/255.0;

CGFloat sat=(Value & 0xff)/255.0;

CGFloat bright=0.5;

UIColor *backGroundColor=[UIColor colorWithHue:hue saturation:sat brightness:bright alpha:1.0];

//  Finally - set the background colour

self.view.backgroundColor=backgroundColor;

Note, that you already have the two bytes separately in data[i+1] and data[i+2] so you can save the bit masking & shifting and just use -

    else if (data[i] == 0x0B)
    {
       CGFloat hue=data[i+1]/255.0;
       CGFloat saturation=data[i+2]/255.0;

       self.view.backgroundColor=[UIColor colorWithHue:hue saturation:saturation brightness:0.5 alpha:1.0];

     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top