diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build diff --git a/README.md b/README.md index 08b824a..4efea99 100644 --- a/README.md +++ b/README.md @@ -36,172 +36,21 @@ if (TDMS_AddChannelToGroup(&Channel1Group1, 7. Add data to Channels with `TDMS_SetChannelDataValues` or add data to all Channels of a Channel Group with `TDMS_SetGroupDataValues` then save the out buffer on disk. ## Example -```C -#include -#include -#include -#include "TDMS.h" - -#define MEMALLOC(size) calloc(4096, sizeof(uint8_t)); \ - if(!Buffer) \ - { \ - printf("malloc failed!\n"); \ - char ch; \ - scanf("%c", &ch); \ - return (1); \ - } - - -int main() -{ - printf("Hello World!\n"); - - FILE *MyFile; - TDMS_File_t FileTDMS; - TDMS_Group_t Group1; - TDMS_Group_t Group2; - TDMS_Channel_t Channel1Group1; - TDMS_Channel_t Channel2Group1; - TDMS_Channel_t Channel1Group2; - - uint8_t *Buffer; - uint32_t Size = 0; - - MyFile = fopen("./Test.tdms", "wb"); - if (!MyFile) - { - printf("File open failed!\n"); - char ch; - scanf("%c", &ch); - return (1); - } - - if (TDMS_InitFile(&FileTDMS) != TDMS_OK) - printf("Init file failed!"); - - - if (TDMS_AddGroupToFile(&Group1, - &FileTDMS, - "Group 1 name") != TDMS_OK) - printf("Add Group 1 failed!"); - - if (TDMS_AddGroupToFile(&Group2, - &FileTDMS, - "Group 2 name") != TDMS_OK) - printf("Add Group 2 failed!"); - - - if (TDMS_AddChannelToGroup(&Channel1Group1, - &Group1, - "Channel 1 name", - TDMS_DataType_U8) != TDMS_OK) - printf("Add Channel 1 failed!"); - - if (TDMS_AddChannelToGroup(&Channel2Group1, - &Group1, - "Channel 2 name", - TDMS_DataType_SingleFloat) != TDMS_OK) - printf("Add Channel 2 failed!"); - - if (TDMS_AddChannelToGroup(&Channel1Group2, - &Group2, - "Channel 3 name", - TDMS_DataType_TimeStamp) != TDMS_OK) - printf("Add Channel 3 failed!"); - - - TDMS_GenFirstPart(&FileTDMS, NULL, &Size); - Buffer = MEMALLOC(Size + 1); - TDMS_GenFirstPart(&FileTDMS, Buffer, &Size); - fwrite(Buffer, 1, Size, MyFile); - free(Buffer); - - - TDMS_AddPropertyToFile(NULL, - &Size, - "Description", - TDMS_DataType_String, - "A file generated by TDMS library"); - Buffer = MEMALLOC(Size + 1); - TDMS_AddPropertyToFile(Buffer, - &Size, - "Description", - TDMS_DataType_String, - "A file generated by TDMS library"); - fwrite(Buffer, 1, Size, MyFile); - free(Buffer); - - TDMS_AddPropertyToFile(NULL, - &Size, - "Author", - TDMS_DataType_String, - "Hossein-M98"); - Buffer = MEMALLOC(Size + 1); - TDMS_AddPropertyToFile(Buffer, - &Size, - "Author", - TDMS_DataType_String, - "Hossein-M98"); - fwrite(Buffer, 1, Size, MyFile); - free(Buffer); - - - TDMS_AddPropertyToGroup(&Group1, - NULL, - &Size, - "Description", - TDMS_DataType_String, - "This is Group 1"); - Buffer = MEMALLOC(Size + 1); - TDMS_AddPropertyToGroup(&Group1, - Buffer, - &Size, - "Description", - TDMS_DataType_String, "This is Group 1"); - fwrite(Buffer, 1, Size, MyFile); - free(Buffer); - - - TDMS_AddPropertyToChannel(&Channel1Group2, - NULL, - &Size, - "Description", - TDMS_DataType_String, - "This is a Date and Time channel"); - Buffer = MEMALLOC(Size + 1); - TDMS_AddPropertyToChannel(&Channel1Group2, - Buffer, - &Size, - "Description", - TDMS_DataType_String, - "This is a Date and Time channel"); - fwrite(Buffer, 1, Size, MyFile); - free(Buffer); - - - uint8_t Data[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14}; - float Data2[] = {100.25, 101.5, 102.75, 103.25, 104.5, 105.75}; - TDMS_SetGroupDataValues(&Group1, NULL, &Size, - Data, sizeof(Data) / sizeof(uint8_t), - Data2, sizeof(Data2) / sizeof(float)); - Buffer = MEMALLOC(Size + 1); - TDMS_SetGroupDataValues(&Group1, Buffer, &Size, - Data, sizeof(Data) / sizeof(uint8_t), - Data2, sizeof(Data2) / sizeof(float)); - fwrite(Buffer, 1, Size, MyFile); - free(Buffer); - - uint64_t second = TDMS_TimeSecond(2023, 5, 17, 12, 14, 10); - TDMS_Timestamp_t Data4[] = {{.Fraction = 0, .Second = second}}; - TDMS_SetChannelDataValues(&Channel1Group2, NULL, &Size, - Data4, sizeof(Data4) / sizeof(TDMS_Timestamp_t)); - Buffer = MEMALLOC(Size + 1); - TDMS_SetChannelDataValues(&Channel1Group2, Buffer, &Size, - Data4, sizeof(Data4) / sizeof(TDMS_Timestamp_t)); - fwrite(Buffer, 1, Size, MyFile); - free(Buffer); - - fclose(MyFile); - return (0); -} +To run the basic example, follow these steps: + 1. Clone the repository +```bash +git clone https://github.com/MahdaSystem/TDMS.git +``` + 2. Go to the example directory +```bash +cd ./TDMS/example/basic +``` + 3. Run the make command +```bash +make all +``` + 4. Run the example +```bash +./build/output.elf ``` + 5. The `.tdms` file will be generated in the `build` directory diff --git a/example/basic/main.c b/example/basic/main.c new file mode 100644 index 0000000..689716a --- /dev/null +++ b/example/basic/main.c @@ -0,0 +1,176 @@ +/** + ********************************************************************************** + * @file main.c + * @author Hossein.M (https://github.com/Hossein-M98) + * @brief Basic example for TDMS library + ********************************************************************************** + */ + +#include +#include +#include +#include "TDMS.h" + +#define MEMALLOC(size) calloc(4096, sizeof(uint8_t)); \ + if(!Buffer) \ + { \ + printf("malloc failed!\n"); \ + char ch; \ + scanf("%c", &ch); \ + return (1); \ + } + + +int main() +{ + printf("TDMS Library test\n"); + + FILE *MyFile; + TDMS_File_t FileTDMS; + TDMS_Group_t Group1; + TDMS_Group_t Group2; + TDMS_Channel_t Channel1Group1; + TDMS_Channel_t Channel2Group1; + TDMS_Channel_t Channel1Group2; + + uint8_t *Buffer; + uint32_t Size = 0; + + MyFile = fopen("./build/Test.tdms", "wb"); + if (!MyFile) + { + printf("File open failed!\n"); + char ch; + scanf("%c", &ch); + return (1); + } + + if (TDMS_InitFile(&FileTDMS) != TDMS_OK) + printf("Init file failed!"); + + + if (TDMS_AddGroupToFile(&Group1, + &FileTDMS, + "Group 1 name") != TDMS_OK) + printf("Add Group 1 failed!"); + + if (TDMS_AddGroupToFile(&Group2, + &FileTDMS, + "Group 2 name") != TDMS_OK) + printf("Add Group 2 failed!"); + + + if (TDMS_AddChannelToGroup(&Channel1Group1, + &Group1, + "Channel 1 name", + TDMS_DataType_U8) != TDMS_OK) + printf("Add Channel 1 failed!"); + + if (TDMS_AddChannelToGroup(&Channel2Group1, + &Group1, + "Channel 2 name", + TDMS_DataType_SingleFloat) != TDMS_OK) + printf("Add Channel 2 failed!"); + + if (TDMS_AddChannelToGroup(&Channel1Group2, + &Group2, + "Channel 3 name", + TDMS_DataType_TimeStamp) != TDMS_OK) + printf("Add Channel 3 failed!"); + + + TDMS_GenFirstPart(&FileTDMS, NULL, &Size); + Buffer = MEMALLOC(Size + 1); + TDMS_GenFirstPart(&FileTDMS, Buffer, &Size); + fwrite(Buffer, 1, Size, MyFile); + free(Buffer); + + + TDMS_AddPropertyToFile(NULL, + &Size, + "Description", + TDMS_DataType_String, + "A file generated by TDMS library"); + Buffer = MEMALLOC(Size + 1); + TDMS_AddPropertyToFile(Buffer, + &Size, + "Description", + TDMS_DataType_String, + "A file generated by TDMS library"); + fwrite(Buffer, 1, Size, MyFile); + free(Buffer); + + TDMS_AddPropertyToFile(NULL, + &Size, + "Author", + TDMS_DataType_String, + "Hossein-M98"); + Buffer = MEMALLOC(Size + 1); + TDMS_AddPropertyToFile(Buffer, + &Size, + "Author", + TDMS_DataType_String, + "Hossein-M98"); + fwrite(Buffer, 1, Size, MyFile); + free(Buffer); + + + TDMS_AddPropertyToGroup(&Group1, + NULL, + &Size, + "Description", + TDMS_DataType_String, + "This is Group 1"); + Buffer = MEMALLOC(Size + 1); + TDMS_AddPropertyToGroup(&Group1, + Buffer, + &Size, + "Description", + TDMS_DataType_String, "This is Group 1"); + fwrite(Buffer, 1, Size, MyFile); + free(Buffer); + + + TDMS_AddPropertyToChannel(&Channel1Group2, + NULL, + &Size, + "Description", + TDMS_DataType_String, + "This is a Date and Time channel"); + Buffer = MEMALLOC(Size + 1); + TDMS_AddPropertyToChannel(&Channel1Group2, + Buffer, + &Size, + "Description", + TDMS_DataType_String, + "This is a Date and Time channel"); + fwrite(Buffer, 1, Size, MyFile); + free(Buffer); + + + uint8_t Data[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14}; + float Data2[] = {100.25, 101.5, 102.75, 103.25, 104.5, 105.75}; + TDMS_SetGroupDataValues(&Group1, NULL, &Size, + Data, sizeof(Data) / sizeof(uint8_t), + Data2, sizeof(Data2) / sizeof(float)); + Buffer = MEMALLOC(Size + 1); + TDMS_SetGroupDataValues(&Group1, Buffer, &Size, + Data, sizeof(Data) / sizeof(uint8_t), + Data2, sizeof(Data2) / sizeof(float)); + fwrite(Buffer, 1, Size, MyFile); + free(Buffer); + + uint64_t second = TDMS_TimeSecond(2023, 5, 17, 12, 14, 10); + TDMS_Timestamp_t Data4[] = {{.Fraction = 0, .Second = second}}; + TDMS_SetChannelDataValues(&Channel1Group2, NULL, &Size, + Data4, sizeof(Data4) / sizeof(TDMS_Timestamp_t)); + Buffer = MEMALLOC(Size + 1); + TDMS_SetChannelDataValues(&Channel1Group2, Buffer, &Size, + Data4, sizeof(Data4) / sizeof(TDMS_Timestamp_t)); + fwrite(Buffer, 1, Size, MyFile); + free(Buffer); + + printf("Process finished successfully!\n"); + fclose(MyFile); + return (0); +} diff --git a/example/basic/makefile b/example/basic/makefile new file mode 100644 index 0000000..95a3e61 --- /dev/null +++ b/example/basic/makefile @@ -0,0 +1,40 @@ +CC = gcc + +CFLAGS = + +TARGET = output +BUILD_DIR = build +INC_DIR = ../../src/include ../../config +SRC = ./main.c ../../src/TDMS.c + + +ifeq ($(OS),Windows_NT) +FIXPATH = $(subst /,\,$1) +RMD := rd /s /q +MD := mkdir +else +FIXPATH = $1 +RMD = rm -r +MD := mkdir -p +endif + + +SOURCES = $(filter %.c, $(SRC)) +INCLUDES = $(patsubst %,-I%, $(INC_DIR:%/=%)) +OUTPUT_ELF = $(addsuffix .elf,$(call FIXPATH,$(BUILD_DIR)/$(TARGET))) + + +all: $(BUILD_DIR) $(TARGET) + +clean: + $(RMD) $(call FIXPATH,$(BUILD_DIR)) + +.c.o: + $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $(call FIXPATH,$(addprefix $(BUILD_DIR)/,$(notdir $@))) + +# elf file +$(TARGET): $(SOURCES:.c=.o) + $(CC) $(CFLAGS) $(INCLUDES) -o $(OUTPUT_ELF) $(call FIXPATH,$(addprefix $(BUILD_DIR)/,$(notdir $(SOURCES:.c=.o)))) + +$(BUILD_DIR): + $(MD) $(call FIXPATH,$(BUILD_DIR))