// 
// 
// 

#include <Print.h>
#include "UrsHelpers.h"

void hexdump(uint8_t *buf, uint32_t len, Print* dumpDevice, uint8_t LineWidth) {
  dumpDevice->printf("in Hexdump len: %i\n", len);

  uint32_t i, j, last_line = 0;
  for (i = 0; i<len; i++) {
    if (i && !(i%LineWidth)) {
      dumpDevice->print(F("   "));
      for (j = 0; j < LineWidth && last_line + j < len; j++) {
        dumpDevice->printf("%c", (buf[last_line + j]<' ' || buf[last_line + j]>'~') ? '.' : buf[last_line + j]);
      }
      dumpDevice->println();
      last_line = i;
    }
    dumpDevice->printf(" %02x", buf[i]);
  }
  if (i && (i%LineWidth)) {
    dumpDevice->print(F("   "));
    for (j = i%LineWidth; j<LineWidth; j++) {
      dumpDevice->print(F("   "));
    }
    for (uint32_t j = 0; j < LineWidth && last_line + j < len; j++) {
      dumpDevice->printf("%c", (buf[last_line + j]<' ' || buf[last_line + j]>'~') ? '.' : buf[last_line + j]);
    }
    dumpDevice->println();
  }
  dumpDevice->println();
}


//https://gist.github.com/rzajac/eabe85fdab30e330c0b889e13e92eae6
/**
* Rise base to power of.
*
* From: http://bbs.espressif.com/viewtopic.php?t=246
*
* @param base The number.
* @param exp  The exponent.
*
* @return Product.
*/
static int power(int base, int exp) {
  int result = 1;
  while (exp) {
    result *= base;
    exp--;
  }

  return result;
}

/**
* Get string representation of float.
*
* From: http://bbs.espressif.com/viewtopic.php?t=246
*
* Warning: limited to 15 chars & non-reentrant.
*          e.g., don't use more than once per os_printf call.
*
* @param num       The float to convert to string.
* @param decimals  The number of decimal places.
*
* @return The float string representation.
*/
String ftoa(float num, uint8_t decimals) {
  char buf[16];

  int whole = (int)num;
  int decimal = (int)((num - whole) * power(10, decimals));
  if (decimal < 0) {
    // get rid of sign on decimal portion
    decimal -= 2 * decimal;
  }

  char pattern[10]; // setup printf pattern for decimal portion
  os_sprintf(pattern, "%%d.%%0%dd", decimals);
  os_sprintf(buf, pattern, whole, decimal);

  return buf;
}


// Splittet den String 'scr' in einzelne Bestandteile
// Liefert die Anzahl der gefundenen Strings zurück
// Rückgabewert ist negativ, wenn das Empfangsfeld zu klein ist, 'dest' ist aber so weit als möglich gefüllt
// scr:       String, der aufgeteilt werden soll
// delimiter: Trennzeichen
// dest:      Array zur Aufnahme der einzelnen Token. Nicht benutzte Elemente enthalten einen Leerstring
// destSize:  Größe des Empfangsfeldes
int splitString(const String src, const char delimiter, String* dest, size_t destSize) {
  int cnt = 0;
  for (int i = 0; i < destSize; i++)
    dest[i] = String();

  if (src.length() == 0)
    return 0;

  String cpy = src;
  int pos;
  while ((pos = cpy.indexOf(delimiter)) != -1) {
    cnt++; // ein Teilstück gefunden
    if (cnt <= destSize) {
      if (pos > 0) {
        dest[cnt - 1] = cpy.substring(0, pos);
      }
      else {
        dest[cnt - 1] = String();
      }
    }
    cpy = cpy.substring(pos + 1);
  }
  // und den Rest ...
  cnt++;
  if (cnt <= destSize)
    dest[cnt - 1] = cpy;
  if (cnt > destSize)
    return -cnt;
  else
    return cnt;
}