Skip to content
Tiogaplanet edited this page Oct 9, 2018 · 3 revisions

Interact with and control MiP using claps.


enableClapEvents()

void enableClapEvents()

Description

Enable MiP to detect user claps. Once this is enabled, your code can call availableClapEvents() and readClapEvent() to determine when clap events have been detected by MiP.

Parameters

None

Returns

Nothing

Notes

  • MiP defaults to having the clap detection feature disabled after the ESP8266 attaches.

Example

#include <mip_esp8266.h>

MiP     mip;

void setup() {
  bool connectResult = mip.begin();
  if (!connectResult) {
    Serial.println(F("Failed connecting to MiP!"));
    return;
  }

  Serial.println(F("Clap.ino - Use clap related functions."));

  Serial.println(F("Calling disableClapEvents()"));
  mip.disableClapEvents();
  bool isEnabled = mip.areClapEventsEnabled();
  Serial.print(F("areClapEventsEnabled() returns "));
  if (isEnabled) {
    Serial.println(F("true - fail"));
  } else {
    Serial.println(F("false - pass"));
  }

  Serial.println(F("Calling writeClapDelay(501)"));
  mip.writeClapDelay(501);
  uint16_t delay = mip.readClapDelay();
  Serial.print(F("readClapDelay() returns "));
  Serial.println(delay);

  Serial.println(F("Calling enableClapEvents()"));
  mip.enableClapEvents();
  isEnabled = mip.areClapEventsEnabled();
  Serial.print(F("areClapEventsEnabled() returns "));
  if (isEnabled) {
    Serial.println(F("true - pass"));
  } else {
    Serial.println(F("false - fail"));
  }

  Serial.println();
  Serial.println(F("Waiting for clap events!"));
}

void loop() {
  while (mip.availableClapEvents() > 0) {
    uint8_t clapCount = mip.readClapEvent();
    Serial.print(F("Detected "));
      Serial.print(clapCount);
      Serial.println(F(" claps"));
  }
}

disableClapEvents()

void disableClapEvents()

Description

Disables MiP's ability to detect user claps.

Parameters

None

Returns

Nothing

Notes

  • MiP defaults to having the clap detection feature disabled after the ESP8266 attaches.

Example

#include <mip.h>

MiP     mip;

void setup() {
  bool connectResult = mip.begin();
  if (!connectResult) {
    Serial.println(F("Failed connecting to MiP!"));
    return;
  }

  Serial.println(F("Clap.ino - Use clap related functions."));

  Serial.println(F("Calling disableClapEvents()"));
  mip.disableClapEvents();
  bool isEnabled = mip.areClapEventsEnabled();
  Serial.print(F("areClapEventsEnabled() returns "));
  if (isEnabled) {
    Serial.println(F("true - fail"));
  } else {
    Serial.println(F("false - pass"));
  }

  Serial.println(F("Calling writeClapDelay(501)"));
  mip.writeClapDelay(501);
  uint16_t delay = mip.readClapDelay();
  Serial.print(F("readClapDelay() returns "));
  Serial.println(delay);

  Serial.println(F("Calling enableClapEvents()"));
  mip.enableClapEvents();
  isEnabled = mip.areClapEventsEnabled();
  Serial.print(F("areClapEventsEnabled() returns "));
  if (isEnabled) {
    Serial.println(F("true - pass"));
  } else {
    Serial.println(F("false - fail"));
  }

  Serial.println();
  Serial.println(F("Waiting for clap events!"));
}

void loop() {
  while (mip.availableClapEvents() > 0) {
    uint8_t clapCount = mip.readClapEvent();
    Serial.print(F("Detected "));
      Serial.print(clapCount);
      Serial.println(F(" claps"));
  }
}

areClapEventsEnabled()

bool areClapEventsEnabled()

Description

Returns whether MiP's clap detection feature is currently enabled.

Parameters

None

Returns

  • true if MiP was successfully placed in clap detection mode with a previous call to enableClapEvents().
  • false if it is not in clap detection mode.

Example

#include <mip_esp8266.h>

MiP     mip;

