waveshare: clean code

This commit is contained in:
Philippe Teuwen 2020-09-13 03:10:33 +02:00
commit f828240af4

View file

@ -223,7 +223,9 @@ static void read_black(uint32_t i, uint8_t *l, uint8_t model_nr, uint8_t *black)
}
static void read_red(uint32_t i, uint8_t *l, uint8_t model_nr, uint8_t *red) {
for (uint8_t j = 0; j < models[model_nr].len; j++) {
if (model_nr == M1in54B) {
if (red == NULL) {
l[3 + j] = ~0x00;
} else if (model_nr == M1in54B) {
//1.54B needs to flip the red picture data, other screens do not need to flip data
l[3 + j] = ~red[i * models[model_nr].len + j];
} else {
@ -232,26 +234,43 @@ static void read_red(uint32_t i, uint8_t *l, uint8_t model_nr, uint8_t *red) {
}
}
static void transceive_blocking( uint8_t* txBuf, uint16_t txBufLen, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* actLen, uint32_t fwt ){
*actLen = 2;
(void) fwt;
static int transceive_blocking( uint8_t* txBuf, uint16_t txBufLen, uint8_t* rxBuf, uint16_t rxBufLen, uint16_t* actLen, bool retransmit ){
uint8_t fail_num = 0;
if (rxBufLen < 2)
return PM3_EINVARG;
while (1) {
PacketResponseNG resp;
SendCommandMIX(CMD_HF_ISO14443A_READER, ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT, txBufLen, 0, txBuf, txBufLen);
WaitForResponse(CMD_ACK, &resp);
rxBuf[0] = 1;
if (WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {
if (resp.oldarg[0] > rxBufLen) {
PrintAndLogEx(WARNING, "Received % bytes, rxBuf too small (%)", resp.oldarg[0], rxBufLen);
memcpy(rxBuf, resp.data.asBytes, rxBufLen);
*actLen = rxBufLen;
return;
return PM3_ESOFT;
}
memcpy(rxBuf, resp.data.asBytes, resp.oldarg[0]);
*actLen = resp.oldarg[0];
}
if ((retransmit) && (rxBuf[0] != 0 || rxBuf[1] != 0)) {
fail_num++;
if (fail_num > 10) {
PROMPT_CLEARLINE;
PrintAndLogEx(WARNING, "Transmission failed, please try again.");
DropField();
return PM3_ESOFT;
}
} else {
break;
}
}
return PM3_SUCCESS;
}
// 1.54B Keychain
// 1.54B does not share the common base and requires specific handling
static int start_drawing_1in54B(uint8_t model_nr, uint8_t *black, uint8_t *red, uint8_t fail_num) {
uint8_t step = 5;
static int start_drawing_1in54B(uint8_t model_nr, uint8_t *black, uint8_t *red) {
int ret;
uint8_t step_5[128] = {0xcd, 0x05, 100};
uint8_t step_4[2] = {0xcd, 0x04};
uint8_t step_6[2] = {0xcd, 0x06};
@ -261,81 +280,49 @@ static int start_drawing_1in54B(uint8_t model_nr, uint8_t *black, uint8_t *red,
if (model_nr == M1in54B) {
step_5[2] = 100;
}
while (1) {
if (step == 5) {
PrintAndLogEx(INFO, "1.54_Step9: e-paper config2 (black)");
if (model_nr == M1in54B) { //1.54inch B Keychain
for (i = 0; i < 50; i++) {
rx[0] = 1;
rx[1] = 1;
read_black(i, step_5, model_nr, black);
transceive_blocking(step_5, 103, rx, 20, actrxlen, 2157 + 2048); // cd 05
ret = transceive_blocking(step_5, 103, rx, 20, actrxlen, true); // cd 05
if (ret != PM3_SUCCESS) {
return ret;
}
progress = i * 100 / 100;
PrintAndLogEx(INPLACE, "Progress: %d %%", progress);
}
}
PROMPT_CLEARLINE;
step = 6;
} else if (step == 6) {
PrintAndLogEx(INFO, "1.54_Step6: e-paper power on");
transceive_blocking(step_4, 2, rx, 20, actrxlen, 2157 + 2048); //cd 04
step = 7;
if (rx[0] == 0 && rx[1] == 0) {
step = 7;
} else {
fail_num++;
if (fail_num > 10) {
PrintAndLogEx(WARNING, "Update failed, please press any key to exit and try again.");
step = 14;
fail_num = 0;
msleep(200);
ret = transceive_blocking(step_4, 2, rx, 20, actrxlen, true); //cd 04
if (ret != PM3_SUCCESS) {
return ret;
}
}
} else if (step == 7) {
PrintAndLogEx(INFO, "1.54_Step7: e-paper config2 (red)");
if (model_nr == M1in54B) { //1.54inch B Keychain
for (i = 0; i < 50; i++) {
rx[0] = 1;
rx[1] = 1;
read_red(i, step_5, model_nr, red);
transceive_blocking(step_5, 103, rx, 20, actrxlen, 2157 + 2048); // cd 05
ret = transceive_blocking(step_5, 103, rx, 20, actrxlen, true); // cd 05
if (ret != PM3_SUCCESS) {
return ret;
}
progress = i * 100 / 100 + 50;
PrintAndLogEx(INPLACE, "Progress: %d %%", progress);
}
}
PROMPT_CLEARLINE;
step = 8;
rx[0] = 1;
rx[1] = 1;
} else if (step == 8) {
// Send update instructions
PrintAndLogEx(INFO, "1.54_Step8: EDP load to main");
transceive_blocking(step_6, 2, rx, 20, actrxlen, 2157 + 2048); //cd 06
if (rx[0] == 0 && rx[1] == 0) {
rx[0] = 1;
rx[1] = 1;
step = 9;
} else {
fail_num++;
if (fail_num > 10) {
PrintAndLogEx(WARNING, "Update failed, please press any key to exit and try again.");
step = 14;
fail_num = 0;
msleep(200);
ret = transceive_blocking(step_6, 2, rx, 20, actrxlen, true); //cd 06
if (ret != PM3_SUCCESS) {
return ret;
}
}
} else if (step == 9) {
PrintAndLogEx(INFO, "1.54_Step9");
return PM3_SUCCESS;
} else if (step == 14) {
return PM3_ESOFT;
}
}
}
static int start_drawing(uint8_t model_nr, uint8_t *black, uint8_t *red) {
uint8_t fail_num = 0;
uint8_t step = 0, progress = 0;
uint8_t progress = 0;
uint8_t step0[2] = {0xcd, 0x0d};
uint8_t step1[3] = {0xcd, 0x00, 10}; //select e-paper type and reset e-paper 4:2.13inch e-Paper 7:2.9inch e-Paper 10:4.2inch e-Paper 14:7.5inch e-Paper
uint8_t step2[2] = {0xcd, 0x01}; //e-paper normal mode type
@ -386,25 +373,13 @@ static int start_drawing(uint8_t model_nr, uint8_t *black, uint8_t *red) {
DropField();
return PM3_ESOFT;
}
PrintAndLogEx(DEBUG, "model_nr = %d", model_nr);
while (1) {
if (step == 0) {
int ret;
PrintAndLogEx(INFO, "Step0");
transceive_blocking(step0, 2, rx, 20, actrxlen, 2157 + 2048); //cd 0d
if (rx[0] == 0 && rx[1] == 0) {
rx[0] = 1;
rx[1] = 1;
step = 1;
} else {
fail_num++;
if (fail_num > 10) {
PrintAndLogEx(WARNING, "Update failed, please try again.");
DropField();
return PM3_ESOFT;
ret = transceive_blocking(step0, 2, rx, 20, actrxlen, true); //cd 0d
if (ret != PM3_SUCCESS) {
return ret;
}
}
} else if (step == 1) {
PrintAndLogEx(INFO, "Step1: e-paper config");
//step1[2] screen model
//step8[2] nr of bytes sent at once
@ -443,274 +418,203 @@ static int start_drawing(uint8_t model_nr, uint8_t *black, uint8_t *red) {
}
if (model_nr == M1in54B) {
transceive_blocking(step1, 2, rx, 20, actrxlen, 2157 + 2048); //cd 00
ret = transceive_blocking(step1, 2, rx, 20, actrxlen, true); //cd 00
} else {
transceive_blocking(step1, 3, rx, 20, actrxlen, 2157 + 2048);
ret = transceive_blocking(step1, 3, rx, 20, actrxlen, true);
}
if (ret != PM3_SUCCESS) {
return ret;
}
if (rx[0] == 0 && rx[1] == 0) {
rx[0] = 1;
rx[1] = 1;
step = 2;
fail_num = 0;
msleep(100);
} else {
fail_num++;
if (fail_num > 10) {
PrintAndLogEx(WARNING, "Update failed, please try again.");
DropField();
return PM3_ESOFT;
}
}
msleep(10);
} else if (step == 2) {
PrintAndLogEx(INFO, "Step2: e-paper normal mode type");
transceive_blocking(step2, 2, rx, 20, actrxlen, 2157 + 2048); //cd 01
if (rx[0] == 0 && rx[1] == 0) {
rx[0] = 1;
rx[1] = 1;
step = 3;
fail_num = 0;
msleep(100);
} else {
fail_num++;
if (fail_num > 50) {
PrintAndLogEx(WARNING, "Update failed, please try again.");
DropField();
return PM3_ESOFT;
}
ret = transceive_blocking(step2, 2, rx, 20, actrxlen, true); //cd 01
if (ret != PM3_SUCCESS) {
return ret;
}
msleep(100);
} else if (step == 3) {
PrintAndLogEx(INFO, "Step3: e-paper config1");
transceive_blocking(step3, 2, rx, 20, actrxlen, 2157 + 2048); //cd 02
if (rx[0] == 0 && rx[1] == 0) {
rx[0] = 1;
rx[1] = 1;
step = 4;
fail_num = 0;
} else {
fail_num++;
if (fail_num > 10) {
PrintAndLogEx(WARNING, "Update failed, please try again.");
DropField();
return PM3_ESOFT;
}
ret = transceive_blocking(step3, 2, rx, 20, actrxlen, true); //cd 02
if (ret != PM3_SUCCESS) {
return ret;
}
msleep(200);
} else if (step == 4) {
PrintAndLogEx(INFO, "Step4: e-paper power on");
transceive_blocking(step4, 2, rx, 20, actrxlen, 2157 + 2048); //cd 03
ret = transceive_blocking(step4, 2, rx, 20, actrxlen, true); //cd 03
if (ret != PM3_SUCCESS) {
return ret;
}
if (model_nr == M1in54B) {
// 1.54B Keychain handler
PrintAndLogEx(DEBUG, "Start_Drawing_1in54B");
char t = start_drawing_1in54B(model_nr, black, red, fail_num);
if (t == 0) {
step = 11;
ret = start_drawing_1in54B(model_nr, black, red);
if (ret != PM3_SUCCESS) {
return ret;
}
//1.54B Data transfer is complete and wait for refresh
} else if (t == 1) {
step = 14;
//1.54B Data transmission error
}
// 1.54B Keychain handler end
}
if (rx[0] == 0 && rx[1] == 0) {
fail_num = 0;
step = 5;
} else {
fail_num++;
if (fail_num > 10) {
PrintAndLogEx(WARNING, "Update failed, please try again.");
DropField();
return PM3_ESOFT;
}
}
} else if (step == 5) {
PrintAndLogEx(INFO, "Step5: e-paper config2");
transceive_blocking(step5, 2, rx, 20, actrxlen, 2157 + 2048); //cd 05
if (rx[0] == 0 && rx[1] == 0) {
rx[0] = 1;
rx[1] = 1;
step = 6;
fail_num = 0;
ret = transceive_blocking(step5, 2, rx, 20, actrxlen, true); //cd 05
if (ret != PM3_SUCCESS) {
return ret;
}
msleep(100);
} else {
fail_num++;
if (fail_num > 30) {
PrintAndLogEx(WARNING, "Update failed, please try again.");
DropField();
return PM3_ESOFT;
}
}
msleep(10);
} else if (step == 6) {
PrintAndLogEx(INFO, "Step6: EDP load to main") ;
transceive_blocking(step6, 2, rx, 20, actrxlen, 2157 + 2048); //cd 06
if (rx[0] == 0 && rx[1] == 0) {
rx[0] = 1;
rx[1] = 1;
step = 7;
fail_num = 0;
ret = transceive_blocking(step6, 2, rx, 20, actrxlen, true); //cd 06
if (ret != PM3_SUCCESS) {
return ret;
}
msleep(100);
} else {
fail_num++;
if (fail_num > 10) {
PrintAndLogEx(WARNING, "Update failed, please try again.");
DropField();
return PM3_ESOFT;
}
}
} else if (step == 7) {
PrintAndLogEx(INFO, "Step7: Data preparation");
transceive_blocking(step7, 2, rx, 20, actrxlen, 2157 + 2048); //cd 07
if (rx[0] == 0 && rx[1] == 0) {
rx[0] = 1;
rx[1] = 1;
step = 8;
fail_num = 0;
} else {
fail_num++;
if (fail_num > 10) {
PrintAndLogEx(WARNING, "Update failed, please try again.");
DropField();
return PM3_ESOFT;
ret = transceive_blocking(step7, 2, rx, 20, actrxlen, true); //cd 07
if (ret != PM3_SUCCESS) {
return ret;
}
}
} else if (step == 8) { //cd 08
PrintAndLogEx(INFO, "Step8: Start data transfer");
if (model_nr == M2in13) { //2.13inch
for (i = 0; i < 250; i++) {
rx[0] = 1;
rx[1] = 1;
read_black(i, step8, model_nr, black);
transceive_blocking(step8, 19, rx, 20, actrxlen, 2157 + 2048);
ret = transceive_blocking(step8, 19, rx, 20, actrxlen, true); // cd 08
if (ret != PM3_SUCCESS) {
return ret;
}
progress = i * 100 / 250;
PrintAndLogEx(INPLACE, "Progress: %d %%", progress);
}
} else if (model_nr == M2in9) {
for (i = 0; i < 296; i++) {
rx[0] = 1;
rx[1] = 1;
read_black(i, step8, model_nr, black);
transceive_blocking(step8, 19, rx, 20, actrxlen, 2157 + 2048);
ret = transceive_blocking(step8, 19, rx, 20, actrxlen, true); // cd 08
if (ret != PM3_SUCCESS) {
return ret;
}
progress = i * 100 / 296;
PrintAndLogEx(INPLACE, "Progress: %d %%", progress);
}
} else if (model_nr == M4in2) { //4.2inch
for (i = 0; i < 150; i++) {
rx[0] = 1;
rx[1] = 1;
read_black(i, step8, model_nr, black);
transceive_blocking(step8, 103, rx, 20, actrxlen, 2157 + 2048);
ret = transceive_blocking(step8, 103, rx, 20, actrxlen, true); // cd 08
if (ret != PM3_SUCCESS) {
return ret;
}
progress = i * 100 / 150;
PrintAndLogEx(INPLACE, "Progress: %d %%", progress);
}
} else if (model_nr == M7in5) { //7.5inch
for (i = 0; i < 400; i++) {
rx[0] = 1;
rx[1] = 1;
read_black(i, step8, model_nr, black);
transceive_blocking(step8, 123, rx, 20, actrxlen, 2157 + 2048);
ret = transceive_blocking(step8, 123, rx, 20, actrxlen, true); // cd 08
if (ret != PM3_SUCCESS) {
return ret;
}
progress = i * 100 / 400;
PrintAndLogEx(INPLACE, "Progress: %d %%", progress);
msleep(6);
}
} else if (model_nr == M2in13B) { //2.13inch B
for (i = 0; i < 26; i++) {
rx[0] = 1;
rx[1] = 1;
read_black(i, step8, model_nr, black);
transceive_blocking(step8, 109, rx, 20, actrxlen, 2157 + 2048);
ret = transceive_blocking(step8, 109, rx, 20, actrxlen, true); // cd 08
if (ret != PM3_SUCCESS) {
return ret;
}
progress = i * 50 / 26;
PrintAndLogEx(INPLACE, "Progress: %d %%", progress);
}
} else if (model_nr == M7in5HD) { //7.5HD
for (i = 0; i < 484; i++) {
rx[0] = 1;
rx[1] = 1;
read_black(i, step8, model_nr, black);
//memset(&step8[3], 0xf0, 120);
transceive_blocking(step8, 123, rx, 20, actrxlen, 2157 + 2048);
ret = transceive_blocking(step8, 123, rx, 20, actrxlen, true); // cd 08
if (ret != PM3_SUCCESS) {
return ret;
}
progress = i * 100 / 484;
PrintAndLogEx(INPLACE, "Progress: %d %%", progress);
}
memset(&step8[3], 0xff, 120);
transceive_blocking(step8, 110 + 3, rx, 20, actrxlen, 2157 + 2048);
ret = transceive_blocking(step8, 110 + 3, rx, 20, actrxlen, true); // cd 08
if (ret != PM3_SUCCESS) {
return ret;
}
} else if (model_nr == M2in7) { //2.7inch
for (i = 0; i < 48; i++) {
rx[0] = 1;
rx[1] = 1;
//read_black(i,step8, model_nr, black);
memset(&step8[3], 0xFF, sizeof(step8)-3);
transceive_blocking(step8, 124, rx, 20, actrxlen, 2157 + 2048);
ret = transceive_blocking(step8, 124, rx, 20, actrxlen, true); // cd 08
if (ret != PM3_SUCCESS) {
return ret;
}
progress = i * 50 / 48;
PrintAndLogEx(INPLACE, "Progress: %d %%", progress);
}
}
PROMPT_CLEARLINE;
step = 9;
} else if (step == 9) {
PrintAndLogEx(INFO, "Step9: e-paper power on");
if (model_nr == M2in13 || model_nr == M2in9 || model_nr == M4in2 || model_nr == M7in5 || model_nr == M7in5HD) {
transceive_blocking(step9, 2, rx, 20, actrxlen, 2157 + 2048); //cd 18
ret = transceive_blocking(step9, 2, rx, 20, actrxlen, true); //cd 18
// The black-and-white screen sending backplane is also shielded, with no effect. Except 2.7
if (rx[0] != 0 || rx[1] != 0) {
fail_num++;
if (fail_num > 10) {
PrintAndLogEx(WARNING, "Update failed, please try again.");
DropField();
return PM3_ESOFT;
if (ret != PM3_SUCCESS) {
return ret;
}
} else
fail_num = 0;
rx[0] = 1;
rx[1] = 1;
step = 10;
} else if (model_nr == M2in13B || model_nr == M2in7) {
transceive_blocking(step9, 2, rx, 20, actrxlen, 2157 + 2048); //cd 18
//rx[0]=1;rx[1]=1;
step = 19;
ret = transceive_blocking(step9, 2, rx, 20, actrxlen, true); //cd 18
if (ret != PM3_SUCCESS) {
return ret;
}
PrintAndLogEx(INFO, "Step9b");
if (model_nr == M2in7) {
for (i = 0; i < 48; i++) {
read_black(i, step13, model_nr, black);
ret = transceive_blocking(step13, 124, rx, 20, actrxlen, true); //CD 19
if (ret != PM3_SUCCESS) {
return ret;
}
progress = i * 50 / 48 + 50;
PrintAndLogEx(INPLACE, "Progress: %d %%", progress);
}
} else if (model_nr == M2in13B) {
for (i = 0; i < 26; i++) {
read_red(i, step13, model_nr, red);
//memset(&step13[3], 0xfE, 106);
ret = transceive_blocking(step13, 109, rx, 20, actrxlen, true);
if (ret != PM3_SUCCESS) {
return ret;
}
progress = i * 50 / 26 + 50;
PrintAndLogEx(INPLACE, "Progress: %d %%", progress);
}
}
PROMPT_CLEARLINE;
}
} else if (step == 10) {
PrintAndLogEx(INFO, "Step10: Refresh e-paper");
transceive_blocking(step10, 2, rx, 20, actrxlen, 2157 + 2048); //cd 09 refresh command
if (rx[0] != 0 || rx[1] != 0) {
fail_num++;
if (fail_num > 10) {
PrintAndLogEx(WARNING, "Update failed, please try again.");
DropField();
return PM3_ESOFT;
ret = transceive_blocking(step10, 2, rx, 20, actrxlen, true); //cd 09 refresh command
if (ret != PM3_SUCCESS) {
return ret;
}
} else
fail_num = 0;
rx[0] = 1;
rx[1] = 1;
step = 11;
msleep(200);
} else if (step == 11) {
}
PrintAndLogEx(INFO, "Step11: Wait tag to be ready");
if (model_nr == M2in13B || model_nr == M1in54B) { // Black, white and red screen refresh time is longer, wait first
msleep(9000);
} else if (model_nr == M7in5HD) {
msleep(1000);
}
uint8_t fail_num = 0;
while (1) {
rx[0] = 1;
rx[1] = 1;
if (model_nr == M1in54B) {
// send 0xcd 0x08 with 1.54B
transceive_blocking(step8, 2, rx, 20, actrxlen, 2157 + 2048);
ret = transceive_blocking(step8, 2, rx, 20, actrxlen, false); //cd 08
} else {
transceive_blocking(step11, 2, rx, 20, actrxlen, 2157 + 2048); //cd 0a
ret = transceive_blocking(step11, 2, rx, 20, actrxlen, false); //cd 0a
}
if (ret != PM3_SUCCESS) {
return ret;
}
if (rx[0] == 0xff && rx[1] == 0) {
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(SUCCESS, "E-paper Reflash OK");
fail_num = 0;
step = 12;
msleep(200);
break;
} else {
@ -725,49 +629,16 @@ static int start_drawing(uint8_t model_nr, uint8_t *black, uint8_t *red) {
}
}
}
} else if (step == 12) {
PrintAndLogEx(INFO, "Step12: e-paper power off command");
transceive_blocking(step12, 2, rx, 20, actrxlen, 2157 + 2048); //cd 04
rx[0] = 1;
rx[1] = 1;
step = 13;
ret = transceive_blocking(step12, 2, rx, 20, actrxlen, true); //cd 04
if (ret != PM3_SUCCESS) {
return ret;
}
msleep(200);
} else if (step == 13) {
PrintAndLogEx(SUCCESS, "E-paper Update OK");
rx[0] = 1;
rx[1] = 1;
msleep(200);
DropField();
return PM3_SUCCESS;
} else if (step == 19) {
PrintAndLogEx(INFO, "Step9b");
if (model_nr == M2in7) {
for (i = 0; i < 48; i++) {
rx[0] = 1;
rx[1] = 1;
read_black(i, step13, model_nr, black);
transceive_blocking(step13, 124, rx, 20, actrxlen, 2157 + 2048); //CD 19
progress = i * 50 / 48 + 50;
PrintAndLogEx(INPLACE, "Progress: %d %%", progress);
}
} else if (model_nr == M2in13B) {
for (i = 0; i < 26; i++) {
rx[0] = 1;
rx[1] = 1;
read_red(i, step13, model_nr, red);
//memset(&step13[3], 0xfE, 106);
transceive_blocking(step13, 109, rx, 20, actrxlen, 2157 + 2048);
progress = i * 50 / 26 + 50;
PrintAndLogEx(INPLACE, "Progress: %d %%", progress);
}
}
PROMPT_CLEARLINE;
rx[0] = 1;
rx[1] = 1;
step = 10;
}
}
}