// File: rachit31[P]CubeSatADCS/readAccel.cpp
// Function: readAccel(void*)
void* readAccel(void *pArg)
 {
	 // Push Cleanup Handler to cleanup thread
	 pthread_cleanup_push(&Thread::cleanup,pArg);
	 
	 // Code goes here...
	
	 uint8_t buffer[6];
	 int16_t data;
	 
	 const double toAccel = 2.0/32767.0; // gs/count
	 /* const double toAccel = 2.0/(pow(2,15)-1); // gs/count */
	 
	 double a_B[3];
	 
	 SleepTimer myTimer(0.1);
	 
	 myTimer.initialize();
	 
	 while (true)
	{
		i2c.readBuffer(0x6b, 0x28, buffer, 6);
		
		data = (buffer[1] << 8) | buffer[0]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		a_B[0] = -1.0*toAccel*data; // converting to engineering units (g)
		
		data = (buffer[3] << 8) | buffer[2]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		a_B[1] = toAccel*data; // converting to engineering units (g)
		
		data = (buffer[5] << 8) | buffer[4]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		a_B[2] = toAccel*data; // converting to engineering units (g)
		
		accelMutex.lock();
		
		memcpy(accelBuffer, a_B, sizeof(a_B));
		
		accelMutex.unlock();
		
		// std::cout << a_B[0] << ", " << a_B[1] << ", " << a_B[2] << std::endl;
		
		myTimer.sleep();
		
		/*
		accelMutex.lock();
		
		memcpy(accelBuffer, a_B, sizeof(a_B));
		
		accelMutex.unlock();
		
		accelMutex.signal();
		*/
		
		/*
		a_B[0] = -1.0*toAccel*data - Accel_Bw_B[0]; // converting to engineering units (g)
		
		data = (buffer[3] << 8) | buffer[2]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		a_B[1] = toAccel*data - Accel_Bw_B[1]; // converting to engineering units (g)
		
		data = (buffer[5] << 8) | buffer[4]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		a_B[2] = toAccel*data - Accel_Bw_B[2]; // converting to engineering units (g)
		*/
	}
	
     // Run clean up handler to cleanup thread (TLPI pg 676)
     pthread_cleanup_pop(1);
 }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdx
leaq 0x4016(%rip), %rsi      # 0x9a20
leaq -0x20(%rbp), %rdi
callq 0x5980
movabsq $0x3f10002000400080, %rax # imm = 0x3F10002000400080
movq %rax, -0x30(%rbp)
leaq -0x70(%rbp), %rdi
movsd 0x7603(%rip), %xmm0     # 0xd030
callq 0x4fd0
jmp 0x5a34
leaq -0x70(%rbp), %rdi
callq 0x5070
jmp 0x5a3f
jmp 0x5a41
jmp 0x5a43
leaq 0xcaae(%rip), %rdi      # 0x124f8
movl $0x6b, %esi
movl $0x28, %edx
leaq -0x26(%rbp), %rcx
movl $0x6, %r8d
callq 0x45b0
jmp 0x5a65
movzbl -0x25(%rbp), %eax
shll $0x8, %eax
movzbl -0x26(%rbp), %ecx
orl %ecx, %eax
movw %ax, -0x28(%rbp)
movswl -0x28(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x75c2(%rip), %xmm1     # 0xd048
mulsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movzbl -0x23(%rbp), %eax
shll $0x8, %eax
movzbl -0x24(%rbp), %ecx
orl %ecx, %eax
movw %ax, -0x28(%rbp)
movswl -0x28(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x7588(%rip), %xmm1     # 0xd038
mulsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movzbl -0x21(%rbp), %eax
shll $0x8, %eax
movzbl -0x22(%rbp), %ecx
orl %ecx, %eax
movw %ax, -0x28(%rbp)
movswl -0x28(%rbp), %eax
cvtsi2sd %eax, %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
leaq 0xc856(%rip), %rdi      # 0x12338
callq 0x49e0
jmp 0x5ae9
movq -0x40(%rbp), %rcx
leaq 0xc82c(%rip), %rax      # 0x12320
movq %rcx, 0x10(%rax)
movaps -0x50(%rbp), %xmm0
movaps %xmm0, (%rax)
leaq 0xc832(%rip), %rdi      # 0x12338
callq 0x4b30
jmp 0x5b0d
leaq -0x70(%rbp), %rdi
callq 0x5090
jmp 0x5b18
jmp 0x5a41
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x59b0
movq -0x78(%rbp), %rdi
callq 0x3420
nopl (%rax,%rax)