/*
  common.h - Common declarations and definitions

  created  19/01/10
  1.0      14/06/14
*/

#ifndef common_h
#define common_h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#ifndef FALSE
#define FALSE 0
#define TRUE !FALSE
#endif

#ifndef NULL
#define NULL 0
#endif

// General numeric constants
#define PI     3.1415926535897932384626433832795
#define LN2    0.69314718055994530941723212145818
#define LN10   2.3025850929940456840179914546844

// Saturates 'x' to 'min' or 'max'
// note. for the cortex-m4, if the limit is a power of 2, use the "ssat" or "usat" instructions.
#define LIMIT(lo,x,hi) \
  if((x) < (lo)) (x) = (lo); \
  else if((x) > (hi)) (x) = (hi)

#define WRAP(lo,x,hi) \
  if((x) < (lo)) (x) = (hi); \
  else if((x) > (hi)) (x) = (lo)

typedef unsigned long long int Uint64;
typedef signed long long int Int64;

// Error return codes
// These must be negative where used i.e. -OUT_OF_RANGE
enum err_msg_e
{
  NO_ERROR_, // must be first
  OUT_OF_RANGE,
  PARAMETER_ERROR,
  UNRECOGNISED,
  INVALID_VOLUME,
  INVALID_SECTOR,
  INVALID_SIZE,
  INVALID_ADDRESS,
  INVALID_DATA,
  INVALID_GAIN,
  INVALID_DELAY,
  INVALID_RATE,
  INVALID_ROUTING,
  INVALID_VALUE,
  UNDEFINED,
  NO_SEQUENCE,
  NO_HEADER,
  INVALID_HEADER_LENGTH,
  UNSUPPORTED_FORMAT,
  INVALID_NUMBER_OF_TRACKS,
  FILE_TOO_LARGE,
  PROBLEM_READING_FILE,
  INCOMPLETE,
  PROBLEM_OPENING_FILE,
  MIDI_INIT_ERROR,
  MIDI_TERM_ERROR,
  NO_FILE_LOADED,
  NO_EXTENSION,
  NO_MATCH,
  NO_KIT,
  NO_BANK,
  NO_INSTRUMENT,
  TOO_MANY_INSTRUMENTS,
  TOO_MANY_BANKS,
  TOO_MANY_WAVES,
  UNKNOWN_HEADING,
  NOT_EDITABLE,
  INVALID_CRC,
  TOO_MANY_FILES,
  CANNOT_ALLOCATE_MEMORY,
  NOT_ACTIVE,
  INVALID_DEVICE,
  UNCONNECTED,
  TOO_MANY_PATCHES,
  UNSUPPORTED,
  NUM_ERRORS // must be last
};

#define APP_NAME      "Midisyn"
#define APP_SPRITE    "midisyn"
#define APP_DIR       "<"APP_NAME"$Dir>"
#define RESOURCES_DIR "<"APP_NAME"Res$Dir>"

#endif
