Float to string

This commit is contained in:
Christian Colglazier 2021-11-08 19:46:06 -05:00
parent 1509e8a327
commit c9a10c9b2e
3 changed files with 42 additions and 15 deletions

View File

@ -110,21 +110,21 @@ void ADDroplet::Draw() {
10,
Font_6x8,
"A: " +
std::to_string(static_cast<uint32_t>(1000*ad[0].GetAttack())) +
"ms");
FloatToString(ad[0].GetAttack(), 2) +
"s");
WriteString(Patch(),
GetScreenMin(),
20,
Font_6x8,
"D: " +
std::to_string(static_cast<uint32_t>(1000*ad[0].GetDecay())) +
"ms");
FloatToString(ad[0].GetDecay(), 2) +
"s");
WriteString(Patch(),
GetScreenMin(),
30,
Font_6x8,
"C: " +
std::to_string(static_cast<uint32_t>(1000*ad[0].GetCurve())));
FloatToString(ad[0].GetCurve(), 2));
if(GetState() == DropletState::kFull) {
int mid = (GetScreenMax() - GetScreenMin())/2;
WriteString(Patch(),
@ -132,21 +132,21 @@ void ADDroplet::Draw() {
10,
Font_6x8,
"A: " +
std::to_string(static_cast<uint32_t>(1000*ad[1].GetAttack())) +
"ms");
FloatToString(ad[1].GetAttack(), 2) +
"s");
WriteString(Patch(),
mid,
20,
Font_6x8,
"D: " +
std::to_string(static_cast<uint32_t>(1000*ad[1].GetDecay())) +
"ms");
FloatToString(ad[1].GetDecay(), 2) +
"s");
WriteString(Patch(),
mid,
30,
Font_6x8,
"C: " +
std::to_string(static_cast<uint32_t>(1000*ad[1].GetCurve())));
FloatToString(ad[1].GetCurve(), 2));
}
DrawName("AD");
}

View File

@ -115,3 +115,21 @@ std::string WaveToString(uint8_t wf) {
}
return "";
}
std::string FloatToString(float num, int places) {
std::string sign = "";
int integral = static_cast<int>(num);
int fractional = static_cast<int>((abs(num) - abs(integral))
* pow(10, places));
if (num < 0.0f && num > -1.0f) {
sign = "-";
}
return sign + std::to_string(integral) + "." + std::to_string(fractional);
}

View File

@ -7,6 +7,7 @@
#include "daisy_patch.h"
#include <string>
#include <math.h>
using namespace daisy;
using namespace daisysp;
@ -131,11 +132,19 @@ void WriteDoubleCentered(DaisyPatch* patch,
std::string text);
/*
* Converts oscilator to name of wave shape.
*
* @param wf wave shape
* @return name of wave shap
*/
* Converts oscilator to name of wave shape.
*
* @param wf wave shape
* @return name of wave shap
*/
std::string WaveToString(uint8_t wf);
/*
* Converts float to formatted string.
*
* @param input number
* @param number of decimal places
*/
std::string FloatToString(float num, int places);
#endif // CASCADE_UTIL_H_