//
//  sample1ViewController.m
//  sample1
//
//  Created by Akira Ujio on 09/10/30.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "sample1ViewController.h"

@implementation sample1ViewController

@synthesize btn_1,btn_2,btn_3,btn_act_tsk;
@synthesize btn_can_act,btn_ter_tsk,btn_chg_priH,btn_chg_priM;
@synthesize btn_chg_priL;
@synthesize btn_get_pri;
@synthesize btn_wup_tsk;
@synthesize btn_can_wup;
@synthesize btn_rel_wai;
@synthesize btn_sus_tsk;
@synthesize btn_rsm_tsk;
@synthesize btn_frsm_tsk;
@synthesize btn_ras_tex1;
@synthesize btn_ras_tex2;
@synthesize btn_ret_rdq;
@synthesize btn_sta_cyc;
@synthesize btn_stp_cyc;
@synthesize btn_vxget_tim;
@synthesize btn_vmsk_logl;
@synthesize btn_vmsk_logN;
@synthesize btn_ext_tsk;
@synthesize btn_slp_tsk;
@synthesize btn_tslp_tsk;
@synthesize btn_dly_tsk;
@synthesize btn_dis_tex;
@synthesize btn_ena_tex;
@synthesize btn_excep;
@synthesize btn_excepL;
@synthesize btn_QUIT;


void send_cmd_to_itron(char c);

typedef struct { const char *btn_text; char signal; } cmd_t;

static const cmd_t cmds[] = {
{	"TASK1",		'1'		},
{	"TASK2",		'2'		},
{	"TASK3",		'3'		},
{	"act_tsk",		'a'		},
{	"can_act",		'A'		},
{	"ter_tsk",		't'		},
{	"chg_priH",		'>'		},
{	"chg_priM",		'='		},
{	"chg_priL",		'<'		},
{	"get_pri",		'G'		},
{	"wup_tsk",		'w'		},
{	"can_wup",		'W'		},
{	"rel_wai",		'l'		},
{	"sus_tsk",		'u'		},
{	"rsm_tsk",		'm'		},
{	"frsm_tsk",		'M'		},
{	"ras_tex1",		'x'		},
{	"ras_tex2",		'X'		},
{	"rot_rdq",		'r'		},
{	"sta_cyc",		'c'		},
{	"stp_cyc",		'C'		},
{	"vxget_tim",	'V'		},
{	"vmsk_logI",	'v'		},
{	"vmsk_logN",	'q'		},
{	"ext_tsk",		'e'		},
{	"slp_tsk",		's'		},
{	"tslp_tsk",		'S'		},
{	"dly_tsk",		'd'		},
{	"dis_tex",		'y'		},
{	"ena_tex",		'Y'		},
{	"excep",		'z'		},
{	"excepL",		'Z'		},
{	"QUIT",			'Q'		},
};

- (IBAction) btn_push:(id) sender {
	UIButton *obj = (UIButton*)sender;
	char cmd = 0;
	unichar str_[32];
	char str[32];
	[[obj currentTitle] getCharacters:str_ ];
	
	// unichar -> char
	for(int i=0;i<sizeof(str)/sizeof(str[0]);++i) 
	{
		char c = (char)str_[i];
		if( str_[i] > 0x7F ) { str[i] = '\0'; break; }
		str[i] = c;
	}
	
	for(int i=0;i<sizeof(cmds)/sizeof(cmds[0]);++i)
	{
		if( strcmp(cmds[i].btn_text, str )  == 0 )
		{
			cmd = cmds[i].signal;
			break;
		}
	}
	
	//NSLog(@"%s: %d",str, cmd);
	
	if( cmd ) send_cmd_to_itron(cmd);
}


/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[btn_1 release];
	[btn_2 release];
	[btn_3 release];
	[btn_act_tsk release];
	[btn_can_act release];
	[btn_ter_tsk release];
	[btn_chg_priH release];
	[btn_chg_priM release];
	[btn_chg_priL release];
	[btn_get_pri release];
	[btn_wup_tsk release];
	[btn_can_wup release];
	[btn_rel_wai release];
	[btn_sus_tsk release];
	[btn_rsm_tsk release];
	[btn_frsm_tsk release];
	[btn_ras_tex1 release];
	[btn_ras_tex2 release];
	[btn_ret_rdq release];
	[btn_sta_cyc release];
	[btn_stp_cyc release];
	[btn_vxget_tim release];
	[btn_vmsk_logl release];
	[btn_vmsk_logN release];
	[btn_ext_tsk release];
	[btn_slp_tsk release];
	[btn_tslp_tsk release];
	[btn_dly_tsk release];
	[btn_dis_tex release];
	[btn_ena_tex release];
	[btn_excep release];
	[btn_excepL release];
	[btn_QUIT release];
    [super dealloc];
}

@end
