// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
leaq -0x60(%rbp), %rdi
movsd 0x3a2c(%rip), %xmm0     # 0x8048
callq 0x3c70
leaq -0x60(%rbp), %rdi
callq 0x3cee
movq %rbx, -0x40(%rbp)
leaq 0x6e73(%rip), %rdi      # 0xb4a8
movl $0x1e, %esi
movl $0x28, %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 0x6cf4(%rip), %rdi      # 0xb368
callq 0x3722
movzwl %r14w, %eax
shll $0x8, %eax
movzwl %bx, %ecx
orl %eax, %ecx
movswl %cx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x39b9(%rip), %xmm3     # 0x8050
mulsd %xmm3, %xmm0
movzwl %r12w, %eax
shll $0x8, %eax
movzwl %r13w, %ecx
orl %eax, %ecx
movswl %cx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
mulsd %xmm3, %xmm1
movzwl %r15w, %eax
shll $0x8, %eax
movzwl -0x34(%rbp), %ecx
orl %eax, %ecx
movswl %cx, %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd %xmm3, %xmm2
leaq 0x6c78(%rip), %rax      # 0xb350
movsd %xmm2, (%rax)
movsd %xmm1, 0x8(%rax)
movsd %xmm0, 0x10(%rax)
leaq 0x6c7b(%rip), %rdi      # 0xb368
callq 0x3850
leaq -0x60(%rbp), %rdi
callq 0x3d00
jmp 0x462e
movq %rax, %r14
movq -0x40(%rbp), %rbx
jmp 0x470c
movq %rax, %r14
movq %rbx, %rdi
callq 0x5afa
movq %r14, %rdi
callq 0x23b0
movq %rax, %rdi
callq 0x36ce