Skip to content

Infrared

Tiogaplanet edited this page Oct 22, 2018 · 4 revisions

MiP's infrared sensors can not only detect obstructions and gestures, they can be used for interacting with other MiPs.


enableMiPDetectionMode()

void enableMiPDetectionMode(uint8_t id, uint8_t txPower);

Description

Allows MiP to be discovered by another MiP using IR. MiP broadcasts a user-defined identification number for another MiP to read.

Parameters

  • id is the user-defined identification number to send to another MiP. A user-defined value cannot be zero.
  • txPower is the IR transmission power. Valid values are 1 to 120 for about 1-300cm.

Returns

Nothing

Example

#include <mip_esp8266.h>

MiP mip;

// Load this sketch on a pair of MiPs facing each other and use a different MIP_ID_NO for each.
#define MIP_ID_NO       0x10
#define MIP_IR_TX_POWER 0x78

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

  Serial.println(F("EnableMiPDetectionMode.ino - Enable your MiP to be discovered by another using IR."));

  mip.disableMiPDetectionMode();
  
  if (!mip.isMiPDetectionModeEnabled()) {
    Serial.println(F("I am not discoverable."));
  }

  mip.enableMiPDetectionMode(MIP_ID_NO, MIP_IR_TX_POWER);

  if (mip.isMiPDetectionModeEnabled()) {
    Serial.println(F("Now I can be discovered."));
  }
}

void loop() {
  if (mip.availableDetectedMiPEvents()) {
    Serial.print(F("I detected MiP with ID number ")); Serial.println(mip.readDetectedMiP(), HEX);
  }
}

disableMiPDetectionMode()

void disableMiPDetectionMode();

Description

Prevents MiP from being discovered by another MiP using IR.

Parameters

None

Returns

Nothing

Example

#include <mip_esp8266.h>

MiP mip;

// Load this sketch on a pair of MiPs facing each other and use a different MIP_ID_NO for each.
#define MIP_ID_NO       0x10
#define MIP_IR_TX_POWER 0x78

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

  Serial.println(F("EnableMiPDetectionMode.ino - Enable your MiP to be discovered by another using IR."));

  mip.disableMiPDetectionMode();
  
  if (!mip.isMiPDetectionModeEnabled()) {
    Serial.println(F("I am not discoverable."));
  }

  mip.enableMiPDetectionMode(MIP_ID_NO, MIP_IR_TX_POWER);

  if (mip.isMiPDetectionModeEnabled()) {
    Serial.println(F("Now I can be discovered."));
  }
}

void loop() {
  if (mip.availableDetectedMiPEvents()) {
    Serial.print(F("I detected MiP with ID number ")); Serial.println(mip.readDetectedMiP(), HEX);
  }
}

isMiPDetectionModeEnabled()

bool isMiPDetectionModeEnabled()

Description

Checks whether MiP is broadcasting an identification number using IR.

Parameters

None

Returns

  • true if MiP is broadcasting its identification number.

Example

#include <mip.h>

MiP mip;

// Load this sketch on a pair of MiPs facing each other and use a different MIP_ID_NO for each.
#define MIP_ID_NO       0x10
#define MIP_IR_TX_POWER 0x78

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

  Serial.println(F("EnableMiPDetectionMode.ino - Enable your MiP to be discovered by another using IR."));

  mip.disableMiPDetectionMode();
  
  if (!mip.isMiPDetectionModeEnabled()) {
    Serial.println(F("I am not discoverable."));
  }

  mip.enableMiPDetectionMode(MIP_ID_NO, MIP_IR_TX_POWER);

  if (mip.isMiPDetectionModeEnabled()) {
    Serial.println(F("Now I can be discovered."));
  }
}

void loop() {
  if (mip.availableDetectedMiPEvents()) {
    Serial.print(F("I detected MiP with ID number ")); Serial.println(mip.readDetectedMiP(), HEX);
  }
}

readDetectedMiP()

uint8_t readDetectedMiP()

Description

Reads the identification number of a detected MiP.

Parameters

None

Returns

  • The identification number of the detected MiP.

Notes

Zero is a valid identifier. Call availableIRCodeEvents() before calling readDetectedMiP() to prevent interpreting an empty answer with a MiP with an identification number of zero.

Example

#include <mip_esp8266.h>

MiP mip;

// Load this sketch on a pair of MiPs facing each other and use a different MIP_ID_NO for each.
#define MIP_ID_NO       0x10
#define MIP_IR_TX_POWER 0x78

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

  Serial.println(F("EnableMiPDetectionMode.ino - Enable your MiP to be discovered by another using IR."));

  mip.disableMiPDetectionMode();
  
  if (!mip.isMiPDetectionModeEnabled()) {
    Serial.println(F("I am not discoverable."));
  }

  mip.enableMiPDetectionMode(MIP_ID_NO, MIP_IR_TX_POWER);

  if (mip.isMiPDetectionModeEnabled()) {
    Serial.println(F("Now I can be discovered."));
  }
}

