From 52f84c15006139561db00d5a4e003402f8018f94 Mon Sep 17 00:00:00 2001 From: Larry Bank Date: Tue, 28 Apr 2026 17:15:03 -0400 Subject: [PATCH] Added MacOS / Linux command line example --- linux/examples/get_image/Makefile | 13 ++++++++ linux/examples/get_image/main.cpp | 53 +++++++++++++++++++++++++++++++ src/trmnl_lib.cpp | 8 ++--- 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100755 linux/examples/get_image/Makefile create mode 100644 linux/examples/get_image/main.cpp diff --git a/linux/examples/get_image/Makefile b/linux/examples/get_image/Makefile new file mode 100755 index 0000000..1351e9e --- /dev/null +++ b/linux/examples/get_image/Makefile @@ -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 diff --git a/linux/examples/get_image/main.cpp b/linux/examples/get_image/main.cpp new file mode 100644 index 0000000..8b9b161 --- /dev/null +++ b/linux/examples/get_image/main.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +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 \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() */ diff --git a/src/trmnl_lib.cpp b/src/trmnl_lib.cpp index dea9265..7855b9f 100644 --- a/src/trmnl_lib.cpp +++ b/src/trmnl_lib.cpp @@ -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 */