-1

我正在使用连接到 E-Paper 显示器的 ESP32。我正在使用已经开发的代码。该代码显示了我选择的某些加密货币的当前价格以及来自代码其他部分的其他信息。对于这部分代码,我想根据加密货币的值对显示的价格进行四舍五入:如果加密货币的值小于 1,我希望将其四舍五入为 6 位小数,在 1 到 10 之间我希望将其四舍五入4 位小数及以上 100 只有两位小数。显示价格的代码部分如下,我不知道处理这个问题的最佳解决方案是什么。

*/
#ifndef BOARD_HAS_PSRAM
#error "Please enable PSRAM !!!"
#endif


#include <WiFi.h>
#include <HTTPClient.h>
#include <WifiClientSecure.h>
#include <ArduinoJson.h>
#include "cryptos.h"
#include "coingecko-api.h"
#include <Arduino.h>
#include <esp_task_wdt.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "epd_driver.h"
#include "firasans.h"
#include "opensans8b.h"
#include "opensans10b.h"
#include "esp_adc_cal.h"
#include <Wire.h>
#include <SPI.h>
#include <SD.h>


#define BATT_PIN            36
#define SD_MISO             12
#define SD_MOSI             13
#define SD_SCLK             14
#define SD_CS               15

int cursor_x ;
int cursor_y ;

uint8_t *framebuffer;
int vref = 1100;

// ----------------------------
// Configurations - Update these
// ----------------------------

const char *ssid = "xxxxx";
const char *password = "xxxxx";


// ----------------------------
// ----------------------------


void setup()
{
  char buf[128];

  Serial.begin(115200);


  connectToWifi();

  // Correct the ADC reference voltage
  esp_adc_cal_characteristics_t adc_chars;
  esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 1100, &adc_chars);
  if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
    Serial.printf("eFuse Vref:%u mV", adc_chars.vref);
    vref = adc_chars.vref;
  }

  epd_init();

  framebuffer = (uint8_t *)ps_calloc(sizeof(uint8_t), EPD_WIDTH * EPD_HEIGHT / 2);
  if (!framebuffer) {
    Serial.println("alloc memory failed !!!");
    while (1);
  }
  memset(framebuffer, 0xFF, EPD_WIDTH * EPD_HEIGHT / 2);

  epd_poweron();
  epd_clear();

  epd_poweroff();

  epd_poweron();


}

void loop()
{
  downloadBaseData("eur");
  delay(1000);
  downloadBtcAndEthPrice();
  title();
  for (int i = 0; i < cryptosCount; i++)
  {
    cursor_y = (20 * (i + 4));
    renderCryptoCard(cryptos[i]);
  }
  delay(300000);
}

void title()
{

  cursor_x = 20;
  cursor_y = 40;
  char *sym = "Symbol";
  writeln((GFXfont *)&OpenSans10B, sym, &cursor_x, &cursor_y, NULL);

  cursor_x = 150;
  cursor_y = 40;
  char *prc = "Price";
  writeln((GFXfont *)&OpenSans10B, prc, &cursor_x, &cursor_y, NULL);

  cursor_x = 330;
  cursor_y = 40;
  char *da = "Day(%)";
  writeln((GFXfont *)&OpenSans10B, da, &cursor_x, &cursor_y, NULL);

  cursor_x = 430;
  cursor_y = 40;
  char *we = "Week(%)";
  writeln((GFXfont *)&OpenSans10B, we, &cursor_x, &cursor_y, NULL);


}

void renderCryptoCard(Crypto crypto)
{

  Serial.print("Crypto Name  - "); Serial.println(crypto.symbol);

  cursor_x = 35;

  char *string1 = &crypto.symbol[0];

  writeln((GFXfont *)&OpenSans10B, string1, &cursor_x, &cursor_y, NULL);

  cursor_x = 150;

  String Str = String (crypto.price.eur, 6);
  char* string2 = &Str[0];

  Serial.print("price eur - "); Serial.println(Str);

  Rect_t area = {
    .x = cursor_x,
    .y = cursor_y - 15,
    .width = 160,
    .height = 18,
  };

  epd_clear_area(area);

  writeln((GFXfont *)&OpenSans10B, string2, &cursor_x, &cursor_y, NULL);

  Serial.print("Day change - "); Serial.println(formatPercentageChange(crypto.dayChange));

  cursor_x = 330;

  Rect_t area1 = {
    .x = cursor_x,
    .y = cursor_y - 15,
    .width = 100,
    .height = 18,
  };

  epd_clear_area(area1);
  Str = (String)(crypto.dayChange);
  char* string3 = &Str[0];

  writeln((GFXfont *)&OpenSans10B, string3, &cursor_x, &cursor_y, NULL);


  Serial.print("Week change - "); Serial.println(formatPercentageChange(crypto.weekChange));

  cursor_x = 430;

  Rect_t area2 = {
    .x = cursor_x,
    .y = cursor_y - 15,
    .width = 100,
    .height = 18,
  };

  epd_clear_area(area2);

  Str = (String)(crypto.weekChange);
  char* string4 = &Str[0];

  writeln((GFXfont *)&OpenSans10B, string4, &cursor_x, &cursor_y, NULL);

}


void connectToWifi()
{
  WiFi.begin(ssid, password);
  String dots[3] = {".", "..", "..."};
  int numberOfDots = 1;

  //tft.setTextColor(//tft_WHITE, //tft_BLACK);
  while (WiFi.status() != WL_CONNECTED)
  {
    //tft.drawCentreString("Connecting to WiFi " + dots[numberOfDots - 1], 120, 120, 2);
    Serial.println("Connecting to WiFi");
    if (numberOfDots == 3)
    {
      numberOfDots = 0;
    }
    else
    {
      numberOfDots++;
    }

    delay(300);
    //tft.fillScreen(//tft_BLACK);
  }

  Serial.println("Connected!!!_______________");

}




String formatPercentageChange(double change)
{


  double absChange = change;

  if (change < 0)
  {
    absChange = -change;
  }

  if (absChange > 100) {
    return String(absChange, 0) + "%";
  } else if (absChange >= 10) {
    return String(absChange, 1) + "%";
  } else {
    return String(absChange) + "%";
  }
}

'''

起初我想根据加密货币的价格插入一个 if-else-else,这只改变了行

   String Str = String (crypto.price.eur, 6) ;

但我对编程知之甚少,尽管多次尝试,我总是会出错。很抱歉问了这么愚蠢的问题,但我才刚刚开始:)

——马可

4

1 回答 1

2

关于唯一要更改的行,您是对的。条件运算符比if-else-else更简单:

   double f = crypto.price.eur;    // just for brevity
   String Str = String(f, f<1 ? 6 : f<=10 ? 4 : 6);

顺便说一句,你说的小数点以下 1、6 位可能是错误的;1 到 10,4 位小数和 10, 6 位小数之间,但您可以根据实际要求轻松调整以上内容。

于 2021-11-19T19:39:11.357 回答