// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x30(%rbp)
leaq -0x44(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x10a0
leaq 0x166c(%rip), %rax      # 0x32c0
movq %rax, -0x50(%rbp)
movq 0x3369(%rip), %rax      # 0x4fc8
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1080
leaq 0x16ee(%rip), %rdi      # 0x3360
movl $0x30, %esi
movl $0x31, %ecx
leaq 0x1707(%rip), %r8       # 0x338a
leaq 0x170c(%rip), %r9       # 0x3396
movl %ecx, %edx
movb $0x0, %al
callq 0x1090
leaq 0x1705(%rip), %rdi      # 0x339f
movb $0x0, %al
callq 0x1090
leaq 0x1737(%rip), %rdi      # 0x33df
movb $0x0, %al
callq 0x1090
leaq 0x175a(%rip), %rdi      # 0x3410
movb $0x0, %al
callq 0x1090
leaq -0x18(%rbp), %rdi
movl $0x50, %esi
callq 0x1610
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x1cec
leaq 0x178e(%rip), %rdi      # 0x3469
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1100
movb $0x0, -0x3d(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x177f(%rip), %rdx      # 0x347d
callq 0x10f0
movl %eax, -0x38(%rbp)
cmpl $-0x1, %eax
je 0x20fd
movl -0x38(%rbp), %eax
addl $-0x3f, %eax
movl %eax, %ecx
movq %rcx, -0x58(%rbp)
subl $0x34, %eax
ja 0x20f8
movq -0x58(%rbp), %rax
leaq 0x14ad(%rip), %rcx      # 0x31dc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x50(%rbp), %rsi
leaq 0x1742(%rip), %rdi      # 0x3485
movb $0x0, %al
callq 0x1090
jmp 0x20f8
leaq 0x174c(%rip), %rdi      # 0x34a2
movb $0x0, %al
callq 0x1090
movl $0x0, -0x3c(%rbp)
cmpl $0x80, -0x3c(%rbp)
jge 0x1d96
leaq -0x44(%rbp), %rsi
movl -0x3c(%rbp), %eax
leaq -0x18(%rbp), %rdi
movl $0x1, %ecx
movzbl %al, %edx
callq 0x1770
movsbl %al, %eax
movl %eax, -0x34(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1d64
leaq -0x2c(%rbp), %rdi
leaq 0x1718(%rip), %rsi      # 0x34b9
callq 0x1040
movb $0x1, -0x29(%rbp)
leaq -0x2c(%rbp), %rdi
addq $0x4, %rdi
movl $0x30, %esi
movl $0x6, %edx
callq 0x10a0
xorps %xmm0, %xmm0
movss %xmm0, -0x20(%rbp)
leaq -0x2c(%rbp), %rdi
callq 0x1400
leaq 0x16e3(%rip), %rdi      # 0x34bc
movb $0x0, %al
callq 0x1090
leaq -0x18(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
xorl %edx, %edx
movl $0x14, %ecx
callq 0x1770
movsbl %al, %eax
movl %eax, -0x34(%rbp)
jmp 0x20f8
leaq 0x16d2(%rip), %rdi      # 0x34d8
movb $0x0, %al
callq 0x1090
leaq -0x18(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x1460
cmpl $0x0, %eax
jne 0x1e72
leaq -0x2c(%rbp), %rdi
callq 0x2120
movss -0x20(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x16bc(%rip), %rdi      # 0x34f4
movb $0x1, %al
callq 0x1090
leaq -0x2c(%rbp), %rdi
callq 0x1400
leaq 0x166d(%rip), %rdi      # 0x34bc
movb $0x0, %al
callq 0x1090
leaq -0x18(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
xorl %edx, %edx
movl $0x14, %ecx
callq 0x1770
movsbl %al, %eax
movl %eax, -0x34(%rbp)
jmp 0x1e9a
leaq 0x119f(%rip), %rdi      # 0x3018
movb $0x0, %al
callq 0x1090
leaq 0x11ae(%rip), %rdi      # 0x3035
movb $0x0, %al
callq 0x1090
movl $0x2, -0x30(%rbp)
jmp 0x20f8
jmp 0x20f8
leaq 0x165e(%rip), %rdi      # 0x3504
movb $0x0, %al
callq 0x1090
movl $0x0, -0x3c(%rbp)
movslq -0x3c(%rbp), %rax
cmpq $0x14, %rax
jae 0x1ee3
leaq -0x44(%rbp), %rsi
leaq -0x18(%rbp), %rdi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x1770
movsbl %al, %eax
movl %eax, -0x34(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1eb4
leaq 0x1634(%rip), %rdi      # 0x351e
movb $0x0, %al
callq 0x1090
movl $0x3, -0x30(%rbp)
leaq 0x1632(%rip), %rdi      # 0x3531
movb $0x0, %al
callq 0x1090
movl -0x30(%rbp), %edi
callq 0x1100
leaq 0x15c3(%rip), %rdi      # 0x34d8
movb $0x0, %al
callq 0x1090
leaq -0x18(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x1460
cmpl $0x0, %eax
jne 0x1f83
movl -0x28(%rbp), %eax
movl %eax, -0x43(%rbp)
movw -0x24(%rbp), %ax
movw %ax, -0x3f(%rbp)
leaq 0x15f7(%rip), %rdi      # 0x353a
movb $0x0, %al
callq 0x1090
leaq 0x1600(%rip), %rdi      # 0x3551
movb $0x0, %al
callq 0x1090
leaq -0x43(%rbp), %rsi
leaq 0x1605(%rip), %rdi      # 0x3568
movb $0x0, %al
callq 0x1090
movss -0x20(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x15fd(%rip), %rdi      # 0x3577
movb $0x1, %al
callq 0x1090
jmp 0x1fab
leaq 0x108e(%rip), %rdi      # 0x3018
movb $0x0, %al
callq 0x1090
leaq 0x109d(%rip), %rdi      # 0x3035
movb $0x0, %al
callq 0x1090
movl $0x2, -0x30(%rbp)
jmp 0x20f8
leaq 0x157f(%rip), %rdi      # 0x3531
movb $0x0, %al
callq 0x1090
movl -0x30(%rbp), %edi
callq 0x1100
movq 0x3008(%rip), %rax      # 0x4fd0
movq (%rax), %rdi
callq 0x1070
cmpq $0x6, %rax
jne 0x20a8
leaq -0x18(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x1460
cmpl $0x0, %eax
jne 0x2081
movl $0x0, -0x3c(%rbp)
cmpl $0x6, -0x3c(%rbp)
jge 0x203c
movq 0x2fcc(%rip), %rax      # 0x4fd0
movq (%rax), %rax
movb (%rax), %cl
movslq -0x3c(%rbp), %rax
movb %cl, -0x28(%rbp,%rax)
movslq -0x3c(%rbp), %rax
movb %cl, -0x43(%rbp,%rax)
movq 0x2fb0(%rip), %rax      # 0x4fd0
movq (%rax), %rcx
addq $0x1, %rcx
movq 0x2fa2(%rip), %rax      # 0x4fd0
movq %rcx, (%rax)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1ff7
leaq -0x43(%rbp), %rsi
leaq 0x1552(%rip), %rdi      # 0x3599
movb $0x0, %al
callq 0x1090
leaq -0x2c(%rbp), %rdi
callq 0x1400
leaq 0x145e(%rip), %rdi      # 0x34bc
movb $0x0, %al
callq 0x1090
leaq -0x18(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
xorl %edx, %edx
movl $0x14, %ecx
callq 0x1770
movsbl %al, %eax
movl %eax, -0x34(%rbp)
jmp 0x20a6
leaq 0xf90(%rip), %rdi       # 0x3018
movb $0x0, %al
callq 0x1090
leaq 0xf9f(%rip), %rdi       # 0x3035
movb $0x0, %al
callq 0x1090
movl $0x2, -0x30(%rbp)
jmp 0x20f8
jmp 0x20bf
leaq 0x1501(%rip), %rdi      # 0x35b0
movb $0x0, %al
callq 0x1090
movl $0x1, -0x30(%rbp)
jmp 0x20f8
jmp 0x20f8
movq 0x2f18(%rip), %rax      # 0x4fe0
movl (%rax), %esi
leaq 0x1518(%rip), %rdi      # 0x35e9
movb $0x0, %al
callq 0x1090
movq -0x50(%rbp), %rsi
leaq 0x13a2(%rip), %rdi      # 0x3485
movb $0x0, %al
callq 0x1090
leaq 0x150a(%rip), %rdi      # 0x35fb
movb $0x0, %al
callq 0x1090
jmp 0x1cf0
leaq 0x142d(%rip), %rdi      # 0x3531
movb $0x0, %al
callq 0x1090
movl -0x30(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)