made progress android example sending remote bin to arduino example
This commit is contained in:
@@ -90,11 +90,9 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
|||||||
|
|
||||||
// define the single instance of IRDecode
|
// define the single instance of IRDecode
|
||||||
private IRDecode mIRDecode;
|
private IRDecode mIRDecode;
|
||||||
|
|
||||||
private CheckBox mCbDecodeOnBoard;
|
|
||||||
private EditText mEtEmitterIp;
|
private EditText mEtEmitterIp;
|
||||||
private ImageButton mBtnConnect;
|
private ImageButton mBtnConnect;
|
||||||
private TextView mTvConnectionStatus;
|
private View mVWConnectStatus;
|
||||||
|
|
||||||
public ControlFragment() {
|
public ControlFragment() {
|
||||||
}
|
}
|
||||||
@@ -133,10 +131,9 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
|||||||
btnPlus.setOnClickListener(this);
|
btnPlus.setOnClickListener(this);
|
||||||
btnMinus.setOnClickListener(this);
|
btnMinus.setOnClickListener(this);
|
||||||
|
|
||||||
mCbDecodeOnBoard = view.findViewById(R.id.cb_use_encoded);
|
|
||||||
mEtEmitterIp = view.findViewById(R.id.emitter_ip);
|
mEtEmitterIp = view.findViewById(R.id.emitter_ip);
|
||||||
mBtnConnect = view.findViewById(R.id.btn_connect_emitter);
|
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() {
|
mBtnConnect.setOnClickListener(new View.OnClickListener() {
|
||||||
|
|
||||||
@@ -261,8 +258,7 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
|||||||
emitterConnected = EMITTER_CONNECTED;
|
emitterConnected = EMITTER_CONNECTED;
|
||||||
mParent.runOnUiThread(() -> {
|
mParent.runOnUiThread(() -> {
|
||||||
mBtnConnect.setImageDrawable(AppCompatResources.getDrawable(mParent, R.mipmap.button_unlink));
|
mBtnConnect.setImageDrawable(AppCompatResources.getDrawable(mParent, R.mipmap.button_unlink));
|
||||||
mTvConnectionStatus.setText(mParent.getString(R.string.status_connected));
|
mVWConnectStatus.setBackgroundColor(Color.parseColor("#3FAFFF"));
|
||||||
mTvConnectionStatus.setTextColor(Color.parseColor("#7F7FFF"));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
private void onEmitterDisconnected() {
|
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);
|
ToastUtils.showToast(mParent, mParent.getString(R.string.connect_disconnected), Toast.LENGTH_SHORT);
|
||||||
}
|
}
|
||||||
mBtnConnect.setImageDrawable(AppCompatResources.getDrawable(mParent, R.mipmap.button_link));
|
mBtnConnect.setImageDrawable(AppCompatResources.getDrawable(mParent, R.mipmap.button_link));
|
||||||
mTvConnectionStatus.setText(mParent.getString(R.string.status_not_connected));
|
mVWConnectStatus.setBackgroundColor(Color.parseColor("#FF7F7F"));
|
||||||
mTvConnectionStatus.setTextColor(Color.parseColor("#FF7F7F"));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
emitterConnected = EMITTER_DISCONNECTED;
|
emitterConnected = EMITTER_DISCONNECTED;
|
||||||
@@ -291,14 +286,10 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void processEBin(String response) {
|
private void processEBin(String response) {
|
||||||
if (mCbDecodeOnBoard.isChecked()) {
|
|
||||||
String binFileName = FileUtils.binDir + FileUtils.FILE_NAME_PREFIX +
|
String binFileName = FileUtils.binDir + FileUtils.FILE_NAME_PREFIX +
|
||||||
mCurrentRemoteControl.getRemoteMap() + FileUtils.FILE_NAME_EXT;
|
mCurrentRemoteControl.getRemoteMap() + FileUtils.FILE_NAME_EXT;
|
||||||
byte []binContent = FileUtils.getByteArrayFromFile(binFileName);
|
byte []binContent = FileUtils.getByteArrayFromFile(binFileName);
|
||||||
sendBinToEmitter(binContent);
|
sendBinToEmitter(binContent);
|
||||||
} else {
|
|
||||||
emitterConnected = EMITTER_BIN_RECEIVED;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processECtrl(String response) {
|
private void processECtrl(String response) {
|
||||||
@@ -395,12 +386,6 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
vibrate(mParent);
|
vibrate(mParent);
|
||||||
|
|
||||||
boolean decodeOnBoard = mCbDecodeOnBoard.isChecked();
|
|
||||||
|
|
||||||
if (decodeOnBoard) {
|
|
||||||
// send control command to emitter
|
|
||||||
} else {
|
|
||||||
// decode directly in mobile phone
|
// decode directly in mobile phone
|
||||||
int []decoded = null;
|
int []decoded = null;
|
||||||
int id = v.getId();
|
int id = v.getId();
|
||||||
@@ -450,7 +435,6 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
|||||||
ToastUtils.showToast(mParent, this.getString(R.string.ir_not_supported), null);
|
ToastUtils.showToast(mParent, this.getString(R.string.ir_not_supported), null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static class MsgHandler extends Handler {
|
private static class MsgHandler extends Handler {
|
||||||
|
|
||||||
|
|||||||
@@ -227,35 +227,11 @@
|
|||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:layout_alignParentBottom="true">
|
android:layout_alignParentBottom="true">
|
||||||
<View
|
<View
|
||||||
|
android:id="@+id/vw_connect_status"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="1dp"
|
android:layout_height="2dp"
|
||||||
android:layout_margin="4dp"
|
android:layout_margin="4dp"
|
||||||
android:background="#7F7F7F"/>
|
android:background="#FF3F3F"/>
|
||||||
<LinearLayout
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:orientation="horizontal">
|
|
||||||
<CheckBox
|
|
||||||
android:id="@+id/cb_use_encoded"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="60dp"
|
|
||||||
android:textSize="18sp"
|
|
||||||
android:layout_marginStart="20dp"
|
|
||||||
android:text="@string/decode_on_board"
|
|
||||||
android:layout_weight="1"
|
|
||||||
android:checked="false"/>
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/tv_connection_status"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="60dp"
|
|
||||||
android:gravity="center_vertical|end"
|
|
||||||
android:layout_marginEnd="20dp"
|
|
||||||
android:layout_weight="1"
|
|
||||||
android:textSize="18sp"
|
|
||||||
android:textColor="#FF7F7F"
|
|
||||||
android:text="@string/status_not_connected"/>
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ auto *aBin = "a_bin";
|
|||||||
auto *eBin = "e_bin";
|
auto *eBin = "e_bin";
|
||||||
auto *aControl = "a_control";
|
auto *aControl = "a_control";
|
||||||
auto *eControl = "e_control";
|
auto *eControl = "e_control";
|
||||||
|
auto *aError = "a_error";
|
||||||
|
auto *eError = "e_error";
|
||||||
|
|
||||||
int status = WL_IDLE_STATUS;
|
int status = WL_IDLE_STATUS;
|
||||||
unsigned long lastStatusCheck = 0;
|
unsigned long lastStatusCheck = 0;
|
||||||
@@ -77,11 +79,11 @@ void printWiFiStatus() {
|
|||||||
Serial.print("SSID: ");
|
Serial.print("SSID: ");
|
||||||
Serial.println(WiFi.SSID());
|
Serial.println(WiFi.SSID());
|
||||||
|
|
||||||
Serial.print("IP Address: ");
|
Serial.print("IP address: ");
|
||||||
Serial.println(ip);
|
Serial.println(ip);
|
||||||
|
|
||||||
const long rssi = WiFi.RSSI();
|
const long rssi = WiFi.RSSI();
|
||||||
Serial.print("Signal Strength (RSSI): ");
|
Serial.print("Signal strength (RSSI): ");
|
||||||
Serial.print(rssi);
|
Serial.print(rssi);
|
||||||
Serial.println(" dBm");
|
Serial.println(" dBm");
|
||||||
|
|
||||||
@@ -110,8 +112,7 @@ void setup() {
|
|||||||
matrix.endText(SCROLL_LEFT);
|
matrix.endText(SCROLL_LEFT);
|
||||||
matrix.endDraw();
|
matrix.endDraw();
|
||||||
|
|
||||||
Serial.println("Wi-Fi: Station Mode");
|
Serial.println("Wi-Fi started in station mode");
|
||||||
|
|
||||||
Serial.print("Attempting to connect to SSID: ");
|
Serial.print("Attempting to connect to SSID: ");
|
||||||
Serial.println(ssid);
|
Serial.println(ssid);
|
||||||
|
|
||||||
@@ -127,14 +128,43 @@ void setup() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void onConnected(WiFiClient *client) {
|
||||||
|
client->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) {
|
void onCommand(const String *command, WiFiClient *client) {
|
||||||
if (command->startsWith(aHello)) {
|
if (command->startsWith(aHello)) {
|
||||||
|
Serial.println("Received hello command");
|
||||||
client->println(eBin);
|
client->println(eBin);
|
||||||
client->flush();
|
client->flush();
|
||||||
} else if (command->startsWith(aBin)) {
|
} else if (command->startsWith(aBin)) {
|
||||||
Serial.println("Received bin command");
|
Serial.println("Received bin command");
|
||||||
Serial.println(*command);
|
if (remoteOpen(command->c_str()) > 0) {
|
||||||
onRemoteBin(command->c_str());
|
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();
|
client = server.available();
|
||||||
if (client.connected()) {
|
if (client.connected()) {
|
||||||
if (false == clientConnected) {
|
if (false == clientConnected) {
|
||||||
client.flush();
|
onConnected(&client);
|
||||||
Serial.println("We have a new client");
|
|
||||||
client.println("e_hello");
|
|
||||||
clientConnected = true;
|
|
||||||
}
|
}
|
||||||
|
clientConnected = true;
|
||||||
if (client.available()) {
|
if (client.available()) {
|
||||||
String received = client.readStringUntil('\n');
|
const String received = client.readStringUntil('\n');
|
||||||
Serial.println(received);
|
Serial.println(received);
|
||||||
onCommand(&received, &client);
|
onCommand(&received, &client);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (clientConnected) {
|
if (clientConnected) {
|
||||||
client.stop();
|
onDisconnected(&client);
|
||||||
Serial.println("Client disconnected");
|
|
||||||
}
|
}
|
||||||
clientConnected = false;
|
clientConnected = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ int remoteBinLen = 0;
|
|||||||
|
|
||||||
|
|
||||||
// public function definitions
|
// public function definitions
|
||||||
int onRemoteBin(const char *binStr) {
|
int remoteOpen(const char *binStr) {
|
||||||
char *aBinCommand[ABIN_COMMAND_SEG];
|
char *aBinCommand[ABIN_COMMAND_SEG];
|
||||||
char *remoteBinStr = nullptr;
|
char *remoteBinStr = nullptr;
|
||||||
int categoryId = 0;
|
int categoryId = 0;
|
||||||
@@ -53,7 +53,8 @@ int onRemoteBin(const char *binStr) {
|
|||||||
|
|
||||||
aBinCommandSeg = splitString(binStr, aBinCommand, ABIN_COMMAND_SEG, ",");
|
aBinCommandSeg = splitString(binStr, aBinCommand, ABIN_COMMAND_SEG, ",");
|
||||||
if (ABIN_COMMAND_SEG != aBinCommandSeg) {
|
if (ABIN_COMMAND_SEG != aBinCommandSeg) {
|
||||||
Serial.println("Invalid aBinCommand");
|
Serial.print("Invalid aBin command: ");
|
||||||
|
Serial.println(binStr);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
categoryId = strtol(aBinCommand[SEG_ABIN_CATE], nullptr, 10);
|
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);
|
remoteBinBase64Len = strtol(aBinCommand[SEG_ABIN_LENGTH], nullptr, 10);
|
||||||
remoteBinStr = aBinCommand[SEG_ABIN_BIN];
|
remoteBinStr = aBinCommand[SEG_ABIN_BIN];
|
||||||
if (remoteBinBase64Len != strlen(remoteBinStr)) {
|
if (remoteBinBase64Len != strlen(remoteBinStr)) {
|
||||||
Serial.println("remoteBin length not correct");
|
Serial.println("Remote bin length not correct");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,19 +70,19 @@ int onRemoteBin(const char *binStr) {
|
|||||||
remoteBin = static_cast<unsigned char*>(malloc(remoteBinLen));
|
remoteBin = static_cast<unsigned char*>(malloc(remoteBinLen));
|
||||||
char debugStr[129];
|
char debugStr[129];
|
||||||
if (nullptr == remoteBin) {
|
if (nullptr == remoteBin) {
|
||||||
Serial.println("Not enough memory for remoteBin");
|
Serial.println("Not enough memory for remote bin");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
Serial.print("Remote bin length = ");
|
|
||||||
Serial.println(remoteBinLen);
|
|
||||||
memset(remoteBin, 0, remoteBinLen);
|
memset(remoteBin, 0, remoteBinLen);
|
||||||
|
|
||||||
if (remoteBinLen != base64_decode(reinterpret_cast<char*>(remoteBin), remoteBinStr, remoteBinBase64Len)) {
|
if (remoteBinLen != base64_decode(reinterpret_cast<char*>(remoteBin), remoteBinStr, remoteBinBase64Len)) {
|
||||||
Serial.println("Base64 decode failed");
|
Serial.println("Failed to decode remote bin");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined REMOTE_BIN_DEBUG
|
#if defined REMOTE_BIN_DEBUG
|
||||||
|
Serial.print("Remote bin length = ");
|
||||||
|
Serial.println(remoteBinLen);
|
||||||
snprintf(debugStr, 128, "%02x %02x %02x %02x %02x %02x %02x %02x",
|
snprintf(debugStr, 128, "%02x %02x %02x %02x %02x %02x %02x %02x",
|
||||||
remoteBin[0], remoteBin[1], remoteBin[2], remoteBin[3],
|
remoteBin[0], remoteBin[1], remoteBin[2], remoteBin[3],
|
||||||
remoteBin[4], remoteBin[5], remoteBin[6], remoteBin[7]);
|
remoteBin[4], remoteBin[5], remoteBin[6], remoteBin[7]);
|
||||||
@@ -93,11 +94,18 @@ int onRemoteBin(const char *binStr) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (IR_DECODE_FAILED == ir_binary_open(categoryId, subCateId, remoteBin, remoteBinLen)) {
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
Serial.println("ir_binary_open success");
|
Serial.println("Remote bin loaded successfully");
|
||||||
ir_close();
|
|
||||||
|
|
||||||
return remoteBinLen;
|
return remoteBinLen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int remoteControl(const char *controlStr) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void remoteClose() {
|
||||||
|
ir_close();
|
||||||
|
}
|
||||||
@@ -24,8 +24,12 @@
|
|||||||
#ifndef ARDUINO_EXAMPLE_REMOTE_H
|
#ifndef ARDUINO_EXAMPLE_REMOTE_H
|
||||||
#define 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
|
#endif //ARDUINO_EXAMPLE_REMOTE_H
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#include <string.h>
|
#include <cstring>
|
||||||
|
|
||||||
|
|
||||||
// public function definitions
|
// public function definitions
|
||||||
|
|||||||
@@ -25,6 +25,6 @@
|
|||||||
#define ARDUINO_EXAMPLE_UTILS_H
|
#define ARDUINO_EXAMPLE_UTILS_H
|
||||||
|
|
||||||
int splitString(const char *str, char *parts[],
|
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
|
#endif //ARDUINO_EXAMPLE_UTILS_H
|
||||||
Reference in New Issue
Block a user