mirror of
https://github.com/usetrmnl/trmnl_lib.git
synced 2026-04-29 13:45:30 -07:00
Added MacOS / Linux command line example
This commit is contained in:
Executable
+13
@@ -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
|
||||
@@ -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
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user