void setup() {
  bool connectResult = mip.begin();
  if (!connectResult) {
    Serial.println(F("Failed connecting to MiP!"));
    return;
  }

  Serial.println(F("Clap.ino - Use clap related functions."));

  Serial.println(F("Calling disableClapEvents()"));
  mip.disableClapEvents();
  bool isEnabled = mip.areClapEventsEnabled();
  Serial.print(F("areClapEventsEnabled() returns "));
  if (isEnabled) {
    Serial.println(F("true - fail"));
  } else {
    Serial.println(F("false - pass"));
  }

  Serial.println(F("Calling writeClapDelay(501)"));
  mip.writeClapDelay(501);
  uint16_t delay = mip.readClapDelay();
  Serial.print(F("readClapDelay() returns "));
  Serial.println(delay);

  Serial.println(F("Calling enableClapEvents()"));
  mip.enableClapEvents();
  isEnabled = mip.areClapEventsEnabled();
  Serial.print(F("areClapEventsEnabled() returns "));
  if (isEnabled) {
    Serial.println(F("true - pass"));
  } else {
    Serial.println(F("false - fail"));
  }

  Serial.println();
  Serial.println(F("Waiting for clap events!"));
}

void loop() {
  while (mip.availableClapEvents() > 0) {
    uint8_t clapCount = mip.readClapEvent();
    Serial.print(F("Detected "));
      Serial.print(clapCount);
      Serial.println(F(" claps"));
  }
}

writeClapDelay()

void writeClapDelay(uint16_t delay)

Description

Sets the expected delay between user claps.

Parameters

  • delay is the new delay to be used for clap detection.

Returns

Nothing

Notes

  • I don't know what the units are for this setting. Maybe milliseconds?
  • MiP defaults to having the clap delay set to 500 after the application first connects.

Example

#include <mip_esp8266.h>

MiP     mip;

void setup() {
  bool connectResult = mip.begin();
  if (!connectResult) {
    Serial.println(F("Failed connecting to MiP!"));
    return;
  }

  Serial.println(F("Clap.ino - Use clap related functions."));

  Serial.println(F("Calling disableClapEvents()"));
  mip.disableClapEvents();
  bool isEnabled = mip.areClapEventsEnabled();
  Serial.print(F("areClapEventsEnabled() returns "));
  if (isEnabled) {
    Serial.println(F("true - fail"));
  } else {
    Serial.println(F("false - pass"));
  }

  Serial.println(F("Calling writeClapDelay(501)"));
  mip.writeClapDelay(501);
  uint16_t delay = mip.readClapDelay();
  Serial.print(F("readClapDelay() returns "));
  Serial.println(delay);

  Serial.println(F("Calling enableClapEvents()"));
  mip.enableClapEvents();
  isEnabled = mip.areClapEventsEnabled();
  Serial.print(F("areClapEventsEnabled() returns "));
  if (isEnabled) {
    Serial.println(F("true - pass"));
  } else {
    Serial.println(F("false - fail"));
  }

  Serial.println();
  Serial.println(F("Waiting for clap events!"));
}

void loop() {
  while (mip.availableClapEvents() > 0) {
    uint8_t clapCount = mip.readClapEvent();
    Serial.print(F("Detected "));
      Serial.print(clapCount);
      Serial.println(F(" claps"));
  }
}

readClapDelay()

uint16_t readClapDelay()

Description

Read MiP’s current clap delay.

Parameters

None

Returns

The current setting of the delay expected between claps. It defaults to 500.

Example

#include <mip_esp8266.h>

MiP     mip;

void setup() {
  bool connectResult = mip.begin();
  if (!connectResult) {
    Serial.println(F("Failed connecting to MiP!"));
    return;
  }

  Serial.println(F("Clap.ino - Use clap related functions."));

  Serial.println(F("Calling disableClapEvents()"));
  mip.disableClapEvents();
  bool isEnabled = mip.areClapEventsEnabled();
  Serial.print(F("areClapEventsEnabled() returns "));
  if (isEnabled) {
    Serial.println(F("true - fail"));
  } else {
    Serial.println(F("false - pass"));
  }

  Serial.println(F("Calling writeClapDelay(501)"));
  mip.writeClapDelay(501);
  uint16_t delay = mip.readClapDelay();
  Serial.print(F("readClapDelay() returns "));
  Serial.println(delay);

  Serial.println(F("Calling enableClapEvents()"));
  mip.enableClapEvents();
  isEnabled = mip.areClapEventsEnabled();
  Serial.print(F("areClapEventsEnabled() returns "));
  if (isEnabled) {
    Serial.println(F("true - pass"));
  } else {
    Serial.println(F("false - fail"));
  }

  Serial.println();
  Serial.println(F("Waiting for clap events!"));
}

