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

View File

@ -115,3 +115,21 @@ std::string WaveToString(uint8_t wf) {
} }
return ""; 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 "daisy_patch.h"
#include <string> #include <string>
#include <math.h>
using namespace daisy; using namespace daisy;
using namespace daisysp; using namespace daisysp;
@ -131,11 +132,19 @@ void WriteDoubleCentered(DaisyPatch* patch,
std::string text); std::string text);
/* /*
* Converts oscilator to name of wave shape. * Converts oscilator to name of wave shape.
* *
* @param wf wave shape * @param wf wave shape
* @return name of wave shap * @return name of wave shap
*/ */
std::string WaveToString(uint8_t wf); 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_ #endif // CASCADE_UTIL_H_