// File: /workspace/github_c_makefile_star2_O1/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, 0xb(%rsp)
movq 0x269d(%rip), %rax      # 0x3fc8
movq (%rax), %rdi
xorl %esi, %esi
callq 0x1080
leaq 0x970(%rip), %rdi       # 0x22ac
leaq 0x993(%rip), %r8        # 0x22d6
leaq 0x998(%rip), %r9        # 0x22e2
movl $0x30, %esi
movl $0x31, %edx
movl $0x31, %ecx
xorl %eax, %eax
callq 0x1090
leaq 0xa34(%rip), %rdi       # 0x239b
callq 0x1040
leaq 0xa67(%rip), %rdi       # 0x23da
callq 0x1040
leaq 0xa8b(%rip), %rdi       # 0x240a
callq 0x1040
leaq 0x30(%rsp), %rdi
movl $0x50, %esi
callq 0x1150
testl %eax, %eax
jne 0x1de9
leaq 0x24(%rsp), %rax
movb $0x0, 0x6(%rax)
leaq 0x940(%rip), %r15       # 0x22eb
leaq 0x30(%rsp), %r12
leaq 0xb69(%rip), %r14       # 0x2520
leaq 0xb(%rsp), %r13
movl $0x0, 0xc(%rsp)
movq %rbx, 0x38(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x10e0
cmpl $0x62, %eax
jle 0x1a51
leal -0x63(%rax), %ecx
cmpl $0x6, %ecx
ja 0x1a97
leaq 0x802(%rip), %rdx       # 0x21f0
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
leaq 0xb07(%rip), %rdi       # 0x2505
callq 0x1040
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x15aa
testl %eax, %eax
jne 0x1b4f
leaq 0x95e(%rip), %rdi       # 0x237d
xorl %eax, %eax
callq 0x1090
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movl $0x28, %esi
callq 0x16dc
testl %eax, %eax
je 0x1c1e
leaq 0xb69(%rip), %rdi       # 0x25b0
callq 0x1040
jmp 0x1cc0
cmpl $0x3f, %eax
jne 0x1bf7
movq 0x257f(%rip), %rax      # 0x3fe0
movl (%rax), %esi
leaq 0x901(%rip), %rdi       # 0x236b
xorl %eax, %eax
callq 0x1090
leaq 0x87b(%rip), %rdi       # 0x22f3
leaq 0x78d(%rip), %rsi       # 0x220c
xorl %eax, %eax
callq 0x1090
leaq 0x9d5(%rip), %rdi       # 0x2462
callq 0x1040
jmp 0x19c9
cmpl $0x73, %eax
jne 0x19c9
movq 0x2529(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
callq 0x1070
cmpq $0x6, %rax
jne 0x1c05
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x15aa
testl %eax, %eax
jne 0x1b4f
movq 0x24fb(%rip), %rdi      # 0x3fd0
movq (%rdi), %rax
leaq 0x1(%rax), %rcx
xorl %edx, %edx
movb (%rax,%rdx), %sil
movb %sil, 0x14(%rsp,%rdx)
movb %sil, 0x24(%rsp,%rdx)
leaq (%rcx,%rdx), %rsi
movq %rsi, (%rdi)
incq %rdx
cmpq $0x6, %rdx
jne 0x1ade
leaq 0x851(%rip), %rdi       # 0x2354
leaq 0x24(%rsp), %rsi
xorl %eax, %eax
callq 0x1090
jmp 0x1cd7
leaq 0x7d8(%rip), %rdi       # 0x22f3
leaq 0x6ea(%rip), %rsi       # 0x220c
xorl %eax, %eax
callq 0x1090
jmp 0x19c9
leaq 0x9d0(%rip), %rdi       # 0x2505
callq 0x1040
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x15aa
testl %eax, %eax
je 0x1d2d
movq %r14, %rdi
callq 0x1040
leaq 0x9de(%rip), %rdi       # 0x253c
callq 0x1040
movl $0x2, 0xc(%rsp)
jmp 0x19c9
leaq 0x9eb(%rip), %rdi       # 0x2562
callq 0x1040
xorl %r15d, %r15d
movzbl %r15b, %edx
movq %r12, %rdi
movq %r13, %rsi
movl $0x1, %ecx
callq 0x165c
incl %r15d
cmpl $0x80, %r15d
jne 0x1b7f
movl $0x100564f, 0x10(%rsp)  # imm = 0x100564F
leaq 0x14(%rsp), %rax
movw $0x3030, 0x4(%rax)      # imm = 0x3030
movl $0x30303030, (%rax)     # imm = 0x30303030
movl $0x0, 0x1c(%rsp)
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x1594
leaq 0x9a4(%rip), %rdi       # 0x2578
callq 0x1040
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
movl $0x14, %ecx
callq 0x165c
leaq 0x6f9(%rip), %r15       # 0x22eb
jmp 0x19c9
cmpl $-0x1, %eax
jne 0x19c9
jmp 0x1d0e
leaq 0x862(%rip), %rdi       # 0x246e
callq 0x1040
movl $0x1, 0xc(%rsp)
jmp 0x19c9
leaq 0x981(%rip), %rdi       # 0x25a6
callq 0x1040
movq $0x3f800000, 0x5c(%rsp) # imm = 0x3F800000
movq %r14, %rdi
callq 0x176a
movl $0x1b774, %edi          # imm = 0x1B774
callq 0x1130
callq 0x149c
xorps %xmm1, %xmm1
movl $0x320, %r15d           # imm = 0x320
leaq 0x733(%rip), %rbx       # 0x238d
movss %xmm1, 0x2c(%rsp)
movss 0x580(%rip), %xmm0      # 0x21e8
callq 0x14ad
movq %r14, %rdi
callq 0x1799
callq 0x149c
movq %r14, %rdi
callq 0x1874
xorps %xmm0, %xmm0
cvtss2sd 0x58(%rsp), %xmm0
movq %rbx, %rdi
movb $0x1, %al
callq 0x1090
movss 0x2c(%rsp), %xmm1
addss 0x58(%rsp), %xmm1
decl %r15d
jne 0x1c5a
divss 0x53e(%rip), %xmm1      # 0x21ec
movss %xmm1, 0x1c(%rsp)
leaq 0x630(%rip), %r15       # 0x22eb
movq 0x38(%rsp), %rbx
xorps %xmm0, %xmm0
cvtss2sd 0x1c(%rsp), %xmm0
leaq 0x643(%rip), %rdi       # 0x2313
movb $0x1, %al
callq 0x1090
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x1594
leaq 0x88d(%rip), %rdi       # 0x2578
callq 0x1040
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl $0x14, %ecx
callq 0x165c
leaq 0x817(%rip), %r14       # 0x2520
jmp 0x19c9
leaq 0x7e8(%rip), %rdi       # 0x24fd
callq 0x1040
movl 0xc(%rsp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzwl 0x18(%rsp), %eax
leaq 0x24(%rsp), %rbx
movw %ax, 0x4(%rbx)
movl 0x14(%rsp), %eax
movl %eax, (%rbx)
leaq 0x75e(%rip), %rdi       # 0x24a6
callq 0x1040
leaq 0x768(%rip), %rdi       # 0x24bc
callq 0x1040
leaq 0x5c3(%rip), %rdi       # 0x2323
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
xorps %xmm0, %xmm0
cvtss2sd 0x1c(%rsp), %xmm0
leaq 0x5b8(%rip), %rdi       # 0x2332
movb $0x1, %al
callq 0x1090
leaq 0x775(%rip), %rdi       # 0x24fd
callq 0x1040
movl 0xc(%rsp), %edi
callq 0x10f0
leaq 0x735(%rip), %rdi       # 0x24d2
callq 0x1040
movl $0x14, %ebp
leaq 0x30(%rsp), %rbx
leaq 0xb(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x165c
decl %ebp
jne 0x1db1
leaq 0x71d(%rip), %rdi       # 0x24eb
callq 0x1040
leaq 0x723(%rip), %rdi       # 0x24fd
callq 0x1040
movl $0x3, %edi
callq 0x10f0
leaq 0x7a3(%rip), %rdi       # 0x2593
callq 0x1040
movl $0x1, %edi
callq 0x10f0
nop