void loop() {
  while (mip.availableClapEvents() > 0) {
    uint8_t clapCount = mip.readClapEvent();
    Serial.print(F("Detected "));
      Serial.print(clapCount);
      Serial.println(F(" claps"));
  }
}

availableClapEvents()

uint8_t availableClapEvents()

Description

Returns the number of clap detection events that the library currently has sitting in its queue, ready to be read by calling readClapEvent(). MiP must have already been placed in clap detection mode via a call to enableClapEvents() for new clap detection events to be added to this queue.

Parameters

None

Returns

  • 0 if there are currently no clap detection events ready for reading. Calling readClapEvent() now would retun 0.
  • Non-zero value indicates the number of readClapEvent() calls that can be made and successfully return a valid clap detection event.

Notes

  • The maximum number of clap detection events that can be queued up between calls to readClapEvent() is 8. If this count is exceeded, the oldest events will be overwritten.

Example

#include <mip_esp8266.h>

MiP     mip;

void setup() {
  bool connectResult = mip.begin();
  if (!connectResult) {
    Serial.println(F("Failed connecting to MiP!"));
    return;
  }

  Serial.println(F("Clap.ino - Use clap related functions."));

  Serial.println(F("Calling disableClapEvents()"));
  mip.disableClapEvents();
  bool isEnabled = mip.areClapEventsEnabled();
  Serial.print(F("areClapEventsEnabled() returns "));
  if (isEnabled) {
    Serial.println(F("true - fail"));
  } else {
    Serial.println(F("false - pass"));
  }

  Serial.println(F("Calling writeClapDelay(501)"));
  mip.writeClapDelay(501);
  uint16_t delay = mip.readClapDelay();
  Serial.print(F("readClapDelay() returns "));
  Serial.println(delay);

  Serial.println(F("Calling enableClapEvents()"));
  mip.enableClapEvents();
  isEnabled = mip.areClapEventsEnabled();
  Serial.print(F("areClapEventsEnabled() returns "));
  if (isEnabled) {
    Serial.println(F("true - pass"));
  } else {
    Serial.println(F("false - fail"));
  }

  Serial.println();
  Serial.println(F("Waiting for clap events!"));
}

void loop() {
  while (mip.availableClapEvents() > 0) {
    uint8_t clapCount = mip.readClapEvent();
    Serial.print(F("Detected "));
      Serial.print(clapCount);
      Serial.println(F(" claps"));
  }
}

readClapEvent()

uint8_t readClapEvent()

Description

Returns a clap detection event from the library's queue. They will be returned in the order that MiP detected them. MiP must have already been placed in clap detection mode via a call to enableClapEvents() for new clap detection events to be added to this queue.

Parameters

None

Returns

  • 0 if the clap detection event queue is empty. availableClapEvents() would return 0 in this scenario.
  • The number of claps detected in this event (ie. 2 for a double clap).

Notes

  • The maximum number of clap detection events that can be queued up between calls to readClapEvent() is 8. If this count is exceeded, the oldest events will be overwritten.

Example

#include <mip_esp8266.h>

MiP     mip;

void setup() {
  bool connectResult = mip.begin();
  if (!connectResult) {
    Serial.println(F("Failed connecting to MiP!"));
    return;
  }

  Serial.println(F("Clap.ino - Use clap related functions."));

  Serial.println(F("Calling disableClapEvents()"));
  mip.disableClapEvents();
  bool isEnabled = mip.areClapEventsEnabled();
  Serial.print(F("areClapEventsEnabled() returns "));
  if (isEnabled) {
    Serial.println(F("true - fail"));
  } else {
    Serial.println(F("false - pass"));
  }

  Serial.println(F("Calling writeClapDelay(501)"));
  mip.writeClapDelay(501);
  uint16_t delay = mip.readClapDelay();
  Serial.print(F("readClapDelay() returns "));
  Serial.println(delay);

  Serial.println(F("Calling enableClapEvents()"));
  mip.enableClapEvents();
  isEnabled = mip.areClapEventsEnabled();
  Serial.print(F("areClapEventsEnabled() returns "));
  if (isEnabled) {
    Serial.println(F("true - pass"));
  } else {
    Serial.println(F("false - fail"));
  }

  Serial.println();
  Serial.println(F("Waiting for clap events!"));
}

void loop() {
  while (mip.availableClapEvents() > 0) {
    uint8_t clapCount = mip.readClapEvent();
    Serial.print(F("Detected "));
      Serial.print(clapCount);
      Serial.println(F(" claps"));
  }
}