diff --git a/android-example/app/src/main/java/net/irext/ircontrol/ui/fragment/ControlFragment.java b/android-example/app/src/main/java/net/irext/ircontrol/ui/fragment/ControlFragment.java index e706e17..09af0f4 100644 --- a/android-example/app/src/main/java/net/irext/ircontrol/ui/fragment/ControlFragment.java +++ b/android-example/app/src/main/java/net/irext/ircontrol/ui/fragment/ControlFragment.java @@ -90,11 +90,9 @@ public class ControlFragment extends Fragment implements View.OnClickListener { // define the single instance of IRDecode private IRDecode mIRDecode; - - private CheckBox mCbDecodeOnBoard; private EditText mEtEmitterIp; private ImageButton mBtnConnect; - private TextView mTvConnectionStatus; + private View mVWConnectStatus; public ControlFragment() { } @@ -133,10 +131,9 @@ public class ControlFragment extends Fragment implements View.OnClickListener { btnPlus.setOnClickListener(this); btnMinus.setOnClickListener(this); - mCbDecodeOnBoard = view.findViewById(R.id.cb_use_encoded); mEtEmitterIp = view.findViewById(R.id.emitter_ip); mBtnConnect = view.findViewById(R.id.btn_connect_emitter); - mTvConnectionStatus = view.findViewById(R.id.tv_connection_status); + mVWConnectStatus = view.findViewById(R.id.vw_connect_status); mBtnConnect.setOnClickListener(new View.OnClickListener() { @@ -261,8 +258,7 @@ public class ControlFragment extends Fragment implements View.OnClickListener { emitterConnected = EMITTER_CONNECTED; mParent.runOnUiThread(() -> { mBtnConnect.setImageDrawable(AppCompatResources.getDrawable(mParent, R.mipmap.button_unlink)); - mTvConnectionStatus.setText(mParent.getString(R.string.status_connected)); - mTvConnectionStatus.setTextColor(Color.parseColor("#7F7FFF")); + mVWConnectStatus.setBackgroundColor(Color.parseColor("#3FAFFF")); }); } private void onEmitterDisconnected() { @@ -279,8 +275,7 @@ public class ControlFragment extends Fragment implements View.OnClickListener { ToastUtils.showToast(mParent, mParent.getString(R.string.connect_disconnected), Toast.LENGTH_SHORT); } mBtnConnect.setImageDrawable(AppCompatResources.getDrawable(mParent, R.mipmap.button_link)); - mTvConnectionStatus.setText(mParent.getString(R.string.status_not_connected)); - mTvConnectionStatus.setTextColor(Color.parseColor("#FF7F7F")); + mVWConnectStatus.setBackgroundColor(Color.parseColor("#FF7F7F")); }); emitterConnected = EMITTER_DISCONNECTED; @@ -291,14 +286,10 @@ public class ControlFragment extends Fragment implements View.OnClickListener { } private void processEBin(String response) { - if (mCbDecodeOnBoard.isChecked()) { - String binFileName = FileUtils.binDir + FileUtils.FILE_NAME_PREFIX + - mCurrentRemoteControl.getRemoteMap() + FileUtils.FILE_NAME_EXT; - byte []binContent = FileUtils.getByteArrayFromFile(binFileName); - sendBinToEmitter(binContent); - } else { - emitterConnected = EMITTER_BIN_RECEIVED; - } + String binFileName = FileUtils.binDir + FileUtils.FILE_NAME_PREFIX + + mCurrentRemoteControl.getRemoteMap() + FileUtils.FILE_NAME_EXT; + byte []binContent = FileUtils.getByteArrayFromFile(binFileName); + sendBinToEmitter(binContent); } private void processECtrl(String response) { @@ -395,60 +386,53 @@ public class ControlFragment extends Fragment implements View.OnClickListener { @Override public void onClick(View v) { vibrate(mParent); + // decode directly in mobile phone + int []decoded = null; + int id = v.getId(); + if (id == R.id.iv_power) { + decoded = irControl(KEY_POWER); + } else if (id == R.id.iv_up) { + decoded = irControl(KEY_UP); + } else if (id == R.id.iv_down) { + decoded = irControl(KEY_DOWN); + } else if (id == R.id.iv_left) { + decoded = irControl(KEY_LEFT); + } else if (id == R.id.iv_right) { + decoded = irControl(KEY_RIGHT); + } else if (id == R.id.iv_ok) { + decoded = irControl(KEY_OK); + } else if (id == R.id.iv_plus) { + decoded = irControl(KEY_PLUS); + } else if (id == R.id.iv_minus) { + decoded = irControl(KEY_MINUS); + } else if (id == R.id.iv_back) { + decoded = irControl(KEY_BACK); + } else if (id == R.id.iv_home) { + decoded = irControl(KEY_HOME); + } else if (id == R.id.iv_menu) { + decoded = irControl(KEY_MENU); + } - boolean decodeOnBoard = mCbDecodeOnBoard.isChecked(); - - if (decodeOnBoard) { - // send control command to emitter + // debug decoded value + StringBuilder decodedValue = new StringBuilder(); + for (int i = 0; i < Objects.requireNonNull(decoded).length; i++) { + decodedValue.append(decoded[i]); + decodedValue.append(","); + } + Log.d(TAG, "decodedValue : " + decodedValue); + if (EMITTER_AVAILABLE == emitterConnected) { + Log.d(TAG, "emitter available, send decoded to emitter"); + sendDecodedToEmitter(decodedValue.toString()); + } + // send decoded integer array to IR emitter + ConsumerIrManager irEmitter = + (ConsumerIrManager) mParent.getSystemService(Context.CONSUMER_IR_SERVICE); + if (null != irEmitter && irEmitter.hasIrEmitter()) { + if (decoded.length > 0) { + irEmitter.transmit(38000, decoded); + } } else { - // decode directly in mobile phone - int []decoded = null; - int id = v.getId(); - if (id == R.id.iv_power) { - decoded = irControl(KEY_POWER); - } else if (id == R.id.iv_up) { - decoded = irControl(KEY_UP); - } else if (id == R.id.iv_down) { - decoded = irControl(KEY_DOWN); - } else if (id == R.id.iv_left) { - decoded = irControl(KEY_LEFT); - } else if (id == R.id.iv_right) { - decoded = irControl(KEY_RIGHT); - } else if (id == R.id.iv_ok) { - decoded = irControl(KEY_OK); - } else if (id == R.id.iv_plus) { - decoded = irControl(KEY_PLUS); - } else if (id == R.id.iv_minus) { - decoded = irControl(KEY_MINUS); - } else if (id == R.id.iv_back) { - decoded = irControl(KEY_BACK); - } else if (id == R.id.iv_home) { - decoded = irControl(KEY_HOME); - } else if (id == R.id.iv_menu) { - decoded = irControl(KEY_MENU); - } - - // debug decoded value - StringBuilder decodedValue = new StringBuilder(); - for (int i = 0; i < Objects.requireNonNull(decoded).length; i++) { - decodedValue.append(decoded[i]); - decodedValue.append(","); - } - Log.d(TAG, "decodedValue : " + decodedValue); - if (EMITTER_AVAILABLE == emitterConnected) { - Log.d(TAG, "emitter available, send decoded to emitter"); - sendDecodedToEmitter(decodedValue.toString()); - } - // send decoded integer array to IR emitter - ConsumerIrManager irEmitter = - (ConsumerIrManager) mParent.getSystemService(Context.CONSUMER_IR_SERVICE); - if (null != irEmitter && irEmitter.hasIrEmitter()) { - if (decoded.length > 0) { - irEmitter.transmit(38000, decoded); - } - } else { - ToastUtils.showToast(mParent, this.getString(R.string.ir_not_supported), null); - } + ToastUtils.showToast(mParent, this.getString(R.string.ir_not_supported), null); } } diff --git a/android-example/app/src/main/res/layout/fragment_control.xml b/android-example/app/src/main/res/layout/fragment_control.xml index 055f0ba..89a7679 100644 --- a/android-example/app/src/main/res/layout/fragment_control.xml +++ b/android-example/app/src/main/res/layout/fragment_control.xml @@ -227,35 +227,11 @@ android:orientation="vertical" android:layout_alignParentBottom="true"> - - - - - + android:background="#FF3F3F"/> flush(); + Serial.println("Client connected"); + client->println(eHello); +} + +void onDisconnected(WiFiClient *client) { + remoteClose(); + client->flush(); + client->stop(); + Serial.println("Client disconnected"); +} + +void onError(WiFiClient *client) { + client->flush(); + client->stop(); +} + void onCommand(const String *command, WiFiClient *client) { if (command->startsWith(aHello)) { + Serial.println("Received hello command"); client->println(eBin); client->flush(); } else if (command->startsWith(aBin)) { Serial.println("Received bin command"); - Serial.println(*command); - onRemoteBin(command->c_str()); + if (remoteOpen(command->c_str()) > 0) { + client->println(eControl); + } else { + Serial.println("Failed to parse bin command"); + client->println(eError); + } + } else if (command->startsWith(aControl)) { + Serial.println("Received control command"); + remoteControl(command->c_str()); + } else if (command->startsWith(aError)) { + Serial.println("Received error command"); + onError(client); } } @@ -150,21 +180,17 @@ void loop() { client = server.available(); if (client.connected()) { if (false == clientConnected) { - client.flush(); - Serial.println("We have a new client"); - client.println("e_hello"); - clientConnected = true; + onConnected(&client); } - + clientConnected = true; if (client.available()) { - String received = client.readStringUntil('\n'); + const String received = client.readStringUntil('\n'); Serial.println(received); onCommand(&received, &client); } } else { if (clientConnected) { - client.stop(); - Serial.println("Client disconnected"); + onDisconnected(&client); } clientConnected = false; } diff --git a/arduino-example/src/remote.cpp b/arduino-example/src/remote.cpp index cd917ef..fcf1fac 100644 --- a/arduino-example/src/remote.cpp +++ b/arduino-example/src/remote.cpp @@ -43,7 +43,7 @@ int remoteBinLen = 0; // public function definitions -int onRemoteBin(const char *binStr) { +int remoteOpen(const char *binStr) { char *aBinCommand[ABIN_COMMAND_SEG]; char *remoteBinStr = nullptr; int categoryId = 0; @@ -53,7 +53,8 @@ int onRemoteBin(const char *binStr) { aBinCommandSeg = splitString(binStr, aBinCommand, ABIN_COMMAND_SEG, ","); if (ABIN_COMMAND_SEG != aBinCommandSeg) { - Serial.println("Invalid aBinCommand"); + Serial.print("Invalid aBin command: "); + Serial.println(binStr); return -1; } categoryId = strtol(aBinCommand[SEG_ABIN_CATE], nullptr, 10); @@ -61,7 +62,7 @@ int onRemoteBin(const char *binStr) { remoteBinBase64Len = strtol(aBinCommand[SEG_ABIN_LENGTH], nullptr, 10); remoteBinStr = aBinCommand[SEG_ABIN_BIN]; if (remoteBinBase64Len != strlen(remoteBinStr)) { - Serial.println("remoteBin length not correct"); + Serial.println("Remote bin length not correct"); return -1; } @@ -69,19 +70,19 @@ int onRemoteBin(const char *binStr) { remoteBin = static_cast(malloc(remoteBinLen)); char debugStr[129]; if (nullptr == remoteBin) { - Serial.println("Not enough memory for remoteBin"); + Serial.println("Not enough memory for remote bin"); return -1; } - Serial.print("Remote bin length = "); - Serial.println(remoteBinLen); memset(remoteBin, 0, remoteBinLen); if (remoteBinLen != base64_decode(reinterpret_cast(remoteBin), remoteBinStr, remoteBinBase64Len)) { - Serial.println("Base64 decode failed"); + Serial.println("Failed to decode remote bin"); return -1; } #if defined REMOTE_BIN_DEBUG + Serial.print("Remote bin length = "); + Serial.println(remoteBinLen); snprintf(debugStr, 128, "%02x %02x %02x %02x %02x %02x %02x %02x", remoteBin[0], remoteBin[1], remoteBin[2], remoteBin[3], remoteBin[4], remoteBin[5], remoteBin[6], remoteBin[7]); @@ -93,11 +94,18 @@ int onRemoteBin(const char *binStr) { #endif if (IR_DECODE_FAILED == ir_binary_open(categoryId, subCateId, remoteBin, remoteBinLen)) { - Serial.println("ir_binary_open failed"); + Serial.println("Failed to load remote bin"); return -1; } - Serial.println("ir_binary_open success"); - ir_close(); + Serial.println("Remote bin loaded successfully"); return remoteBinLen; +} + +int remoteControl(const char *controlStr) { + return 0; +} + +void remoteClose() { + ir_close(); } \ No newline at end of file diff --git a/arduino-example/src/remote.h b/arduino-example/src/remote.h index d43444d..29ed1c5 100644 --- a/arduino-example/src/remote.h +++ b/arduino-example/src/remote.h @@ -24,8 +24,12 @@ #ifndef ARDUINO_EXAMPLE_REMOTE_H #define ARDUINO_EXAMPLE_REMOTE_H -// #define REMOTE_BIN_DEBUG (1) +#define REMOTE_BIN_DEBUG (1) -int onRemoteBin(const char *binStr); +int remoteOpen(const char *binStr); + +int remoteControl(const char *controlStr); + +void remoteClose(); #endif //ARDUINO_EXAMPLE_REMOTE_H \ No newline at end of file diff --git a/arduino-example/src/utils.cpp b/arduino-example/src/utils.cpp index 1bff719..e7f72e8 100644 --- a/arduino-example/src/utils.cpp +++ b/arduino-example/src/utils.cpp @@ -23,7 +23,7 @@ #include -#include +#include // public function definitions diff --git a/arduino-example/src/utils.h b/arduino-example/src/utils.h index b28dbc4..3a4b885 100644 --- a/arduino-example/src/utils.h +++ b/arduino-example/src/utils.h @@ -25,6 +25,6 @@ #define ARDUINO_EXAMPLE_UTILS_H int splitString(const char *str, char *parts[], - const int parts_max, const char *delimiter); + int parts_max, const char *delimiter); #endif //ARDUINO_EXAMPLE_UTILS_H \ No newline at end of file