Example #1
0
int readPacket() {
  Serial1.readBytes(buff_ptr+1, 2);
  int packetSize = unpackNumber(buff_ptr, 1, 2);
  int bytesRead = 0;
  while(bytesRead < packetSize && Serial1.available() > 0) {
    bytesRead += Serial1.readBytes(buff_ptr + bytesRead + 3, packetSize-bytesRead);
  }
  return bytesRead;
}
Example #2
0
	const uint8_t *unpackFilesIncoming(
		std::unordered_multimap<std::string, Transfer::FileIncoming> &map,
		const uint8_t *src
	) {
		size_t count;
		src = unpackNumber(&count, src);

		for (size_t i = 0; i < count; ++i) {
			std::string key;
			src = unpackString(key, src);

			std::string file_tmp_name;
			src = unpackString(file_tmp_name, src);

			std::string file_name;
			src = unpackString(file_name, src);

			std::string file_type;
			src = unpackString(file_type, src);

			size_t file_size;
			src = unpackNumber(&file_size, src);

			map.emplace(
				std::move(key),
				Transfer::FileIncoming(
					std::move(file_tmp_name),
					std::move(file_name),
					std::move(file_type),
					file_size
				)
			);
		}

		return src;
	}
Example #3
0
// Interprets commands from Linino for controlling the thermostat and touchscreen
void processCommands(TempControl *tc, ScreenControl *sc) {
  if(isStreaming && (millis() - lastStreamTime) > STREAM_DELAY) {
    isStreaming = false;
    sc->clearTouch();
    sc->hideApp(false, false);
  }
//  Serial.println("processing...");
  while(Serial1.available() > 0) {
    memset(buff_ptr, 0x00, COMMAND_BUFFER_SIZE);
    int numBytes;
    Serial1.readBytes(buff_ptr, 1);
    switch(buff_ptr[0]) {
      case GET_TEMP:
        numBytes = readPacket();
        buff_ptr[6] = tc->getRoomTemp();
        buff_ptr[8] = tc->getTargetTemp();
        Serial1.print(String(GET_TEMP));
        Serial1.write((uint8_t *)(buff_ptr + 6), 4);
        Serial1.println();
        break;
      case GET_MODE:
        numBytes = readPacket();
        Serial1.println(String(GET_MODE) + modeToString(tc));
        break;
      case GET_UNIT:
        numBytes = readPacket();
        Serial1.println(String(GET_UNIT) + unitToString(tc));
        break;
      case IS_ON:
        numBytes = readPacket();
        Serial1.println(String(IS_ON) + isOnToString(tc));
        break;
      case SET_TARGET_TEMP:
        numBytes = readPacket();
        tc->setTargetTemp(unpackNumber(buff_ptr, 3, 2));
        Serial1.println(String(SET_TARGET_TEMP));
        break;
      case SWITCH:
        numBytes = readPacket();
        Serial1.println(String(SWITCH));
        switch(buff_ptr[3]) {
          case SWITCH_MODE:
            tc->switchMode();
            break;
          case SWITCH_UNIT:
            tc->switchUnit();
            break;
          case SWITCH_FAN:
            tc->switchFan();
            break;
        }
        break;
      case WRITE_TEXT:
        numBytes = readPacket();
        Serial1.println(String(WRITE_TEXT));
        buff_ptr[numBytes + 3] = '\0';
        sc->layerMode(2);
        sc->writeText(unpackNumber(buff_ptr, 3, 2), unpackNumber(buff_ptr, 5, 2),
                            unpackNumber(buff_ptr, 7, 2), unpackNumber(buff_ptr, 9, 2),
                            unpackNumber(buff_ptr, 11, 1), buff_ptr+12);
        sc->layerMode(1);
        break;
      case STREAM_IMAGE:
        numBytes = readPacket();
        sc->hideApp(true, true);
        sc->layerMode(2);
        sc->drawImage((uint16_t *) (buff_ptr + 11), (numBytes - 8) / 2, unpackNumber(buff_ptr, 3, 2), unpackNumber(buff_ptr, 5, 2),
                      unpackNumber(buff_ptr, 7, 2), unpackNumber(buff_ptr, 9, 2));
        Serial1.println(String(STREAM_IMAGE));
        sc->layerMode(1);
        lastStreamTime = millis();
        if(!isStreaming) {
          isStreaming = true;
        }
        break;
      case CLEAR_APP:
        numBytes = readPacket();
        sc->clearApp();
        Serial1.println(String(CLEAR_APP));
        break;
      case APP_TOUCH:
        numBytes = readPacket();
        if(sc->appTouched) {
          buff_ptr[6] = sc->appX;
          buff_ptr[8] = sc->appY;
          Serial1.print(String(APP_TOUCH));
          Serial1.write((uint8_t *)(buff_ptr + 6), 4);
          Serial1.println();
          sc->appTouched = false;
        } else {
          Serial1.println(String(APP_TOUCH) + "X");
        }
        break;
      case SET_IP:
        numBytes = readPacket();
        strcpy(sc->ipaddr, buff_ptr + 3);
        Serial1.println(String(SET_IP));
        break;
      default:
        Serial.println("Error: no such command");
        numBytes = Serial1.readBytes(buff_ptr, COMMAND_BUFFER_SIZE);
    }
  }
}