// File: rachit31[P]CubeSatADCS/readMag.cpp
// Function: readMag(void*)
void* readMag(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 toMag = 4.0/32767.0; // gauss/count
	 /* const double toMag = 4.0/(pow(2,15)-1); // gauss/count */
	 
	 double B_B[3];
	 
	 SleepTimer myTimer(1.0/1.25);
	 
	 myTimer.initialize();
	 
	 while (true)
	{
		i2c.readBuffer(0x1E, 0x28, buffer, 6);
		
		data = (buffer[1] << 8) | buffer[0]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		B_B[0] = 1.0*toMag*data; // converting to engineering units (gauss)
		
		data = (buffer[3] << 8) | buffer[2]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		B_B[1] = toMag*data; // converting to engineering units (gauss)
		
		data = (buffer[5] << 8) | buffer[4]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		B_B[2] = toMag*data; // converting to engineering units (gauss)
		
		magMutex.lock();
		
		memcpy(magBuffer, B_B, sizeof(B_B));
		
		magMutex.unlock();
		
		// std::cout << B_B[0] << ", " << B_B[1] << ", " << B_B[2] << std::endl;
		
		myTimer.sleep();
		
		/*
		magMutex.lock();
		
		memcpy(magBuffer, B_B, sizeof(B_B));
		
		magMutex.unlock();
		
		magMutex.signal();
		*/
		
		/*
		B_B[0] = -1.0*toMag*data - Mag_Bw_B[0]; // converting to engineering units (gauss)
		
		data = (buffer[3] << 8) | buffer[2]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		B_B[1] = toMag*data - Mag_Bw_B[1]; // converting to engineering units (gauss)
		
		data = (buffer[5] << 8) | buffer[4]; // converting to signed 16 bit value (in raw counts, no units), by bit shifting
		B_B[2] = toMag*data - Mag_Bw_B[2]; // converting to engineering units (gauss)
		*/
	}
	
     // 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 0x3ec6(%rip), %rsi      # 0x9a20
leaq -0x20(%rbp), %rdi
callq 0x5980
movabsq $0x3f20002000400080, %rax # imm = 0x3F20002000400080
movq %rax, -0x30(%rbp)
leaq -0x70(%rbp), %rdi
movsd 0x74d3(%rip), %xmm0     # 0xd050
callq 0x4fd0
jmp 0x5b84
leaq -0x70(%rbp), %rdi
callq 0x5070
jmp 0x5b8f
jmp 0x5b91
jmp 0x5b93
leaq 0xc95e(%rip), %rdi      # 0x124f8
movl $0x1e, %esi
movl $0x28, %edx
leaq -0x26(%rbp), %rcx
movl $0x6, %r8d
callq 0x45b0
jmp 0x5bb5
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 0x7482(%rip), %xmm1     # 0xd058
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
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 0xc78e(%rip), %rdi      # 0x123b8
callq 0x49e0
jmp 0x5c31
movq -0x40(%rbp), %rcx
leaq 0xc764(%rip), %rax      # 0x123a0
movq %rcx, 0x10(%rax)
movaps -0x50(%rbp), %xmm0
movaps %xmm0, (%rax)
leaq 0xc76a(%rip), %rdi      # 0x123b8
callq 0x4b30
jmp 0x5c55
leaq -0x70(%rbp), %rdi
callq 0x5090
jmp 0x5c60
jmp 0x5b91
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
nopw %cs:(%rax,%rax)
nopl (%rax)