Question

I have several UIProgressViews that read the voltage of an external device attached to the iOS device. The view scales it from 0-5 volts and I wanted to know if there was a way to retrieve the current progress/voltage of the UIProgressView and compare it to a threshold that is input by a user. Once this threshold is surpassed; a method I have written should be called.

The problem isn't necessarily the comparison part or calling the method. I am having trouble retrieving a live update of what the current value of the progressview is.

Thanks for your time.

EDIT FOR PART II:

So the problem is I did not code this and it is code pertaining to a certain product called the Nanospark board. This board is an input/output control board for factory systems that is run by various iOS devices.

I cannot quite seem to figure out what the previous coder did within this class:

    //

//  AnalogVC.m

//  NanosparkController

//



#import "AnalogVC.h"

#import "AppDelegate.h"



@interface InputItem : NSObject

@property (weak,nonatomic) UISwitch *onSwitch;

@property (weak,nonatomic) UIProgressView *progressView;

@property (weak,nonatomic) UILabel *valueLabel;

@end



@implementation InputItem

AppDelegate *appDelegate;

+ (id)itemWithSwitch:(id)temp progress:(id)progress label:(id)label

{


    InputItem *item = [[InputItem alloc] init];

    item.onSwitch = temp;

    item.progressView = progress;

    item.valueLabel = label;

    return item;

}



- (void)setDisconnected

{


    self.onSwitch.on = NO;

    self.onSwitch.enabled = NO;

    self.valueLabel.text = @"0.000 v";

    self.progressView.progress = 0;



}



- (void)setOn

{



self.onSwitch.on = YES;

    self.onSwitch.enabled = YES;

    self.valueLabel.text = @"0.000 v";

    self.progressView.progress = 0;

[appDelegate watchPins:@"testing ON"];

}



- (void)setOff

{


    self.onSwitch.on = NO;

    self.onSwitch.enabled = YES;

    self.valueLabel.text = @"0.000 v";

    self.progressView.progress = 0;

[appDelegate watchPins:@"testing OFF"];

}



- (void)setValue:(double)value

{

    if (self.onSwitch.on)

    {

        self.valueLabel.text = [NSString stringWithFormat:@"%0.3f v",value];

        self.progressView.progress = value/5.0;

if(value > 0.8){

[appDelegate watchPins:@"testing VALUE"];

}

    }

}

@end



@interface AnalogVC ()

{


    NSArray *_inputItems;

    AppDelegate *appDelegate;

    NSMutableArray *channel0Values;



    UIColor *custom1;

    UIColor *custom2;

    UIColor *custom3;

    UIColor *custom4;

}



@property (nonatomic) NCBoardManager *manager;



@property (weak,nonatomic) IBOutlet UISwitch *inputSwitch0;

@property (weak,nonatomic) IBOutlet UISwitch *inputSwitch1;

@property (weak,nonatomic) IBOutlet UISwitch *inputSwitch2;

@property (weak,nonatomic) IBOutlet UISwitch *inputSwitch3;

@property (weak,nonatomic) IBOutlet UISwitch *inputSwitch4;

@property (weak,nonatomic) IBOutlet UISwitch *inputSwitch5;



@property (weak,nonatomic) IBOutlet UIProgressView *inputProgress0;

@property (weak,nonatomic) IBOutlet UIProgressView *inputProgress1;

@property (weak,nonatomic) IBOutlet UIProgressView *inputProgress2;

@property (weak,nonatomic) IBOutlet UIProgressView *inputProgress3;

@property (weak,nonatomic) IBOutlet UIProgressView *inputProgress4;

@property (weak,nonatomic) IBOutlet UIProgressView *inputProgress5;



@property (weak,nonatomic) IBOutlet UILabel *inputValue0;

@property (weak,nonatomic) IBOutlet UILabel *inputValue1;

@property (weak,nonatomic) IBOutlet UILabel *inputValue2;

@property (weak,nonatomic) IBOutlet UILabel *inputValue3;

@property (weak,nonatomic) IBOutlet UILabel *inputValue4;

@property (weak,nonatomic) IBOutlet UILabel *inputValue5;



@property (weak,nonatomic) IBOutlet UISlider *outputSlider0;

@property (weak,nonatomic) IBOutlet UISlider *outputSlider1;



@property (weak,nonatomic) IBOutlet UIStepper *outputStepper0;

@property (weak,nonatomic) IBOutlet UIStepper *outputStepper1;



@property (weak,nonatomic) IBOutlet UILabel *outputValue0;

@property (weak,nonatomic) IBOutlet UILabel *outputValue1;





