Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* User Type definition */
- typedef void(*callback)(void);
- typedef struct threadargs
- {
- callback cb;
- } threadargs;
- /**
- * startScanner - Configure scanner and start listening scanner until there is bar code to roll up
- * @vCallback: The function to call when a bar code is received from serial port
- */
- void startScanner(void (*vCallback)(void))
- {
- threadargs ta = {vCallback};
- if(pthread_create(&scannerThread, NULL, scannerThreadFunc, &ta))
- {
- printf("Thread creation fails!\n");
- }
- }
- static void *scannerThreadFunc(void *arg)
- {
- s8 readBuf[MAX_SCANNER_SIZE];
- s32 bytesRead;
- threadargs *ta = arg;
- while(1)
- {
- bytesRead = read(fd, readBuf, MAX_SCANNER_SIZE);
- if(bytesRead == -1)
- {
- printf("No Read\n");
- }
- else
- {
- ta->cb();
- memset(readBuf, '\0', MAX_SCANNER_SIZE);
- }
- }
- pthread_exit(NULL);
- }
- void vCallbackFunc(void)
- {
- printf("Callback function\n");
- }
- int main(void)
- {
- startScanner(vCallbackFunc);
- pthread_join(scannerThread, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement