Added MacOS / Linux command line example

This commit is contained in:
Larry Bank
2026-04-28 17:15:03 -04:00
parent 089c71675b
commit 52f84c1500
3 changed files with 70 additions and 4 deletions
+13
View File
@@ -0,0 +1,13 @@
CFLAGS=-D__LINUX__ -c -Wall -O2
LIBS = -ltrmnl -L/opt/homebrew/opt/curl/lib -lcurl
all: trmnl_demo
trmnl_demo: main.o
$(CXX) main.o $(LIBS) -o trmnl_demo
main.o: main.cpp
$(CXX) $(CFLAGS) main.cpp
clean:
rm -rf *.o demo
+53
View File
@@ -0,0 +1,53 @@
#include <trmnl_lib.h>
#include <stdio.h>
#include <stdlib.h>
TRMNL trmnl;
void showImage(const char *szDir, uint8_t *pImage, int iImageSize)
{
char szCMD[256], szName[256];
FILE *f;
strcpy(szName, szDir);
if (szName[strlen(szName)-1] != '/') strcat(szName, "/");
strcat(szName, "temp_image.png");
f = fopen(szName, "w+b");
if (f) {
fwrite(pImage, iImageSize, 1, f);
fflush(f);
fclose(f);
} else {
printf("Error opening %s\n", szName);
return;
}
snprintf(szCMD, sizeof(szCMD), "open %s", szName);
system(szCMD);
} /* showImage() */
int main(int argc, char *argv[])
{
int rc;
uint8_t *pImage;
int iImageSize;
printf("Starting TRMNL demo\n");
if (argc != 3) {
printf("Usage: trmnl <API Key> <temp image dir>\n");
printf("Example: ./trmnl_demo abc_123 ~/home/Downloads\n");
return 0;
}
rc = trmnl.getAPI(argv[1]); // also reads and sends the sensor values if valid
if (rc == TRMNL_SUCCESS) {
printf("API request succeeded\n");
rc = trmnl.getImage(&pImage, &iImageSize);
if (rc == TRMNL_SUCCESS) {
printf("Image download succeeded (%d bytes)\n", iImageSize);
showImage(argv[2], pImage, iImageSize); // save and use "open" shell cmd
trmnl.freeImage();
}
}
return 0;
} /* main() */
+4 -4
View File
@@ -286,12 +286,12 @@ int rc = TRMNL_ERROR;
* Do something nice with it!
*/
printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
//printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
// printf("%s\n", chunk.memory);
// Parse the JSON
pJSON = cJSON_ParseWithLength((const char *)chunk.memory, chunk.size);
if (pJSON) {
printf("JSON parsed successfully!\n");
//printf("JSON parsed successfully!\n");
if (cJSON_HasObjectItem(pJSON, "status")) {
pItem = cJSON_GetObjectItem(pJSON, "status");
_status = pItem->valueint;
@@ -306,7 +306,7 @@ int rc = TRMNL_ERROR;
}
if (_image_url.length() > 0) {
rc = TRMNL_SUCCESS;
printf("Image URL: %s\n", _image_url.c_str());
//printf("Image URL: %s\n", _image_url.c_str());
}
} // if pJSON
} // successful GET request
@@ -448,7 +448,7 @@ int TRMNL::getImage(uint8_t **pBuffer, int *pSize)
* Do something nice with it!
*/
printf("%lu bytes of image retrieved\n", (unsigned long)chunk.size);
//printf("%lu bytes of image retrieved\n", (unsigned long)chunk.size);
}
/* cleanup curl stuff */