- (IBAction)inputChannelChanged:(UISwitch *)sender;

- (IBAction)outputSliderMoved:(UISlider *)sender;

- (IBAction)outputSliderStopped:(UISlider *)sender;

- (IBAction)outputStepperChanged:(UIStepper *)sender;

@end



@implementation AnalogVC{}





//////////////////////////////

#pragma mark View Lifecycle

//////////////////////////////





- (void)viewDidLoad

{

    [super viewDidLoad];

        NSLog(@"Analog VC loaded");

    _inputItems = @[[InputItem itemWithSwitch:_inputSwitch0 progress:_inputProgress0 label:_inputValue0],

                    [InputItem itemWithSwitch:_inputSwitch1 progress:_inputProgress1 label:_inputValue1],

                    [InputItem itemWithSwitch:_inputSwitch2 progress:_inputProgress2 label:_inputValue2],

                    [InputItem itemWithSwitch:_inputSwitch3 progress:_inputProgress3 label:_inputValue3],

                    [InputItem itemWithSwitch:_inputSwitch4 progress:_inputProgress4 label:_inputValue4],

                    [InputItem itemWithSwitch:_inputSwitch5 progress:_inputProgress5 label:_inputValue5]];



    _manager = [NCBoardManager sharedBoardManager];



    __unsafe_unretained AnalogVC *vc = self;

    [_manager setAnalogInputHandling:dispatch_get_main_queue()

                              filter:^(NCAnalogInputs *inputs){ return YES; }

                             handler:^(NCAnalogInputs *inputs){ [vc setAnalogInputs:inputs]; }];





    //  Register for notifications

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(boardConnected:)

                                                 name:CONNECTED_NOTIFICATION

                                               object:nil];



    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(boardDisconnected:)

                                                 name:DISCONNECTED_NOTIFICATION

                                               object:nil];

}



- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    [self updateAnalogInputs];

    [self updateAnalogOutputs];



    custom1 = [UIColor whiteColor];

    custom2 = [UIColor darkGrayColor];

    custom3 = [UIColor blackColor];

    custom4 = [UIColor colorWithRed:.97 green:.97 blue:.588 alpha:1.0];



    CAGradientLayer *gradient = [CAGradientLayer layer];

    gradient.frame = self.view.bounds;

    gradient.colors = [NSArray arrayWithObjects:(id)[custom2 CGColor], (id)[custom1 CGColor], (id)[custom2 CGColor], nil];

    gradient.startPoint = CGPointMake(0.5, 0);

    gradient.endPoint = CGPointMake(0.5, 1.0);

    gradient.locations = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:1.0], nil];

    [self.view.layer insertSublayer:gradient atIndex:0];



    [self.inputSwitch0 setOnTintColor:custom4];

    [self.inputSwitch1 setOnTintColor:custom4];

    [self.inputSwitch2 setOnTintColor:custom4];

    [self.inputSwitch3 setOnTintColor:custom4];

    [self.inputSwitch4 setOnTintColor:custom4];

    [self.inputSwitch5 setOnTintColor:custom4];



    [self.inputSwitch0 setTintColor:custom3];

    [self.inputSwitch1 setTintColor:custom3];

    [self.inputSwitch2 setTintColor:custom3];

    [self.inputSwitch3 setTintColor:custom3];

    [self.inputSwitch4 setTintColor:custom3];

    [self.inputSwitch5 setTintColor:custom3];



    self.inputProgress0.trackTintColor = custom3;

    self.inputProgress1.trackTintColor = custom3;

    self.inputProgress2.trackTintColor = custom3;

    self.inputProgress3.trackTintColor = custom3;

    self.inputProgress4.trackTintColor = custom3;

    self.inputProgress5.trackTintColor = custom3;



    self.inputProgress0.progressTintColor = custom4;

    self.inputProgress1.progressTintColor = custom4;

    self.inputProgress2.progressTintColor = custom4;

    self.inputProgress3.progressTintColor = custom4;

    self.inputProgress4.progressTintColor = custom4;

    self.inputProgress5.progressTintColor = custom4;



    self.outputSlider0.minimumTrackTintColor = custom4;

    self.outputSlider1.minimumTrackTintColor = custom4;

    self.outputSlider0.maximumTrackTintColor = custom3;

    self.outputSlider1.maximumTrackTintColor = custom3;



    self.outputSlider0.thumbTintColor = custom3;

    self.outputSlider1.thumbTintColor = custom3;



    if(_manager.isBoardConnected)

    {

        self.outputStepper0.tintColor = custom4;

        self.outputStepper1.tintColor = custom4;

        self.outputStepper0.enabled = TRUE;

        self.outputStepper1.enabled = TRUE;

        self.outputSlider0.enabled = TRUE;

        self.outputSlider1.enabled = TRUE;

    }

    else

    {

        self.outputStepper0.tintColor = custom2;

        self.outputStepper1.tintColor = custom2;

        self.outputStepper0.enabled = FALSE;

        self.outputStepper1.enabled = FALSE;

        self.outputSlider0.enabled = FALSE;

        self.outputSlider1.enabled = FALSE;

    }

}





