Allow passing __FlashStringHelper* in addition to char* everywhere
Arduino supplies the F() macro that allows easily putting strings in PROGMEM and marking them with a custom type. Essentially, a __FlashStringHelper* is just the same char*, but cast to a different type, which allows for example Serial.print() to automatically handle both PROGMEM and normal strings. By letting this library accept __FlashStringHelper* as well, you can use easily use the F() macro and reduce the chance of mixing up normal and PROGMEM pointers.
This commit is contained in:
parent
22b77ecb68
commit
5d101b8a7b
@ -83,6 +83,20 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Adafruit_MQTT::Adafruit_MQTT(const __FlashStringHelper *server, uint16_t port, const __FlashStringHelper *cid,
|
||||||
|
const __FlashStringHelper *user, const __FlashStringHelper *pass) {
|
||||||
|
servername = (const char *)server;
|
||||||
|
portnum = port;
|
||||||
|
clientid = (const char *)cid;
|
||||||
|
username = (const char *)user;
|
||||||
|
password = (const char *)pass;
|
||||||
|
|
||||||
|
for (uint8_t i=0; i<MAXSUBSCRIPTIONS; i++) {
|
||||||
|
subscriptions[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Adafruit_MQTT::Adafruit_MQTT(char *server, uint16_t port, char *cid, char *user, char *pass) {
|
Adafruit_MQTT::Adafruit_MQTT(char *server, uint16_t port, char *cid, char *user, char *pass) {
|
||||||
strncpy(servername, server, SERVERNAME_SIZE);
|
strncpy(servername, server, SERVERNAME_SIZE);
|
||||||
@ -372,6 +386,13 @@ Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver,
|
|||||||
qos = q;
|
qos = q;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver,
|
||||||
|
const __FlashStringHelper *feed, uint8_t q) {
|
||||||
|
mqtt = mqttserver;
|
||||||
|
topic = (const char *)feed;
|
||||||
|
qos = q;
|
||||||
|
}
|
||||||
|
|
||||||
bool Adafruit_MQTT_Publish::publish(int32_t i) {
|
bool Adafruit_MQTT_Publish::publish(int32_t i) {
|
||||||
char payload[18];
|
char payload[18];
|
||||||
itoa(i, payload, 10);
|
itoa(i, payload, 10);
|
||||||
@ -403,3 +424,10 @@ Adafruit_MQTT_Subscribe::Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver,
|
|||||||
topic = feed;
|
topic = feed;
|
||||||
qos = q;
|
qos = q;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Adafruit_MQTT_Subscribe::Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver,
|
||||||
|
const __FlashStringHelper *feed, uint8_t q) {
|
||||||
|
mqtt = mqttserver;
|
||||||
|
topic = (const char *)feed;
|
||||||
|
qos = q;
|
||||||
|
}
|
||||||
|
@ -94,6 +94,8 @@ class Adafruit_MQTT {
|
|||||||
public:
|
public:
|
||||||
Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
|
Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
|
||||||
const char *user, const char *pass);
|
const char *user, const char *pass);
|
||||||
|
Adafruit_MQTT(const __FlashStringHelper *server, uint16_t port, const __FlashStringHelper *cid,
|
||||||
|
const __FlashStringHelper *user, const __FlashStringHelper *pass);
|
||||||
virtual ~Adafruit_MQTT() {}
|
virtual ~Adafruit_MQTT() {}
|
||||||
|
|
||||||
// Connect to the MQTT server. Returns 0 on success, otherwise an error code
|
// Connect to the MQTT server. Returns 0 on success, otherwise an error code
|
||||||
@ -124,7 +126,12 @@ class Adafruit_MQTT {
|
|||||||
|
|
||||||
// Publish a message to a topic using the specified QoS level. Returns true
|
// Publish a message to a topic using the specified QoS level. Returns true
|
||||||
// if the message was published, false otherwise.
|
// if the message was published, false otherwise.
|
||||||
|
// The topic must be stored in PROGMEM. It can either be a
|
||||||
|
// char*, or a __FlashStringHelper* (the result of the F() macro).
|
||||||
bool publish(const char *topic, const char *payload, uint8_t qos);
|
bool publish(const char *topic, const char *payload, uint8_t qos);
|
||||||
|
bool publish(const __FlashStringHelper *topic, const char *payload, uint8_t qos) {
|
||||||
|
return publish((const char *)topic, payload, qos);
|
||||||
|
}
|
||||||
|
|
||||||
// Add a subscription to receive messages for a topic. Returns true if the
|
// Add a subscription to receive messages for a topic. Returns true if the
|
||||||
// subscription could be added or was already present, false otherwise.
|
// subscription could be added or was already present, false otherwise.
|
||||||
@ -180,6 +187,7 @@ class Adafruit_MQTT {
|
|||||||
class Adafruit_MQTT_Publish {
|
class Adafruit_MQTT_Publish {
|
||||||
public:
|
public:
|
||||||
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed, uint8_t qos = 0);
|
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed, uint8_t qos = 0);
|
||||||
|
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const __FlashStringHelper *feed, uint8_t qos = 0);
|
||||||
|
|
||||||
bool publish(const char *s);
|
bool publish(const char *s);
|
||||||
bool publish(double f, uint8_t precision=2); // Precision controls the minimum number of digits after decimal.
|
bool publish(double f, uint8_t precision=2); // Precision controls the minimum number of digits after decimal.
|
||||||
@ -196,6 +204,7 @@ private:
|
|||||||
class Adafruit_MQTT_Subscribe {
|
class Adafruit_MQTT_Subscribe {
|
||||||
public:
|
public:
|
||||||
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, const char *feedname, uint8_t q=0);
|
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, const char *feedname, uint8_t q=0);
|
||||||
|
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, const __FlashStringHelper *feedname, uint8_t q=0);
|
||||||
|
|
||||||
bool setCallback(void (*callback)(char *));
|
bool setCallback(void (*callback)(char *));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user