// File: /workspace/github_c_makefile_star2_O3/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 0x266d(%rip), %rax      # 0x3fc8
movq (%rax), %rdi
xorl %esi, %esi
callq 0x1080
leaq 0x944(%rip), %rdi       # 0x22b0
leaq 0x967(%rip), %r8        # 0x22da
leaq 0x96c(%rip), %r9        # 0x22e6
movl $0x30, %esi
movl $0x31, %edx
movl $0x31, %ecx
xorl %eax, %eax
callq 0x1090
leaq 0xa08(%rip), %rdi       # 0x239f
callq 0x1040
leaq 0xa3b(%rip), %rdi       # 0x23de
callq 0x1040
leaq 0xa5f(%rip), %rdi       # 0x240e
callq 0x1040
leaq 0x30(%rsp), %rdi
movl $0x50, %esi
callq 0x1150
testl %eax, %eax
jne 0x1e23
leaq 0x24(%rsp), %rax
movb $0x0, 0x6(%rax)
leaq 0x914(%rip), %r15       # 0x22ef
leaq 0x30(%rsp), %r12
leaq 0xb3d(%rip), %r14       # 0x2524
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 0x1a87
leal -0x63(%rax), %ecx
cmpl $0x6, %ecx
ja 0x1acd
leaq 0x7d6(%rip), %rdx       # 0x21f4
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
leaq 0xadb(%rip), %rdi       # 0x2509
callq 0x1040
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x162a
testl %eax, %eax
jne 0x1b85
leaq 0x932(%rip), %rdi       # 0x2381
xorl %eax, %eax
callq 0x1090
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movl $0x28, %esi
callq 0x1758
testl %eax, %eax
je 0x1c54
leaq 0xb3d(%rip), %rdi       # 0x25b4
callq 0x1040
movss 0x1c(%rsp), %xmm1
jmp 0x1cfa
cmpl $0x3f, %eax
jne 0x1c2d
movq 0x2549(%rip), %rax      # 0x3fe0
movl (%rax), %esi
leaq 0x8cf(%rip), %rdi       # 0x236f
xorl %eax, %eax
callq 0x1090
leaq 0x849(%rip), %rdi       # 0x22f7
leaq 0x75b(%rip), %rsi       # 0x2210
xorl %eax, %eax
callq 0x1090
leaq 0x9a3(%rip), %rdi       # 0x2466
callq 0x1040
jmp 0x19f9
cmpl $0x73, %eax
jne 0x19f9
movq 0x24f3(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
callq 0x1070
cmpq $0x6, %rax
jne 0x1c3b
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x162a
testl %eax, %eax
jne 0x1b85
movq 0x24c5(%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 0x1b14
leaq 0x81f(%rip), %rdi       # 0x2358
leaq 0x24(%rsp), %rsi
xorl %eax, %eax
callq 0x1090
jmp 0x1d0f
leaq 0x7a6(%rip), %rdi       # 0x22f7
leaq 0x6b8(%rip), %rsi       # 0x2210
xorl %eax, %eax
callq 0x1090
jmp 0x19f9
leaq 0x99e(%rip), %rdi       # 0x2509
callq 0x1040
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x162a
testl %eax, %eax
je 0x1d65
movq %r14, %rdi
callq 0x1040
leaq 0x9ac(%rip), %rdi       # 0x2540
callq 0x1040
movl $0x2, 0xc(%rsp)
jmp 0x19f9
leaq 0x9b9(%rip), %rdi       # 0x2566
callq 0x1040
xorl %r15d, %r15d
movzbl %r15b, %edx
movq %r12, %rdi
movq %r13, %rsi
movl $0x1, %ecx
callq 0x16dc
incl %r15d
cmpl $0x80, %r15d
jne 0x1bb5
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 0x1614
leaq 0x972(%rip), %rdi       # 0x257c
callq 0x1040
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
movl $0x14, %ecx
callq 0x16dc
leaq 0x6c7(%rip), %r15       # 0x22ef
jmp 0x19f9
cmpl $-0x1, %eax
jne 0x19f9
jmp 0x1d46
leaq 0x830(%rip), %rdi       # 0x2472
callq 0x1040
movl $0x1, 0xc(%rsp)
jmp 0x19f9
leaq 0x94f(%rip), %rdi       # 0x25aa
callq 0x1040
movss 0x580(%rip), %xmm0      # 0x21e8
movlps %xmm0, 0x5c(%rsp)
movq %r14, %rdi
callq 0x17e6
movl $0x1b774, %edi          # imm = 0x1B774
callq 0x1130
callq 0x151c
xorps %xmm1, %xmm1
movl $0x320, %r15d           # imm = 0x320
leaq 0x6fd(%rip), %rbx       # 0x2391
movss %xmm1, 0x2c(%rsp)
movss 0x54a(%rip), %xmm0      # 0x21ec
callq 0x152d
movq %r14, %rdi
callq 0x1815
callq 0x151c
movq %r14, %rdi
callq 0x18a7
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 0x1c94
divss 0x508(%rip), %xmm1      # 0x21f0
movss %xmm1, 0x1c(%rsp)
leaq 0x5fa(%rip), %r15       # 0x22ef
movq 0x38(%rsp), %rbx
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
leaq 0x60f(%rip), %rdi       # 0x2317
movb $0x1, %al
callq 0x1090
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x1614
leaq 0x859(%rip), %rdi       # 0x257c
callq 0x1040
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl $0x14, %ecx
callq 0x16dc
leaq 0x7e3(%rip), %r14       # 0x2524
jmp 0x19f9
leaq 0x7b4(%rip), %rdi       # 0x2501
callq 0x1040
movl 0xc(%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 0x728(%rip), %rdi       # 0x24aa
callq 0x1040
leaq 0x732(%rip), %rdi       # 0x24c0
callq 0x1040
leaq 0x58d(%rip), %rdi       # 0x2327
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
xorps %xmm0, %xmm0
cvtss2sd 0x1c(%rsp), %xmm0
leaq 0x582(%rip), %rdi       # 0x2336
movb $0x1, %al
callq 0x1090
leaq 0x73f(%rip), %rdi       # 0x2501
callq 0x1040
movl 0xc(%rsp), %edi
callq 0x10f0
leaq 0x6ff(%rip), %rdi       # 0x24d6
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 0x16dc
decl %ebp
jne 0x1deb
leaq 0x6e7(%rip), %rdi       # 0x24ef
callq 0x1040
leaq 0x6ed(%rip), %rdi       # 0x2501
callq 0x1040
movl $0x3, %edi
callq 0x10f0
leaq 0x76d(%rip), %rdi       # 0x2597
callq 0x1040
movl $0x1, %edi
callq 0x10f0
nopl (%rax)