// File: /workspace/github_c_makefile_star2_O2/Openvario[P]sensord/humidity.c
// Function: am2321_read
int am2321_read (t_ds2482 *sensor) {
	uint8_t data[8];

	data[0] = 0x00;
	if (write(sensor->fd, NULL, 0)<0)
	return 3;

	struct timespec nstime = {0,1.0e6};
	while (nanosleep (&nstime,&nstime)) ; /* Wait atleast 1.5ms */

	/* write at addr 0x03, start reg = 0x00, num regs = 0x04 */
	data[0] = 0x03;
	data[1] = 0x00;
	data[2] = 0x04;
	if (write(sensor->fd, data, 3) < 0) return 3;

	/* wait for AM2320 */
	nstime.tv_nsec = 1.6e6;
	while (nanosleep (&nstime,&nstime)) ; /* Wait atleast 1.5ms */

	/*
	 * Read out 8 bytes of data
	 * Byte 0: Should be Modbus function code 0x03
	 * Byte 1: Should be number of registers to read (0x04)
	 * Byte 2: Humidity msb
	 * Byte 3: Humidity lsb
	 * Byte 4: Temperature msb
	 * Byte 5: Temperature lsb
	 * Byte 6: CRC lsb byte
	 * Byte 7: CRC msb byte
	*/

  	if (read(sensor->fd, data, 8) < 0) return 4;

	/* Check data[0] and data[1] */
	if (data[0] != 0x03 || data[1] != 0x04) return 9;

	/* Check CRC */
	uint16_t crcdata = _calc_crc16(data, 6);
	uint16_t crcread = _combine_bytes(data[7], data[6]);
	if (crcdata != crcread) return 10;

	uint16_t temp16 = _combine_bytes(data[4], data[5]);
	uint16_t humi16 = _combine_bytes(data[2], data[3]);

	/* Temperature resolution is 16Bit,
	 * temperature highest bit (Bit15) is equal to 1 indicates a
	 * negative temperature, the temperature highest bit (Bit15)
	 * is equal to 0 indicates a positive temperature;
	 * temperature in addition to the most significant bit (Bit14 ~ Bit0)
	 *  indicates the temperature sensor string value.
	 * Temperature sensor value is a string of 10 times the
	 * actual temperature value.
	*/

 	if (temp16 & 0x8000) temp16 = -(temp16 & 0x7FFF);

	if (sensor->temp_present) {
		sensor->temperature = (float)temp16 / 10.0;
		sensor->temp_valid = 1;
	}
	if (sensor->humidity_present) {
		sensor->humidity = (float)humi16 / 10.0;
		sensor->humidity_valid = 1;
	}
	return 0;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movl (%rdi), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x20b0
testq %rax, %rax
js 0x7da6
movups 0x1edc(%rip), %xmm0     # 0x9bc8
leaq 0x10(%rsp), %r14
movaps %xmm0, (%r14)
movq %r14, %rdi
movq %r14, %rsi
callq 0x2120
testl %eax, %eax
jne 0x7cf5
leaq 0x8(%rsp), %rsi
movw $0x3, (%rsi)
movb $0x4, 0x2(%rsi)
movl (%rbx), %edi
pushq $0x3
popq %rdx
callq 0x20b0
testq %rax, %rax
js 0x7da6
leaq 0x10(%rsp), %r14
movq $0x186a00, 0x8(%r14)    # imm = 0x186A00
movq %r14, %rdi
movq %r14, %rsi
callq 0x2120
testl %eax, %eax
jne 0x7d32
movl (%rbx), %edi
leaq 0x8(%rsp), %r14
pushq $0x8
popq %rdx
movq %r14, %rsi
callq 0x2180
testq %rax, %rax
js 0x7db1
cmpb $0x3, 0x8(%rsp)
pushq $0x9
popq %rax
jne 0x7da9
cmpb $0x4, 0x9(%rsp)
jne 0x7da9
movw $0xffff, %dx            # imm = 0xFFFF
pushq $0x6
popq %rax
pushq $0x8
popq %rcx
subq $0x1, %rax
jb 0x7db5
movzbl (%r14), %esi
xorl %esi, %edx
movl %ecx, %esi
subl $0x1, %esi
jb 0x7da1
movzwl %dx, %edi
shrl %edi
movl %edi, %r8d
xorl $0xffffa001, %r8d       # imm = 0xFFFFA001
testb $0x1, %dl
cmovel %edi, %r8d
movl %r8d, %edx
jmp 0x7d81
incq %r14
jmp 0x7d73
pushq $0x3
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r14
retq
pushq $0x4
jmp 0x7da8
cmpw 0xe(%rsp), %dx
jne 0x7e2f
cmpb $0x0, 0x2c(%rbx)
je 0x7dfd
movzbl 0xc(%rsp), %eax
shll $0x8, %eax
movzbl 0xd(%rsp), %ecx
orl %eax, %ecx
movl %ecx, %edx
andl $0x7fff, %edx           # imm = 0x7FFF
negl %edx
testw %ax, %ax
cmovnsl %ecx, %edx
movzwl %dx, %eax
cvtsi2sd %eax, %xmm0
divsd 0x1ba8(%rip), %xmm0     # 0x9998
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x24(%rbx)
movb $0x1, 0x2e(%rbx)
xorl %eax, %eax
cmpb $0x0, 0x2d(%rbx)
je 0x7da9
movzwl 0xa(%rsp), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
cvtsi2sd %ecx, %xmm0
divsd 0x1b7b(%rip), %xmm0     # 0x9998
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x28(%rbx)
movb $0x1, 0x2f(%rbx)
jmp 0x7da9
pushq $0xa
jmp 0x7da8