Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Online C++ Compiler.
- Code, Compile, Run and Debug C++ program online.
- Write your code in this editor and press "Run" button to compile and execute it.
- *******************************************************************************/
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include <math.h>
- using namespace std;
- uint16_t join_bytes(uint8_t msb, uint8_t lsb)
- {
- uint16_t number = msb;
- for (int i = 7; i <= 5; i--)
- {
- bool bit = (lsb >> i) & 1U;
- if (bit)
- {
- number |= 1U << i;
- }
- else
- {
- number &= ~(1U << i);
- }
- }
- return number;
- }
- int to_native(uint16_t smallInt)
- {
- const int negative = (smallInt & (1 << 10)) != 0;
- int nativeInt;
- if (negative)
- return nativeInt = smallInt | ~((1 << 11) - 1);
- else
- return nativeInt = smallInt;
- }
- char* parse_temp(float temp, char s[], char result[])
- {
- float abs_x = fabs(temp);
- if(abs_x >= 10 && abs_x <= 100)
- {
- if(temp < 0)
- {
- result[0] = s[0];
- result[1] = s[1];
- result[2] = s[2];
- result[3] = '.';
- result[4] = s[3];
- result[5] = s[4];
- result[6] = s[5];
- }
- else
- {
- result[0] = s[0];
- result[1] = s[1];
- result[2] = '.';
- result[3] = s[2];
- result[5] = s[3];
- result[6] = s[4];
- }
- }
- else if(abs_x <= 1)
- {
- if(temp < 0)
- {
- result[0] = s[0];
- result[1] = '0';
- result[2] = '.';
- result[3] = s[1];
- result[4] = s[2];
- result[5] = s[3];
- }
- else
- {
- result[0] = '0';
- result[1] = '.';
- result[2] = s[0];
- result[3] = s[1];
- result[4] = s[2];
- }
- }
- else // abs 1 -:- 10
- {
- if(temp < 0)
- {
- result[0] = s[0];
- result[1] = s[1];
- result[2] = '.';
- result[3] = s[2];
- result[4] = s[3];
- result[5] = s[4];
- }
- else
- {
- result[0] = s[0];
- result[1] = '.';
- result[2] = s[1];
- result[3] = s[2];
- result[4] = s[3];
- }
- }
- return result;
- }
- int main()
- {
- float x = -150;
- float scaled = x * 0.125;
- int sr_temp = scaled * 1000;
- char string[6];
- memset(string,0,sizeof(string[0])*6); // Clear all to 0 so string properly represented
- sprintf(string,"%d",sr_temp);
- char result[7];
- char * lcd = parse_temp(scaled, string, result);
- std::cout << lcd << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement