void sendCode(int repeat) {
  if (codeType == NEC) {
    if (repeat) {
      sendNEC(REPEAT, codeLen);
      printf("Sent NEC repeat\n");
    } 
    else {
      sendNEC(codeValue, codeLen);
      printf("Sent NEC ");
      printf("%X", codeValue);
    }
  } 
  else if (codeType == SONY) {
    sendSony(codeValue, codeLen);
    printf("Sent Sony ");
    printf("%X", codeValue);
  } 
  else if (codeType == RC5 || codeType == RC6) {
    if (!repeat) {
      // Flip the toggle bit for a new button press
      toggle = 1 - toggle;
    }
    // Put the toggle bit into the code to send
    codeValue = codeValue & ~(1 << (codeLen - 1));
    codeValue = codeValue | (toggle << (codeLen - 1));
    if (codeType == RC5) {
      printf("Sent RC5 ");
      printf("%X", codeValue);
      sendRC5(codeValue, codeLen);
    } 
    else {
      sendRC6(codeValue, codeLen);
      printf("Sent RC6 ");
      printf("%X\n", codeValue);
    }
  } 
  else if (codeType == UNKNOWN /* i.e. raw */) {
    // Assume 38 KHz
    sendRaw(rawCodes, codeLen, 38000);
    printf("Sent raw\n");
  }
}
示例#2
0
/**
 * Expects one of:
 *  70,MODE_ECO,FAN_AUTO (temp,acMode,fanSpeed)
 *  OFF
 *
 */
int setState(String command) {
  Spark.publish("SET_STATE", command);

  int start = Time.now();

  bool toggleOn = false;
  int temp;
  enum AcModes mode;
  enum FanSpeeds speed;

  if (command == "OFF") {
    temp = 0;
    mode = MODE_OFF;
    speed = FAN_OFF;
  } else if (command == "ON") {
    toggleOn = true;

    // Ignored but set to surpress compiler warnings
    temp = 72;
    mode = MODE_ECO;
    speed = FAN_AUTO;
  } else {
    int firstComma = command.indexOf(",");
    if (firstComma == -1) {
      return 2;
    }

    int secondComma = command.indexOf(",", firstComma + 1);
    if (secondComma == -1) {
      return 3;
    }

    temp = command.substring(0, firstComma).toInt();
    if (temp < MIN_TEMP || temp > MAX_TEMP) {
      return 4;
    }

    mode = getModeForName(command.substring(firstComma + 1, secondComma));
    if (mode == MODE_INVALID) {
      Spark.publish("MODE_INVALID", command.substring(firstComma + 1, secondComma));
      return 5;
    }

    speed = getSpeedForName(command.substring(secondComma + 1));
    if (speed == FAN_INVALID) {
      Spark.publish("FAN_INVALID", command.substring(secondComma + 1));
      return 6;
    }
  }

  // Try to get AC to the correct state for up to 10 seconds
  while (Time.now() - start < 10) {
    // Special handling for OFF
    if (toggleOn) {
      if (!isAcOn()) {
        Spark.publish("ON", "");
        sendNEC(AC_CMD__ON_OFF);
      } else {
        break;
      }
    }
    else if (mode == MODE_OFF) {
      if (isAcOn()) {
        Spark.publish("OFF", "");
        sendNEC(AC_CMD__ON_OFF);
      } else {
        break;
      }
    } else if (!isAcOn()) {
      // First turn it on if it is off
      Spark.publish("ON", "");
      sendNEC(AC_CMD__ON_OFF);
    } else {
      bool stable = true;
      if (mode != getAcMode()) {
        stable = false;
        switch (mode) {
          case MODE_FAN:
            Spark.publish("MODE", "MODE_FAN");
            sendNEC(AC_CMD__FAN_ONLY);
            break;
          case MODE_ECO:
            Spark.publish("MODE", "MODE_ECO");
            sendNEC(AC_CMD__ENERGY_SAVER);
            break;
          case MODE_COOL:
            Spark.publish("MODE", "MODE_COOL");
            sendNEC(AC_CMD__COOL);
            break;
        }
      }

      if (speed != getFanSpeed()) {
        stable = false;
        switch (speed) {
          case FAN_AUTO:
            Spark.publish("SPEED", "FAN_AUTO");
            sendNEC(AC_CMD__AUTO_FAN);
            break;
          case FAN_LOW:
            Spark.publish("SPEED", "FAN_LOW");
            sendNEC(AC_CMD__FAN_SPEED_D);
            sendNEC(AC_CMD__FAN_SPEED_D);
            sendNEC(AC_CMD__FAN_SPEED_D);
            break;
          case FAN_MEDIUM:
            Spark.publish("SPEED", "FAN_MEDIUM");
            sendNEC(AC_CMD__FAN_SPEED_D);
            sendNEC(AC_CMD__FAN_SPEED_D);
            sendNEC(AC_CMD__FAN_SPEED_D);
            sendNEC(AC_CMD__FAN_SPEED_U);
            break;
          case FAN_HIGH:
            Spark.publish("SPEED", "FAN_HIGH");
            sendNEC(AC_CMD__FAN_SPEED_U);
            sendNEC(AC_CMD__FAN_SPEED_U);
            break;
        }
      }

      if (mode != MODE_FAN && temp != getTemp()) {
        stable = false;
        int tempDiff = temp - getTemp();
        if (tempDiff < 0) {
          for (int i = 0; i < abs(tempDiff); i++) {
            sendNEC(AC_CMD__TEMP_TIMER_D);
          }
        } else {
          for (int i = 0; i < abs(tempDiff); i++) {
            sendNEC(AC_CMD__TEMP_TIMER_U);
          }
        }
      }

      // Yay at a stable state!
      if (stable) {
        return 0;
      }
    }

    // Wait for the AC to handle IR codes and the display to update
    delay(2000);
    processAcDisplayData();
  }

  if (Time.now() - start >= 30) {
    // Return 1000 for timeout
    return 1000;
  } else {
    // Return 0 for success
    return 0;
  }
}