// File: /workspace/github_c_makefile_star2_O2/Openvario[P]sensord/sensorcal.c
// Function: main
int main (int argc, char **argv) {

	// local variables
	t_24c16 eeprom;
	t_eeprom_data data;

	int exit_code=0;
	int result;
	int c;
	int i;
	char sn[7];
	char zero[1]={0x00};


	// usage message
	const char* Usage = "\n"\
	"  -c              calibrate sensors\n"\
	"  -s [serial]     write serial number (6 characters)\n"\
    "  -i              initialize EEPROM. All values will be cleared !!!\n"\
	"\n";

	// disable buffering
	setbuf(stdout, NULL);

	// print banner
	printf("sensorcal V%c.%c RELEASE %c build: %s %s\n", VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE,  __DATE__, __TIME__);
	printf("sensorcal Copyright (C) 2014  see AUTHORS on www.openvario.org\n");
	printf("This program comes with ABSOLUTELY NO WARRANTY;\n");
	printf("This is free software, and you are welcome to redistribute it under certain conditions;\n");

	// open eeprom object
	result = eeprom_open(&eeprom, 0x50);
	if (result != 0)
	{
		printf("No EEPROM found !!\n");
		exit(1);
	}

	sn[6]=0;
	// check commandline arguments
	while ((c = getopt (argc, argv, "his:cde")) != -1)
	{
		switch (c) {
			case 'h':
				printf("Usage: sensorcal [OPTION]\n%s",Usage);
				break;

			case 'i':
				printf("Initialize EEPROM ...\n");
				for (i=0; i<128; i++)
				{
					result = eeprom_write(&eeprom, &zero[0], i, 1);
				}
				strcpy(data.header, "OV");
				data.data_version = EEPROM_DATA_VERSION;
				memset(data.serial,'0',6);
				data.zero_offset=0.0;
				update_checksum(&data);
				printf("Writing data to EEPROM ...\n");
				result = eeprom_write(&eeprom, (char*)&data, 0x00, sizeof(data));
				break;

			case 'c':
				// read actual EEPROM values
				printf("Reading EEPROM values ...\n\n");
				if( eeprom_read_data(&eeprom, &data) == 0)
				{
					calibrate_ams5915(&data);
					printf("New Offset: %f\n",(data.zero_offset));
					update_checksum(&data);
					printf("Writing data to EEPROM ...\n");
					result = eeprom_write(&eeprom, (char*)&data, 0x00, sizeof(data));
				}
				else
				{
					printf("EEPROM content not valid !!\n");
					printf("Please use -i to initialize EEPROM !!\n");
					exit_code=2;
					break;
				}
				break;
			case 'e':
				// delete complete EEPROM
				printf("Delete whole EEPROM ...\n\n");
				for (i=0; i< sizeof(data); i++)
				{
						result = eeprom_write(&eeprom, &zero[0], 0x00, 1);
				}
				printf("EEPROM cleared !!\n");
				exit_code=3;
				printf("End ...\n");
				exit(exit_code);
				break;
			case 'd':
				// read actual EEPROM values
				printf("Reading EEPROM values ...\n\n");
				if( eeprom_read_data(&eeprom, &data) == 0)
				{
				  memcpy(sn,data.serial,6);
					printf("Actual EEPROM values:\n");
					printf("---------------------\n");
					printf("Serial: \t\t\t%s\n", sn);
					printf("Differential pressure offset:\t%f\n",data.zero_offset);
				}
				else
				{
					printf("EEPROM content not valid !!\n");
					printf("Please use -i to initialize EEPROM !!\n");
					exit_code=2;
					break;
				}
				printf("End ...\n");
				exit(exit_code);
				break;

			case 's':
				if( strlen(optarg) == 6)
				{
					// read actual EEPROM values
					if( eeprom_read_data(&eeprom, &data) == 0)
					{
						for(i=0; i<6;i++)
						{
							sn[i]=data.serial[i]=*optarg;
							optarg++;
						}
						printf("New Serial number: %s\n",sn);
						update_checksum(&data);
						printf("Writing data to EEPROM ...\n");
						result = eeprom_write(&eeprom, (char*)&data, 0x00, sizeof(data));
					}
					else
					{
						printf("EEPROM content not valid !!\n");
						printf("Please use -i to initialize EEPROM !!\n");
						exit_code=2;
						break;
					}
				}
				else
				{
					printf("ERROR: Serialnumber has to have exactly 6 characters !!\n");
					exit_code=1;
					break;
				}
				break;

			case '?':
				printf("Unknow option %c\n", optopt);
				printf("Usage: sensorcal [OPTION]\n%s",Usage);
				printf("Exiting ...\n");
		}
	}

	printf("End ...\n");
	return(exit_code);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movb $0x0, 0x7(%rsp)
movq 0x2709(%rip), %rax      # 0x3fc8
movq (%rax), %rdi
xorl %esi, %esi
callq 0x1080
leaq 0x9e0(%rip), %rdi       # 0x22b0
leaq 0xa03(%rip), %r8        # 0x22da
leaq 0xa08(%rip), %r9        # 0x22e6
pushq $0x30
popq %rsi
pushq $0x31
popq %rcx
movl %ecx, %edx
xorl %eax, %eax
callq 0x1090
leaq 0xaab(%rip), %rdi       # 0x239f
callq 0x1040
leaq 0xade(%rip), %rdi       # 0x23de
callq 0x1040
leaq 0xb02(%rip), %rdi       # 0x240e
callq 0x1040
leaq 0x38(%rsp), %rdi
pushq $0x50
popq %rsi
callq 0x1449
testl %eax, %eax
jne 0x1d7b
pushq $0x1
popq %r15
leaq 0x24(%rsp), %rax
movb $0x0, 0x6(%rax)
leaq 0x38(%rsp), %r12
pushq $0x14
popq %r14
leaq 0x7(%rsp), %r13
movl $0x0, 0x8(%rsp)
movq %rbx, 0x30(%rsp)
movl %ebp, 0xc(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
leaq 0x991(%rip), %rdx       # 0x22ef
callq 0x10e0
leal -0x63(%rax), %ecx
cmpl $0x6, %ecx
ja 0x19d9
leaq 0x882(%rip), %rdx       # 0x21f4
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
leaq 0xb87(%rip), %rdi       # 0x2509
callq 0x1040
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x133c
testl %eax, %eax
jne 0x1adb
leaq 0x9de(%rip), %rdi       # 0x2381
xorl %eax, %eax
callq 0x1090
leaq 0x40(%rsp), %rbp
movq %rbp, %rdi
pushq $0x28
popq %rsi
callq 0x15fc
testl %eax, %eax
je 0x1bc6
leaq 0xbeb(%rip), %rdi       # 0x25b4
callq 0x1040
movss 0x1c(%rsp), %xmm0
jmp 0x1c60
cmpl $0x3f, %eax
je 0x1aff
cmpl $0x73, %eax
je 0x1ab5
cmpl $-0x1, %eax
jne 0x1952
jmp 0x1ca4
leaq 0xb09(%rip), %rdi       # 0x2509
callq 0x1040
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x133c
testl %eax, %eax
jne 0x1adb
jmp 0x1cc3
leaq 0xb40(%rip), %rdi       # 0x2566
callq 0x1040
xorl %ebx, %ebx
cmpl $0x80, %ebx
je 0x1a4a
movzbl %bl, %edx
movq %r12, %rdi
movq %r13, %rsi
movl %r15d, %ecx
callq 0x1567
incl %ebx
jmp 0x1a2d
movl $0x100564f, 0x10(%rsp)  # imm = 0x100564F
leaq 0x14(%rsp), %rax
movw $0x3030, 0x4(%rax)      # imm = 0x3030
movl $0x30303030, (%rax)     # imm = 0x30303030
andl $0x0, 0x1c(%rsp)
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1324
leaq 0xb00(%rip), %rdi       # 0x257c
callq 0x1040
movq %r12, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movl %r14d, %ecx
callq 0x1567
movq 0x30(%rsp), %rbx
jmp 0x1952
leaq 0x855(%rip), %rdi       # 0x22f7
leaq 0x767(%rip), %rsi       # 0x2210
xorl %eax, %eax
callq 0x1090
jmp 0x1952
movq 0x2514(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
callq 0x1070
cmpq $0x6, %rax
jne 0x1b3c
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x133c
testl %eax, %eax
je 0x1b52
leaq 0xa42(%rip), %rdi       # 0x2524
callq 0x1040
leaq 0xa52(%rip), %rdi       # 0x2540
callq 0x1040
pushq $0x2
popq %rax
movl %eax, 0x8(%rsp)
jmp 0x1952
movq 0x24da(%rip), %rax      # 0x3fe0
movl (%rax), %esi
leaq 0x860(%rip), %rdi       # 0x236f
xorl %eax, %eax
callq 0x1090
leaq 0x7da(%rip), %rdi       # 0x22f7
leaq 0x6ec(%rip), %rsi       # 0x2210
xorl %eax, %eax
callq 0x1090
leaq 0x934(%rip), %rdi       # 0x2466
callq 0x1040
jmp 0x1952
leaq 0x92f(%rip), %rdi       # 0x2472
callq 0x1040
movl %r15d, 0x8(%rsp)
jmp 0x1952
movq 0x2477(%rip), %rdi      # 0x3fd0
movq (%rdi), %rax
xorl %ecx, %ecx
cmpq $0x6, %rcx
je 0x1b81
leaq (%rax,%rcx), %rdx
incq %rdx
movb -0x1(%rdx), %sil
movb %sil, 0x14(%rsp,%rcx)
movb %sil, 0x24(%rsp,%rcx)
movq %rdx, (%rdi)
incq %rcx
jmp 0x1b5e
leaq 0x7d0(%rip), %rdi       # 0x2358
leaq 0x24(%rsp), %rsi
xorl %eax, %eax
callq 0x1090
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
callq 0x1324
leaq 0x9d4(%rip), %rdi       # 0x257c
callq 0x1040
movq %r12, %rdi
movq %rbp, %rsi
movl 0xc(%rsp), %ebp
xorl %edx, %edx
movl %r14d, %ecx
callq 0x1567
jmp 0x1952
leaq 0x9dd(%rip), %rdi       # 0x25aa
callq 0x1040
movss 0x60e(%rip), %xmm0      # 0x21e8
movlps %xmm0, 0x5c(%rsp)
movq %rbp, %rdi
callq 0x16c3
movl $0x1b774, %edi          # imm = 0x1B774
callq 0x1130
callq 0x123c
xorps %xmm0, %xmm0
movl $0x320, %ebx            # imm = 0x320
subl $0x1, %ebx
jb 0x1c4d
movss %xmm0, 0x2c(%rsp)
movss 0x5db(%rip), %xmm0      # 0x21ec
callq 0x124b
movq %rbp, %rdi
callq 0x171b
callq 0x123c
movq %rbp, %rdi
callq 0x1808
cvtss2sd 0x58(%rsp), %xmm0
leaq 0x759(%rip), %rdi       # 0x2391
movb $0x1, %al
callq 0x1090
movss 0x2c(%rsp), %xmm0
addss 0x58(%rsp), %xmm0
jmp 0x1bfe
divss 0x59b(%rip), %xmm0      # 0x21f0
movss %xmm0, 0x1c(%rsp)
movq 0x30(%rsp), %rbx
cvtss2sd %xmm0, %xmm0
leaq 0x6ac(%rip), %rdi       # 0x2317
movb $0x1, %al
callq 0x1090
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
callq 0x1324
leaq 0x8f6(%rip), %rdi       # 0x257c
callq 0x1040
movq %r12, %rdi
movq %rbp, %rsi
xorl %edx, %edx
movl %r14d, %ecx
callq 0x1567
movl 0xc(%rsp), %ebp
jmp 0x1952
leaq 0x856(%rip), %rdi       # 0x2501
callq 0x1040
movl 0x8(%rsp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14(%rsp), %rcx
movzwl 0x4(%rcx), %eax
leaq 0x24(%rsp), %rbx
movw %ax, 0x4(%rbx)
movl (%rcx), %eax
movl %eax, (%rbx)
leaq 0x7ca(%rip), %rdi       # 0x24aa
callq 0x1040
leaq 0x7d4(%rip), %rdi       # 0x24c0
callq 0x1040
leaq 0x62f(%rip), %rdi       # 0x2327
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
cvtss2sd 0x1c(%rsp), %xmm0
leaq 0x627(%rip), %rdi       # 0x2336
movb $0x1, %al
callq 0x1090
leaq 0x7e4(%rip), %rdi       # 0x2501
callq 0x1040
movl 0x8(%rsp), %edi
callq 0x10f0
leaq 0x7a4(%rip), %rdi       # 0x24d6
callq 0x1040
leaq 0x38(%rsp), %rbx
leaq 0x7(%rsp), %r15
pushq $0x1
popq %rbp
subl $0x1, %r14d
jb 0x1d5b
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
movl %ebp, %ecx
callq 0x1567
jmp 0x1d44
leaq 0x78d(%rip), %rdi       # 0x24ef
callq 0x1040
leaq 0x793(%rip), %rdi       # 0x2501
callq 0x1040
pushq $0x3
popq %rdi
callq 0x10f0
leaq 0x815(%rip), %rdi       # 0x2597
callq 0x1040
pushq $0x1
jmp 0x1d75
nop