Utility APIs

Miscellaneous helper APIs.

JPEGs

APIs to create JPEG files from RGB images.

For example, this code shows how to create a JPEG with an image captured from the camera (from examples/camera_streaming_http/):

    std::vector<uint8_t> buf(CameraTask::kWidth * CameraTask::kHeight *
                             CameraFormatBpp(CameraFormat::kRgb));
    auto fmt = CameraFrameFormat{
        CameraFormat::kRgb,       CameraFilterMethod::kBilinear,
        CameraRotation::k0,       CameraTask::kWidth,
        CameraTask::kHeight,
        /*preserve_ratio=*/false, buf.data(),
        /*while_balance=*/true};
    if (!CameraTask::GetSingleton()->GetFrame({fmt})) {
      printf("Unable to get frame from camera\r\n");
      return {};
    }

    std::vector<uint8_t> jpeg;
    JpegCompressRgb(buf.data(), fmt.width, fmt.height, /*quality=*/75, &jpeg);
namespace coralmicro

Functions

unsigned long JpegCompressRgb(unsigned char *rgb, int width, int height, int quality, unsigned char *buf, unsigned long size)

Converts an RGB image to JPEG format.

Parameters
  • rgb – The image in RGB format.

  • width – The image’s width.

  • height – The image’s height.

  • quality – The quality of the image after compression (must be within [0-100]).

  • buf – The buffer to return the JPEG image data to.

  • size – The size allocated for buf.

Returns

The size of the resulting JPEG buffer.

JpegBuffer JpegCompressRgb(unsigned char *rgb, int width, int height, int quality)

Converts an RGB image to JPEG format.

Parameters
  • rgb – The image in RGB format.

  • width – The image’s width.

  • height – The image’s height.

  • quality – The quality of the image after compression (must be within [0-100]).

Returns

A JPEG image buffer. Note: You have to deallocate data from JpegBuffer with free().

void JpegCompressRgb(unsigned char *rgb, int width, int height, int quality, std::vector<uint8_t> *out)

Converts an RGB image to JPEG format.

Parameters
  • rgb – The image in RGB format.

  • width – The image’s width.

  • height – The image’s height.

  • quality – The quality of the image after compression (must be within [0-100]).

  • out – the output vector to return the resulting JPEG image to.

struct JpegBuffer
#include <jpeg.h>

Represents a JPEG image buffer.

Public Members

unsigned char *data

The Jpeg image data.

unsigned long size

The size of the image.

Strings

namespace coralmicro

Functions

template<size_t N>
constexpr size_t StrLen(const char (&str)[N])

Gets the length of a const char.

This function is preferred over strlen() for constant strings that can be evaluated at compile time.

Parameters

str – The string to get length.

Template Parameters

N – The string array length determined by the compiler through function template argument deduction.

Returns

The length of the const char string.

template<size_t N>
bool StrStartsWith(const char *s, const char (&prefix)[N])

Checks if a string starts with a prefix.

Parameters
  • s – The full string to check for prefix.

  • prefix – The prefix to look for at the beginning of the string s.

Template Parameters

N – The length of the string.

Returns

True if prefix is the prefix of s, else false.

template<size_t N>
bool StrEndsWith(const std::string &s, const char (&suffix)[N])

Checks if a string ends with a suffix.

Parameters
  • s – The full string to check for suffix.

  • suffix – The suffix to look for at the end of the string s.

Template Parameters

N – The length of the string.

Returns

True if suffix is the suffix of s, else false.

template<typename C, typename ...T>
void StrAppend(C *v, const char *format_str, T... args)

Appends a string to a source string.

Parameters
  • v – The source string to append format_str string to.

  • format_str – The string with format specifier to append to v.

  • args – The arguments to the format specifier.

Template Parameters

T – The variadic class template that allows args to be any type.

std::string StrToHex(const char *s, size_t size)

Returns a string’s hexadecimal representation.

Parameters
  • s – The source array of raw characters.

  • size – The size of the source array.

Returns

The hexadecimal representation of the string.

inline std::string StrToHex(const std::string &s)

Returns a string’s hexadecimal representation.

Parameters

s – The source string.

Returns

The hexadecimal representation of the string.

Timers

namespace coralmicro

Functions

void TimerInit()

Initializes the timer.

Programs on the M7 do not need to call this because it is automatically called internally. M7 programs can immediately call functions such as TimerMicros().

Programs on the M4 must call this to intialize the timer before they can use timers. For example:

TimerInit();
auto current_time = TimerMillis();
uint64_t TimerMicros()

Microseconds since boot.

inline uint64_t TimerMillis()

Milliseconds since boot.

void TimerSetRtcTime(uint32_t sec)
void TimerGetRtcTime(struct tm *time)

Random numbers

namespace coralmicro

Functions

void RandomInit()

Initializes hardware random number generator.

Programs on the M7 do not need to call this because it is automatically called internally. M7 programs can immediately call RandomGenerate().

Programs on the M4 must call this to intialize the generator before calling RandomGenerate().

bool RandomGenerate(void *buf, size_t size)

Generates random byte sequence.

Parameters
  • buf – Buffer to write random bytes to.

  • size – Size of the buffer.

Returns

True upon success, false otherwise.

Temperature sensors

namespace coralmicro

Enums

enum class TempSensor

Enumerates the various temperature sensors.

Values:

enumerator kCpu

The CPU temperature sensor.

enumerator kTpu

The Edge TPU temperature sensor.

Functions

void TempSensorInit()

Initializes the temperature sensors.

Programs on the M7 do not need to call this because it is automatically called internally. M7 programs can immediately call TempSensorRead().

Programs on the M4 must call this to intialize the sensors before calling TempSensorRead().

float TempSensorRead(TempSensor sensor)

Reads the temperature of a sensor.

Parameters

sensor – The sensor to get the temperature.

Returns

The actual temperature in Celcius or 0.0 on failure.