Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1.  
  2. /* User Type definition */
  3. typedef void(*callback)(void);
  4. typedef struct threadargs
  5. {
  6. callback cb;
  7. } threadargs;
  8.  
  9. /**
  10. * startScanner - Configure scanner and start listening scanner until there is bar code to roll up
  11. * @vCallback: The function to call when a bar code is received from serial port
  12. */
  13. void startScanner(void (*vCallback)(void))
  14. {
  15. threadargs ta = {vCallback};
  16. if(pthread_create(&scannerThread, NULL, scannerThreadFunc, &ta))
  17. {
  18. printf("Thread creation fails!\n");
  19. }
  20. }
  21.  
  22. static void *scannerThreadFunc(void *arg)
  23. {
  24. s8 readBuf[MAX_SCANNER_SIZE];
  25. s32 bytesRead;
  26. threadargs *ta = arg;
  27.  
  28. while(1)
  29. {
  30. bytesRead = read(fd, readBuf, MAX_SCANNER_SIZE);
  31. if(bytesRead == -1)
  32. {
  33. printf("No Read\n");
  34. }
  35. else
  36. {
  37. ta->cb();
  38. memset(readBuf, '\0', MAX_SCANNER_SIZE);
  39. }
  40. }
  41. pthread_exit(NULL);
  42. }
  43.  
  44. void vCallbackFunc(void)
  45. {
  46. printf("Callback function\n");
  47. }
  48.  
  49.  
  50. int main(void)
  51. {
  52. startScanner(vCallbackFunc);
  53. pthread_join(scannerThread, NULL);
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement