// File: /workspace/github_cpp_makefile_star2_O1/rachit31[P]CubeSatADCS/readGyros.cpp
// Function: readGyros(void*)
void* readGyros(void *pArg)
 {
	 // Push Cleanup Handler to cleanup thread
	 pthread_cleanup_push(&Thread::cleanup,pArg);
	 
	 // Code goes here...
	
	 const double pi = 3.141592653589793;
	 const double gyroToW = 500.0/32767.0*pi/180.0; // radians/second/count
	 /* const double gyroToW = 500/(pow(2,15)-1)*pi/180.0; // radians/second/count */
	 
	 uint8_t buffer[6];
	 int16_t data;
	 
	 double wbi_B[3];
	 
	 SleepTimer myTimer(1.0/119.0);
	 
	 myTimer.initialize();
	 
	 while (true)
	{
		i2c.readBuffer(0x6b, 0x18, buffer, 6);
		
		data = (buffer[1] << 8) | buffer[0]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		wbi_B[0] = -1.0*gyroToW*data; // converting to engineering units (radians/second)
		
		data = (buffer[3] << 8) | buffer[2]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		wbi_B[1] = gyroToW*data; // converting to engineering units (radians/second)
		
		data = (buffer[5] << 8) | buffer[4]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		wbi_B[2] = gyroToW*data; // converting to engineering units (radians/second)
		
		gyroMutex.lock();
		
		memcpy(gyroBuffer, wbi_B, sizeof(wbi_B));
		
		gyroMutex.unlock();
		
		gyroMutex.signal();
		
		myTimer.sleep();
		
		// std::cout << wbi_B[0] << ", " << wbi_B[1] << ", " << wbi_B[2] << std::endl;
		
		/*
		wbi_B[0] = -1.0*gyroToW*data - Bw_B[0]; // converting to engineering units (radians/second)
		
		data = (buffer[3] << 8) | buffer[2]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		wbi_B[1] = gyroToW*data - Bw_B[1]; // converting to engineering units (radians/second)
		
		data = (buffer[5] << 8) | buffer[4]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		wbi_B[2] = gyroToW*data - Bw_B[2]; // converting to engineering units (radians/second)
		*/
	}
	
     // Run clean up handler to cleanup thread (TLPI pg 676)
     pthread_cleanup_pop(1);
 }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
leaq -0x60(%rbp), %rdi
movsd 0x3c50(%rip), %xmm0     # 0x8008
callq 0x3c70
leaq -0x60(%rbp), %rdi
callq 0x3cee
movq %rbx, -0x40(%rbp)
leaq 0x70d7(%rip), %rdi      # 0xb4a8
movl $0x6b, %esi
movl $0x18, %edx
leaq -0x2e(%rbp), %rcx
movl $0x6, %r8d
callq 0x33be
movzbl -0x2d(%rbp), %r15d
movzbl -0x2e(%rbp), %eax
movl %eax, -0x34(%rbp)
movzbl -0x2b(%rbp), %r12d
movzbl -0x2c(%rbp), %r13d
movzbl -0x29(%rbp), %r14d
movzbl -0x2a(%rbp), %ebx
leaq 0x6e58(%rip), %rdi      # 0xb268
callq 0x3722
movzwl %r14w, %eax
shll $0x8, %eax
movzwl %bx, %ecx
orl %eax, %ecx
movswl %cx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x3be5(%rip), %xmm2     # 0x8018
mulsd %xmm2, %xmm0
movzwl %r12w, %eax
shll $0x8, %eax
movzwl %r13w, %ecx
orl %eax, %ecx
movswl %cx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
mulsd %xmm2, %xmm1
movzwl %r15w, %eax
shll $0x8, %eax
movzwl -0x34(%rbp), %ecx
orl %eax, %ecx
movswl %cx, %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd 0x3b9f(%rip), %xmm2     # 0x8010
leaq 0x6dd8(%rip), %rax      # 0xb250
movsd %xmm2, (%rax)
movsd %xmm1, 0x8(%rax)
movsd %xmm0, 0x10(%rax)
leaq 0x6ddb(%rip), %rdi      # 0xb268
callq 0x3850
leaq 0x6dcf(%rip), %rdi      # 0xb268
callq 0x3b40
leaq -0x60(%rbp), %rdi
callq 0x3d00
jmp 0x43ca
movq %rax, %r14
movq -0x40(%rbp), %rbx
jmp 0x44b8
movq %rax, %r14
movq %rbx, %rdi
callq 0x5afa
movq %r14, %rdi
callq 0x23b0
movq %rax, %rdi
callq 0x36ce