/*
 * temperatur.cpp
 *
 *  Created on: Feb 23, 2018
 *      Author: sebastian
 */
#include <wiringPi.h>
//#include <wiringPiSPI.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>
#include <unistd.h>

#include "global.h"
#include "logger.h"
#include "temperatur.h"
#include "spi.h"

uint8_t rawData[4] = {0,0,0,0};

void temperatur_init(){
	pinMode(TEMP_CS, OUTPUT);
	digitalWrite(TEMP_CS, HIGH);
}

int16_t get_temperatur(){
	//active low
	digitalWrite(TEMP_CS, LOW);
	//do one read, Temperatur will only be reevaluated on CS High
	for(int i = 0; i < 4; i++){
		rawData[i] = spi_readwrite(0);
	}
	digitalWrite(TEMP_CS, HIGH);

	//extracting the temperature data without the lowest two bit
	int16_t temp = (((uint16_t)rawData[0]) << 4) | (((uint16_t)rawData[1] >> 4) & 0x0F);
	logger(V_BASIC, "%x, %x, %x, %x\n", rawData[0], rawData[1], rawData[2], rawData[3]);
	return temp;
}

