Close

Using the Nextion display: Issue solved

A project log for Building the Thor robot

Building a 6-axis robot based on the Thor robot. When size DOES matter!

olaf-baeyensOlaf Baeyens 08/27/2017 at 00:590 Comments

Finally solved the issue with my Nextion display commands not being recognized by the Arduino Due code.

The Arduino implementation of the Nextion button and dual state button gets triggered when you release the button.

The button "press" command gets sent to the serial port but is ignored, you need the button "release" to be sent.


Make sure you activate this checkbox in order for the Ultratronics board Nextion implementation callback gets triggered. Without this the Nextion will not send the release event through the serial port.



We need some code to extend the NextDSButton and the NextButton.  Nextion decided to make the Pid, Cid and Name protected which is stupid since we can use this to make more compact code.

class NexDSButtonExt : public NexDSButton {
public:
	NexDSButtonExt(uint8_t pid, uint8_t cid, const char *name): NexDSButton(pid, cid, name) {
	}
	uint8_t getObjpid(void) {
		return this->getObjPid();
	}
	uint8_t getObjcid(void)  {
		return this->getObjCid();
	}
	const char *getObjname(void)  {
		return this->getObjName();
	}
};
class NexButtonExt : public NexButton {
public:
	NexButtonExt(uint8_t pid, uint8_t cid, const char *name) : NexButton(pid, cid, name) {
	}
	uint8_t getObjpid(void) {
		return this->getObjPid();
	}
	uint8_t getObjcid(void) {
		return this->getObjCid();
	}
	const char *getObjname(void) {
		return this->getObjName();
	}
};

So we start by exposing these protected fields (note C++ does not like that you use the same name, so I changed one character to lowercase for the time being).

This class will get extended a lot in the future as we develop the Thor functional code.


Using the extended classes is done with code below.
NexButtonExt MotorEnable_E1_Left = NexButtonExt(MotorControl_Page, MotorEnable_E1_Left_ID, "b11");

And finally can specifically choose the page number and the object and assign a custom action inside one method. No need for a separate callback for every button. Note the typecast to NextDSButtonExt.

void MotorEnable_Callback(void *ptr) {
	auto btn = (NexDSButtonExt *)(ptr);
	if (btn->getObjpid() == MotorControl_Page) {
		if (btn->getObjcid() == MotorEnable_E0_ID) {
			uint32_t dual_state;
			bool isValid= btn->getValue(&dual_state);
                        if (isValid) {
			    if (dual_state) {
			    	digitalWrite(LED_BUILTIN, HIGH);
        		    } else {
	        		digitalWrite(LED_BUILTIN, LOW);
	        	    }
                        }
		}
	}
}
void MotorEnable_Callback2(void *ptr) {
	auto btn = (NexButtonExt *)(ptr);
	btn->setText(btn->getObjname());
}

Example above changes the debug LED for a specific button on a specific page.

The second callback send the object name back to the button.


Time is up for this week. To be continued.

Discussions