timelaps.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* test-spi-rpi-01.c - Programme pour Raspberry Pi */
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <linux/types.h>
  7. #include <linux/spi/spidev.h>
  8. #include <sys/ioctl.h>
  9. int focus( int fd_spi){
  10. unsigned char bytes[2]={0x00,0xE6};
  11. printf("FOCUS !\n");
  12. return write(fd_spi,bytes,2);
  13. }
  14. int shot ( int fd_spi ) {
  15. unsigned char bytes[2]={0x00,0xF5};
  16. printf("SHOT !\n");
  17. return write(fd_spi,bytes,2);
  18. }
  19. int stdby ( int fd_spi ) {
  20. unsigned char bytes[2]={0x00,0x2F};
  21. printf("STDBY !\n ");
  22. return write(fd_spi,bytes,2);
  23. }
  24. int main(int argc, char *argv[])
  25. {
  26. int fd_spi;
  27. char ligne[80];
  28. int value;
  29. unsigned char byte;
  30. unsigned int speed = 250000;
  31. if (argc != 3) {
  32. fprintf(stderr, "usage: %s <spi-port> <spi-speed>n", argv[0]);
  33. exit(EXIT_FAILURE);
  34. }
  35. fd_spi = open(argv[1], O_RDWR);
  36. if (fd_spi < 0) {
  37. perror(argv[1]);
  38. exit(EXIT_FAILURE);
  39. }
  40. if (sscanf(argv[2], "%d", & speed) != 1) {
  41. fprintf(stderr, "Wrong value for speed: %sn", argv[2]);
  42. exit(EXIT_FAILURE);
  43. }
  44. if (ioctl(fd_spi, SPI_IOC_WR_MAX_SPEED_HZ, & speed) != 0) {
  45. perror("ioctl");
  46. exit(EXIT_FAILURE);
  47. }
  48. while ( 1 )
  49. {
  50. stdby(fd_spi);
  51. sleep(20);
  52. focus(fd_spi);
  53. usleep(200000);
  54. shot(fd_spi);
  55. usleep(100000);
  56. }
  57. while (fgets(ligne, 80, stdin) != NULL) {
  58. if (sscanf(ligne, "%d", & value) != 1){
  59. fprintf(stderr, "integer value expectedn");
  60. continue;
  61. }
  62. byte = (unsigned char) (value & 0xFF);
  63. if (write(fd_spi, & byte, 1) != 1) {
  64. perror("write");
  65. exit(EXIT_FAILURE);
  66. }
  67. }
  68. close(fd_spi);
  69. return EXIT_SUCCESS;
  70. }