void loop() {
  if (mip.availableDetectedMiPEvents()) {
    Serial.print(F("I detected MiP with ID number ")); Serial.println(mip.readDetectedMiP(), HEX);
  }
}

availableDetectedMiPEvents()

uint8_t availableDetectedMiPEvents()

Description

Provides the number of other MiPs that MiP has detected.

Parameters

None

Returns

  • The number of MiPs that MiP has detected.

Example

#include <mip_esp8266.h>

MiP mip;

// Load this sketch on a pair of MiPs facing each other and use a different MIP_ID_NO for each.
#define MIP_ID_NO       0x10
#define MIP_IR_TX_POWER 0x78

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

  Serial.println(F("EnableMiPDetectionMode.ino - Enable your MiP to be discovered by another using IR."));

  mip.disableMiPDetectionMode();
  
  if (!mip.isMiPDetectionModeEnabled()) {
    Serial.println(F("I am not discoverable."));
  }

  mip.enableMiPDetectionMode(MIP_ID_NO, MIP_IR_TX_POWER);

  if (mip.isMiPDetectionModeEnabled()) {
    Serial.println(F("Now I can be discovered."));
  }
}

void loop() {
  if (mip.availableDetectedMiPEvents()) {
    Serial.print(F("I detected MiP with ID number ")); Serial.println(mip.readDetectedMiP(), HEX);
  }
}

sendIRDongleCode()

void sendIRDongleCode(uint16_t sendCode, uint8_t transmitPower)

Description

Sends a two-byte code to another MiP using IR.

Parameters

  • sendCode contains a two-byte code to send to another MiP.
  • txPower is the IR transmission power. Valid values are 1 to 120 for about 1-300cm.

Returns

Nothing

Example

#include <mip_esp8266.h>

// Try different values for transmission power (0x01 - 0x78)
#define MIP_IR_TX_POWER  0x78

MiP  mip;
bool connectResult;

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

  Serial.println(F("SendIRDongleCode.ino - Send code to another MiP using IR."));
}

void loop() {
  uint16_t dongleCode;

  char formattedOutput[14];
  
  // Try different codes for dongleCode.
  dongleCode = 0x45;
  dongleCode <<= 8;
  dongleCode |= 0x67;

  sprintf(formattedOutput, "Sending 0x%04X", dongleCode);

  Serial.println(formattedOutput);

  mip.sendIRDongleCode(dongleCode, MIP_IR_TX_POWER);

  delay(1000);
}

readIRDongleCode()

uint32_t readIRDongleCode()

Description

Reads code sent by another MiP using IR.

Parameters

None

Returns

  • A 32-bit value containing the data received via IR.
  • 0 if no data was received.

Example

#include <mip_esp8266.h>

MiP       mip;
bool      connectResult;

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

  Serial.println(F("ReadIRDongleCode.ino - Receive code from another MiP using IR."));
}

void loop() {
  uint32_t receiveCode;

  if (mip.availableIRCodeEvents()) {
    receiveCode = mip.readIRDongleCode();

    Serial.print(F("Received "));
    Serial.print(((receiveCode >> 28) & 0xFF), HEX);
    Serial.print(F(" "));
    Serial.print(((receiveCode >> 16) & 0xFF), HEX);
    Serial.print(F(" "));
    Serial.print(((receiveCode >> 8) & 0xFF), HEX);
    Serial.print(F(" "));
    Serial.print((receiveCode & 0xFF), HEX);
    Serial.println();
  }
}

availableIRCodeEvents()

uint8_t availableIRCodeEvents()

Description

Returns the number of IR code events that the library currently has sitting in its queue, ready to be read by calling readIRCodeEvent().

Parameters

None

Returns

  • The number of available code events that MiP has detected.

Example

#include <mip_esp8266.h>

MiP       mip;
bool      connectResult;

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

  Serial.println(F("ReadIRDongleCode.ino - Receive code from another MiP using IR."));
}

void loop() {
  uint32_t receiveCode;

  if (mip.availableIRCodeEvents()) {
    receiveCode = mip.readIRDongleCode();

    Serial.print(F("Received "));
    Serial.print(((receiveCode >> 28) & 0xFF), HEX);
    Serial.print(F(" "));
    Serial.print(((receiveCode >> 16) & 0xFF), HEX);
    Serial.print(F(" "));
    Serial.print(((receiveCode >> 8) & 0xFF), HEX);
    Serial.print(F(" "));
    Serial.print((receiveCode & 0xFF), HEX);
    Serial.println();
  }
}