// File: /workspace/github_c_makefile_star2_O2/WereCatf[P]Gonzales/dhtsensor.c
// Function: main
int main(int argc, char* argv[])
{
	uint8_t port, pin, retries;
	structDhtSensor *dhtSensor=NULL;

	if(argc <= 1 || strcmp(argv[1], "-h")==0 || strcmp(argv[1], "--help")==0){
		printf("This is a simple application for reading temperature and\n"
			"humidity off a DHT11- or DHT22-sensor. Call it as follows:\n\n");
		printf("dhtsensor [sensortype] [port] [pin]\n");
		printf("  'sensortype' can be 'dht11' or 'dht22'.\n");
		printf("  'port' can be a number 0-6, or a letter 'a'-'g'.\n");
		printf("  'pin' can be a number 0-31.\n");
		return 0;
	}

	if(argc < 4){
		printf("Too few parameters given!\n");
		return 1;
	}

	port = atoi(argv[2]);
	if(*argv[2] >= 'a' && *argv[2] <= 'g') port = *argv[2] - 'a';
	if(*argv[2] >= 'A' && *argv[2] <= 'G') port = *argv[2] - 'A';
	if(*argv[2] >= '0' && *argv[2] <= '6') port = *argv[2] - '0';

	pin = atoi(argv[3]);
	if(port > 6){
		printf("Port must be in the range of 0-6, or a-g!\n");
		return 1;
	}
	if(pin > 31){
		printf("Pin must be in the range of 0-31!\n");
		return 1;
	}

	int error=initGpio();
	if(error==1){
		printf("Error opening /dev/mem!\n");
		return 1;
	} else if(error==2){
		printf("Error mmapping memory!\n");
		return 2;
	}

	retries=3;
	if(strcmp(argv[1], "dht11")==0 || strcmp(argv[1], "DHT11")==0){
		while(dhtSensor==NULL){
			dhtSensor=dhtRead(port, pin, 0);
			if(dhtSensor){
				printf("DHT11 temperature: %.01fC, humidity: %.01f%%.\n",
					dhtSensor->temperature, dhtSensor->humidity);
				break;
			}
			retries--;
			if(!retries){
				printf("Error reading from sensor!\n");
				return 1;
			}
		}
	} else if(strcmp(argv[1], "dht22")==0 || strcmp(argv[1], "DHT22")==0){
		while(dhtSensor==NULL){
			dhtSensor=dhtRead(port, pin, 1);
			if(dhtSensor){
				printf("DHT22 temperature: %.01fC, humidity: %.01f%%.\n",
					dhtSensor->temperature, dhtSensor->humidity);
				printf("Retries: %d\n", 3 - retries);
				break;
			}
			retries--;
			if(!retries){
				printf("Error reading from sensor!\n");
				return 1;
			}
		}
	} else {
		printf("Unknown argument '%s' given, should be 'dht11' or 'dht22'!\n",
			argv[1]);
		return 1;
	}
	free(dhtSensor);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
cmpl $0x2, %edi
jl 0x2639
movq %rsi, %r14
movl %edi, %ebx
movq 0x8(%rsi), %rdi
cmpb $0x2d, (%rdi)
jne 0x2618
cmpb $0x68, 0x1(%rdi)
jne 0x2618
cmpb $0x0, 0x2(%rdi)
je 0x2639
leaq 0xa01(%rip), %rsi       # 0x3020
callq 0x20b0
testl %eax, %eax
je 0x2639
cmpl $0x3, %ebx
ja 0x267d
leaq 0xb48(%rip), %rdi       # 0x317c
jmp 0x2793
leaq 0xb56(%rip), %rdi       # 0x3196
callq 0x2050
leaq 0xbbf(%rip), %rdi       # 0x320b
callq 0x2050
leaq 0xbd7(%rip), %rdi       # 0x322f
callq 0x2050
leaq 0xbf5(%rip), %rdi       # 0x3259
callq 0x2050
leaq 0xc1c(%rip), %rdi       # 0x328c
callq 0x2050
xorl %r14d, %r14d
jmp 0x279c
movq 0x10(%r14), %rbx
movq %rbx, %rdi
callq 0x20e0
movb (%rbx), %cl
leal -0x61(%rcx), %edx
cmpb $0x7, %dl
movzbl %dl, %edx
cmovael %eax, %edx
leal -0x41(%rcx), %eax
cmpb $0x7, %al
movzbl %al, %eax
cmovael %edx, %eax
addb $-0x30, %cl
cmpb $0x7, %cl
movzbl %cl, %ebp
cmovael %eax, %ebp
cmpb $0x7, %bpl
jb 0x26c0
leaq 0xa97(%rip), %rdi       # 0x3152
jmp 0x2793
movq 0x18(%r14), %rdi
callq 0x20e0
movzbl %al, %ebx
cmpl $0x20, %ebx
jb 0x26dd
leaq 0xa58(%rip), %rdi       # 0x3130
jmp 0x2793
xorl %eax, %eax
callq 0x28e0
cmpl $0x2, %eax
je 0x26fa
cmpl $0x1, %eax
jne 0x270d
leaq 0xa23(%rip), %rdi       # 0x3118
jmp 0x2793
leaq 0xa00(%rip), %rdi       # 0x3101
callq 0x2050
pushq $0x2
jmp 0x279a
movzbl %bpl, %ebp
movq 0x8(%r14), %r14
leaq 0x90b(%rip), %rsi       # 0x3027
movq %r14, %rdi
callq 0x20b0
testl %eax, %eax
je 0x2774
leaq 0x8fe(%rip), %rsi       # 0x302d
movq %r14, %rdi
callq 0x20b0
testl %eax, %eax
je 0x2774
leaq 0x920(%rip), %rsi       # 0x3062
movq %r14, %rdi
callq 0x20b0
testl %eax, %eax
je 0x27c4
leaq 0x913(%rip), %rsi       # 0x3068
movq %r14, %rdi
callq 0x20b0
testl %eax, %eax
je 0x27c4
leaq 0x942(%rip), %rdi       # 0x30aa
movq %r14, %rsi
xorl %eax, %eax
callq 0x2070
jmp 0x2798
movb $-0x3, %r14b
movl %ebp, %edi
movl %ebx, %esi
xorl %edx, %edx
callq 0x2266
testq %rax, %rax
jne 0x27a8
incb %r14b
jne 0x2777
leaq 0x953(%rip), %rdi       # 0x30e6
callq 0x2050
pushq $0x1
popq %r14
movl %r14d, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
cvtss2sd 0x4(%rax), %xmm0
cvtss2sd (%rax), %xmm1
leaq 0x878(%rip), %rdi       # 0x3033
movb $0x2, %al
callq 0x2070
jmp 0x281e
xorl %r12d, %r12d
pushq $0x1
popq %r14
movl %ebp, %edi
movl %ebx, %esi
movl %r14d, %edx
callq 0x2266
testq %rax, %rax
jne 0x27f3
incl %r12d
cmpb $0x3, %r12b
jne 0x27cb
leaq 0x8fa(%rip), %rdi       # 0x30e6
callq 0x2050
jmp 0x279c
movq %rax, %r15
cvtss2sd 0x4(%rax), %xmm0
cvtss2sd (%rax), %xmm1
leaq 0x868(%rip), %rdi       # 0x306e
movb $0x2, %al
callq 0x2070
leaq 0x889(%rip), %rdi       # 0x309d
movl %r12d, %esi
xorl %eax, %eax
callq 0x2070
movq %r15, %rdi
callq 0x2030
jmp 0x2675
nop