//////////////////////////////

#pragma mark Rotation Calls

//////////////////////////////





- (NSUInteger)supportedInterfaceOrientations

{

    return UIInterfaceOrientationPortrait;

}



- (BOOL)shouldAutorotate

{

    return FALSE;

}





//////////////////////////

#pragma mark Board Calls

//////////////////////////





- (void)boardConnected:(NSNotification *)notification

{

    [self updateAnalogInputs];

    [self updateAnalogOutputs];



    self.outputStepper0.enabled = TRUE;

    self.outputStepper1.enabled = TRUE;

    self.outputSlider0.enabled = TRUE;

    self.outputSlider1.enabled = TRUE;



    self.outputStepper0.tintColor = custom4;

    self.outputStepper1.tintColor = custom4;

}



- (void)boardDisconnected:(NSNotification *)notification

{

    [self updateAnalogInputs];

    [self updateAnalogOutputs];



    self.outputStepper0.enabled = FALSE;

    self.outputStepper1.enabled = FALSE;

    self.outputSlider0.enabled = FALSE;

    self.outputSlider1.enabled = FALSE;



    self.outputStepper0.tintColor = custom2;

    self.outputStepper1.tintColor = custom2;

}



- (void)updateAnalogInputs

{


    uint8_t channel = self.manager.analogInputChannels;

    switch (self.manager.analogInputStatus)

    {

        case NCInputConnected:

            //  Check if channels we left on

            if (channel) self.manager.analogInputChannels = 0;

            [_inputItems makeObjectsPerformSelector:@selector(setOff)];

            break;

        case NCInputDisconnected:

            [_inputItems makeObjectsPerformSelector:@selector(setDisconnected)];

            break;

        case NCInputLiveUpdating:

            for (InputItem *item in _inputItems)

            {

                //if (channel & 1) [item setOn];

                //else             [item setOff];

                channel >>= 1;

            }

            break;

        case NCInputSampling:

            [_inputItems makeObjectsPerformSelector:@selector(setDisconnected)];

            break;

        case NCInputTransfering:

            [_inputItems makeObjectsPerformSelector:@selector(setDisconnected)];

            break;

    }



}





- (void)setAnalogInputs:(NCAnalogInputs *)inputs

{


    int i = 0;

    uint8_t channels = inputs.channels;

    for (InputItem *item in _inputItems)

    {

        if (channels & 1)

        {

            [item setValue:[inputs valueForChannel:i]];


        }

        channels >>= 1;

        i++;

    }

}



- (void)updateAnalogOutputs

{

    BOOL connected = [self.manager isBoardConnected];



    self.outputSlider0.value = self.manager.analogOutput0;

    self.outputSlider0.enabled = connected;

    self.outputStepper0.value = self.outputSlider0.value * 1000;

    self.outputStepper0.enabled = connected;

    self.outputValue0.text = [NSString stringWithFormat:@"%0.3f v",self.outputSlider0.value];



    self.outputSlider1.value = self.manager.analogOutput1;

    self.outputSlider1.enabled = connected;

    self.outputStepper1.value = self.outputSlider1.value * 1000;

    self.outputStepper1.enabled = connected;

    self.outputValue1.text = [NSString stringWithFormat:@"%0.3f v",self.outputSlider1.value];

}





///////////////////////////////

#pragma mark IBAction Methods

///////////////////////////////



- (IBAction)inputChannelChanged:(UISwitch *)sender

{

NSLog(@"TEST");

InputItem *item = [_inputItems objectAtIndex:sender.tag];

    uint8_t channels = self.manager.analogInputChannels;

    if (sender.on)

    {

        channels  |= (1 << sender.tag);

        [item setOn];



    }

    else

    {

        channels &= ~(1 << sender.tag);

        [item setOff];

    }

    if (!self.manager.analogInputChannels) [self.manager startAnalogLiveUpdating];

    else if(!channels) [self.manager stopAnalogLiveUpdating];



    self.manager.analogInputChannels = channels;

}



