Skip to content

Commit ccd23ed

Browse files
authored
Fix display sleep
1 parent 2de989f commit ccd23ed

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

src/main.cpp

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ void loop() {
161161
else{
162162
idleDelay(2000);
163163
}
164+
updatemsdata();
164165
processButtonEvents();
165166
if(!bleActive)writeSerial("Loop end: " + String(millis() / 100));
166167
}
@@ -1161,14 +1162,14 @@ void updatemsdata(){
11611162
((connectionRequested & 0x01) << 2) | // Bit 2: Connection requested (reserved for future features)
11621163
((mloopcounter & 0x0F) << 4); // Bits 4-7: mloopcounter (4 bits)
11631164
// Bit 3: RFU (reserved, set to 0)
1164-
uint8_t msd_payload[16];
1165-
uint16_t msd_cid = 0x2446;
1166-
memset(msd_payload, 0, sizeof(msd_payload));
1167-
memcpy(msd_payload, (uint8_t*)&msd_cid, sizeof(msd_cid));
1168-
memcpy(&msd_payload[2], dynamicreturndata, sizeof(dynamicreturndata));
1169-
msd_payload[13] = temperatureByte; // Temperature with 0.5°C accuracy (-40°C to +87.5°C)
1170-
msd_payload[14] = batteryVoltageLowByte; // Battery voltage lower 8 bits (10mV steps)
1171-
msd_payload[15] = statusByte; // Battery voltage MSB + Reboot flag + Connection requested + RFU + mloopcounter
1165+
// Update global msd_payload array (public, can be read via handleReadMSD())
1166+
uint16_t msd_cid = 0x2446;
1167+
memset(msd_payload, 0, sizeof(msd_payload));
1168+
memcpy(msd_payload, (uint8_t*)&msd_cid, sizeof(msd_cid));
1169+
memcpy(&msd_payload[2], dynamicreturndata, sizeof(dynamicreturndata));
1170+
msd_payload[13] = temperatureByte; // Temperature with 0.5°C accuracy (-40°C to +87.5°C)
1171+
msd_payload[14] = batteryVoltageLowByte; // Battery voltage lower 8 bits (10mV steps)
1172+
msd_payload[15] = statusByte; // Battery voltage MSB + Reboot flag + Connection requested + RFU + mloopcounter
11721173
#ifdef TARGET_NRF
11731174
Bluefruit.Advertising.clearData();
11741175
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
@@ -1655,7 +1656,6 @@ void sendDisplayAnnouncement() {
16551656
packet[lengthPos] = totalLength & 0xFF;
16561657
packet[lengthPos + 1] = (totalLength >> 8) & 0xFF;
16571658
size_t bytesWritten = wifiClient.write(packet, pos);
1658-
// Note: flush() is deprecated, TCP sockets send data immediately
16591659
if (bytesWritten == pos) {
16601660
writeSerial("Display Announcement sent successfully (" + String(bytesWritten) + " bytes)");
16611661
} else {
@@ -2197,6 +2197,8 @@ void initDisplay(){
21972197
writeTextAndFill(infoText.c_str());
21982198
bbepRefresh(&bbep, REFRESH_FULL);
21992199
waitforrefresh(60);
2200+
bbepSleep(&bbep, 1); // Put display to sleep before power down
2201+
delay(200); // Brief delay after sleep command
22002202
}
22012203
pwrmgm(false);
22022204
}
@@ -2553,6 +2555,25 @@ void handleReadConfig(){
25532555
writeSerial("handleReadConfig function completed successfully");
25542556
}
25552557

2558+
void handleReadMSD() {
2559+
writeSerial("=== READ MSD COMMAND (0x0044) ===");
2560+
uint8_t response[2 + 16]; // Response type + command echo + 16 bytes of MSD data
2561+
uint16_t responseLen = 0;
2562+
response[responseLen++] = 0x00; // Success
2563+
response[responseLen++] = RESP_MSD_READ; // Command echo
2564+
memcpy(&response[responseLen], msd_payload, sizeof(msd_payload));
2565+
responseLen += sizeof(msd_payload);
2566+
sendResponse(response, responseLen);
2567+
writeSerial("MSD read response sent (" + String(responseLen) + " bytes)");
2568+
String hexDump = "MSD payload: ";
2569+
for (int i = 0; i < 16; i++) {
2570+
if (i > 0) hexDump += " ";
2571+
if (msd_payload[i] < 16) hexDump += "0";
2572+
hexDump += String(msd_payload[i], HEX);
2573+
}
2574+
writeSerial(hexDump);
2575+
}
2576+
25562577
void handleFirmwareVersion(){
25572578
writeSerial("Building Firmware Version response...");
25582579
uint8_t major = getFirmwareMajor();
@@ -3761,6 +3782,10 @@ void cleanupDirectWriteState(bool refreshDisplay) {
37613782
directWriteTotalBytes = 0;
37623783
directWriteRefreshMode = 0;
37633784
directWriteStartTime = 0;
3785+
if (refreshDisplay && displayPowerState) {
3786+
bbepSleep(&bbep, 1); // Put display to sleep before power down
3787+
delay(200); // Brief delay after sleep command
3788+
}
37643789
displayPowerState = false;
37653790
pwrmgm(false);
37663791
writeSerial("Direct write state cleaned up");
@@ -3805,6 +3830,8 @@ void handleDirectWriteEnd(uint8_t* data, uint16_t len) {
38053830
delay(100);
38063831
bbepRefresh(&bbep, refreshMode);
38073832
bool refreshSuccess = waitforrefresh(60);
3833+
bbepSleep(&bbep, 1); // Put display to sleep before power down
3834+
delay(200); // Brief delay after sleep command
38083835
cleanupDirectWriteState(false);
38093836
if (refreshSuccess) {
38103837
uint8_t refreshResponse[] = {0x00, RESP_DIRECT_WRITE_REFRESH_SUCCESS};
@@ -3848,6 +3875,10 @@ void imageDataWritten(BLEConnHandle conn_hdl, BLECharPtr chr, uint8_t* data, uin
38483875
writeSerial("=== FIRMWARE VERSION COMMAND (0x0043) ===");
38493876
handleFirmwareVersion();
38503877
break;
3878+
case 0x0044: // Read MSD command
3879+
writeSerial("=== READ MSD COMMAND (0x0044) ===");
3880+
handleReadMSD();
3881+
break;
38513882
case 0x0070: // Direct Write Start command
38523883
writeSerial("=== DIRECT WRITE START COMMAND (0x0070) ===");
38533884
handleDirectWriteStart(data + 2, len - 2);

src/main.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ using namespace Adafruit_LittleFS_Namespace;
5757
#define RESP_CONFIG_READ 0x40 // Config read response
5858
#define RESP_CONFIG_WRITE 0x41 // Config write response
5959
#define RESP_CONFIG_CHUNK 0x42 // Config chunk response
60+
#define RESP_MSD_READ 0x44 // MSD (Manufacturer Specific Data) read response
6061

6162
// Communication mode bit definitions (for system_config.communication_modes)
6263
#define COMM_MODE_BLE (1 << 0) // Bit 0: BLE transfer supported
@@ -120,6 +121,7 @@ void bbepStartWrite(BBEPDISP *pBBEP, int iPlane);
120121
int bbepRefresh(BBEPDISP *pBBEP, int iMode);
121122
bool bbepIsBusy(BBEPDISP *pBBEP);
122123
void bbepWakeUp(BBEPDISP *pBBEP);
124+
void bbepSleep(BBEPDISP *pBBEP, int deepSleep);
123125
void bbepSendCMDSequence(BBEPDISP *pBBEP, const uint8_t *pSeq);
124126
void bbepSetAddrWindow(BBEPDISP *pBBEP, int x, int y, int cx, int cy);
125127
void bbepWriteData(BBEPDISP *pBBEP, uint8_t *pData, int iLen);
@@ -132,6 +134,7 @@ uint8_t mloopcounter = 0;
132134
uint8_t rebootFlag = 1; // Set to 1 after reboot, cleared to 0 after BLE connection
133135
uint8_t connectionRequested = 0; // Reserved for future features (connection requested flag)
134136
uint8_t dynamicreturndata[11] = {0}; // Dynamic return data blocks (bytes 2-12 in advertising payload)
137+
uint8_t msd_payload[16] = {0}; // Manufacturer Specific Data payload (public, updated by updatemsdata())
135138

136139
// Button state tracking structure
137140
struct ButtonState {
@@ -275,6 +278,7 @@ void handleReadConfig();
275278
void handleWriteConfig(uint8_t* data, uint16_t len);
276279
void handleWriteConfigChunk(uint8_t* data, uint16_t len);
277280
void handleFirmwareVersion();
281+
void handleReadMSD(); // Read Manufacturer Specific Data (MSD) payload
278282
void cleanupDirectWriteState(bool refreshDisplay);
279283
void handleDirectWriteStart(uint8_t* data, uint16_t len);
280284
void handleDirectWriteData(uint8_t* data, uint16_t len);

0 commit comments

Comments
 (0)