
#import "Testing.h"

@implementation Testing

-(id)init
{
    self = [super init];
    if(self) {
        serialPort = [[NSString alloc] initWithString: @"/dev/tty.usbmodemfd121"];
    }
    
    return self;
}

-(void)runProgram
{
    int result;
    BOOL a = YES;
    while(a)
    {
        while(scanf(" %d", &result)) { //Make sure that there is a space in front
            NSString * b = [self toCorrectForm:result];
            if(b) {[self inputWithInt:b];}
        }
    }
}

-(NSString *) toCorrectForm: (int) number
{
    if(number == 0)                      {return @"000";}
    else if(number > 0  && number < 10)  {return [[NSString alloc] initWithFormat:@"00%d",number];}
    else if(number > 9  && number < 100) {return [[NSString alloc] initWithFormat:@"0%d",number];}
    else if(number > 99 && number < 176) {return [[NSString alloc]initWithFormat:@"%d",number];}
    else                                 {return nil;}

}

-(void) inputWithInt:(NSString*)toBeSent
{
    NSString * str = [NSString stringWithFormat:@"echo %@ > %@", toBeSent, serialPort];
    char * toSend  = [str UTF8String]; 
    
    FILE * sender = popen(toSend, "r");
    pclose(sender);
}

@end