- (IBAction)outputSliderMoved:(UISlider *)sender

{

    if (!sender.tag)

    {

        self.manager.analogOutput0 = sender.value;

        self.outputValue0.text = [NSString stringWithFormat:@"%0.3f v",sender.value];

    }

    else

    {

        self.manager.analogOutput1 = sender.value;

        self.outputValue1.text = [NSString stringWithFormat:@"%0.3f v",sender.value];

    }

}



- (IBAction)outputSliderStopped:(UISlider *)sender

{

    if (!sender.tag)

    {

        self.manager.analogOutput0 = sender.value;

        self.outputStepper0.value = round(sender.value * 1000.0);

        self.outputValue0.text = [NSString stringWithFormat:@"%0.3f v",self.outputStepper0.value/1000.0];

    }

    else

    {

        self.manager.analogOutput1 = sender.value;

        self.outputStepper1.value = round(sender.value * 1000.0);

        self.outputValue1.text = [NSString stringWithFormat:@"%0.3f v",self.outputStepper1.value/1000.0];

    }

}



- (IBAction)outputStepperChanged:(UIStepper *)sender

{

    float value = sender.value/1000.0;



    if (!sender.tag)

    {

        self.manager.analogOutput0 = value;

        self.outputSlider0.value = value;

        self.outputValue0.text = [NSString stringWithFormat:@"%0.3f v",value];

    }

    else

    {

        self.manager.analogOutput1 = sender.value/1000.0;

        self.outputSlider1.value = value;

        self.outputValue1.text = [NSString stringWithFormat:@"%0.3f v",value];

    }

}



@end

The problem I am having is I cannot figure out how to take values to and from the UIProgressViews that are on the storyboard (which is, like you said, a trivial concept). However with this set up it is rather convoluted.

Second problem is that; I am not sure if there is a way I can debug this as the application only runs when the external device (the nanospark controller board) is connected to the iPod.

The last but final problem I am having is that I am assuming the IBAction InputChannelChanged is being called (cannot debug this regularly with a breakpoint as aforementioned because it requires the external device to run the application), but when I run the application it does everything it should and the buttons react correctly to what the original software developer had coded.

This means that if I add my texting method to the IBAction (by adding [appDelegate watchPins@"TEST"]) it does not send the text to the user, but the buttons do still do what they should have done in concordance to the previous developers' aspirations.... this implies that the IBAction method is indeed being called... but then why isn't my text going through? I know that [appDelegate watchPins:@"TEST"]; should work as I have used it within several of his other classes.

Here is a screenshot displaying the UI of the AnalogVC.m:

http://i.stack.imgur.com/NNpZk.png

Do not feel obligated to answer all of these questions it's just I felt it necessary to provided all three for greater context of the problem. Thanks and sorry for the TL;DR.

EDIT 2: I'd upload the image but I do not have the required minimum reputation.

EDIT 3: I have tried to add another IBAction just to see if the buttons react to that; still nothing even if I copied the exact code from the other working classes.

Was it helpful?

Solution

Solution 1:

You need to call your method when you update the UIProgressView I assume that use are using the setProgress: animated: method somewhere in your code to update the progress view. If so, try this code in the method in which you update the UIProgressView:

float myVoltageFromDevice = self.deviceInput / 5.0; //this can be whatever your input is mapped to 0.0...1.0 

//the self.device input variable should be the input from the external device.
if(myFloatFromDevice > myThreshold){  //set my threshold to whatever you would like.
    [self doAction];  //do whatever you need to do when the value is surpassed
}else{    
    [myProgressView setProgress:myVoltageFromDevice animated: NO];
}

Edit: I commented in the code above //this can be whatever your input is mapped to 0.0...1.0. Just in case this isn't clear, to achieve mapping you would do:

float myVoltageFromDevice = self.deviceInput / 5.0; 

because the device input variable should be a value from 0-5 as you said in the OP. This makes the value from 0.0-1.0 which is the range of values that UIProgressView will accept.

Solution 2:

If can't pull off the above (which you really should be able to do), you should use Key Value Observing (KVO) which is detailed in this apple developer doc.

Edit 2:

The code you posted is quite complicated, but I believe that the method that you need to edit is - (void)setAnalogInputs:(NCAnalogInputs *)inputs try changing some of the code to this:

for (InputItem *item in _inputItems)

{

    if (channels & 1)

    {
        if([inputs valueForChannel:i] > myThreshold){
            [self doAction]; //Do your action here.
        }else{
            [item setValue:[inputs valueForChannel:i]];
        }
    }

    channels >>= 1;

    i++;

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