// File: /workspace/github_cpp_makefile_star2_O3/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 -0x58(%rbp), %rdi
movsd 0x3c64(%rip), %xmm0     # 0x8008
callq 0x3c60
leaq -0x58(%rbp), %rdi
callq 0x3cde
movq %rbx, -0x38(%rbp)
leaq 0x6eab(%rip), %r12      # 0xb268
leaq 0x6e8c(%rip), %rbx      # 0xb250
leaq 0x70dd(%rip), %rdi      # 0xb4a8
movl $0x6b, %esi
movl $0x18, %edx
leaq -0x2e(%rbp), %rcx
movl $0x6, %r8d
callq 0x33ae
movzwl -0x2e(%rbp), %r14d
movzwl -0x2c(%rbp), %r15d
movzwl -0x2a(%rbp), %r13d
movq %r12, %rdi
callq 0x3712
movswl %r13w, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x3c0a(%rip), %xmm2     # 0x8018
mulsd %xmm2, %xmm0
movswl %r15w, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
mulsd %xmm2, %xmm1
movswl %r14w, %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd 0x3bdc(%rip), %xmm2     # 0x8010
movsd %xmm2, (%rbx)
movsd %xmm1, 0x8(%rbx)
movsd %xmm0, 0x10(%rbx)
movq %r12, %rdi
callq 0x3840
movq %r12, %rdi
callq 0x3b30
leaq -0x58(%rbp), %rdi
callq 0x3cf0
jmp 0x43c4
movq %rax, %r14
movq -0x38(%rbp), %rbx
jmp 0x446c
movq %rax, %r14
movq %rbx, %rdi
callq 0x59f6
movq %r14, %rdi
callq 0x23a0
movq %rax, %rdi
callq 0x36be