Advertisement
Guest User

Untitled

a guest
May 24th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. /*
  2. ** Copyright 2012, Alexey Roslyakov <alexey.roslyakov@newsycat.com>
  3. **
  4. ** Licensed under the Apache License, Version 2.0 (the "License");
  5. ** you may not use this file except in compliance with the License.
  6. ** You may obtain a copy of the License at
  7. **
  8. **     http://www.apache.org/licenses/LICENSE-2.0
  9. **
  10. ** Unless required by applicable law or agreed to in writing, software
  11. ** distributed under the License is distributed on an "AS IS" BASIS,
  12. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ** See the License for the specific language governing permissions and
  14. ** limitations under the License.
  15. */
  16. #include <unistd.h>
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include <fcntl.h>
  20.  
  21. #include <linux/input.h>
  22.  
  23. static int print_usage(const char *prog)
  24. {
  25.   fprintf(stderr, "Usage: %s <force_feedback_device_path>\n", prog);
  26.  
  27.   return -1;
  28. }
  29.  
  30. int main(int argc, const char **argv)
  31. {
  32.   int i;
  33.   if (argc != 2)
  34.     return print_usage(argv[0]);
  35.  
  36.     struct input_event play;
  37.     struct input_event stop;
  38.     struct ff_effect effect;
  39.     int fd, res;
  40.  
  41.     fd = open(argv[1], O_RDWR);
  42.     if (fd <= 0)
  43.       perror("open");
  44.  
  45.     //unsigned short data[] = {1000,10000, 1000, 10000,0};
  46.     unsigned short data[500];
  47.     for (i=0; i < 500; i+=2) {
  48.       data[i] = 0xff;
  49.       data[i+1] = 0xff;
  50.     }
  51.     memset(&effect, 1, sizeof(effect));
  52.     effect.id = -1;
  53.     effect.type = FF_PERIODIC;
  54.     effect.u.periodic.waveform = FF_CUSTOM;
  55.     effect.u.periodic.period = 6;//100*0x100;
  56.     effect.u.periodic.magnitude = 0;//0x7000;
  57.     effect.u.periodic.offset = 0;
  58.     effect.u.periodic.custom_len = 500;
  59.     effect.u.periodic.custom_data = data;
  60.     effect.direction = 0x40000;
  61.     effect.u.periodic.envelope.attack_length = 0x1000;
  62.     effect.u.periodic.envelope.attack_level  = 0x7fff;
  63.     effect.u.periodic.envelope.fade_length = 0;//0x100;
  64.     effect.u.periodic.envelope.fade_level  = 0;
  65.     effect.trigger.button = 0;
  66.     effect.trigger.interval = 0;
  67.     effect.replay.length = 1;  /* 20 seconds */
  68.     for (i=0; i < 50; i++) {
  69.     res = ioctl(fd, EVIOCSFF, &effect);
  70.     fprintf(stderr, "EVIOCSFF res: %d %d\n", res, effect.id);
  71.  
  72.     int gain = 100;     /* between 0 and 100 */
  73.     struct input_event ie;  /* structure used to communicate with the driver */
  74.  
  75.     ie.type = EV_FF;
  76.     ie.code = FF_GAIN;
  77.     ie.value = 0xFFFFUL;// * gain / 100;
  78.  
  79. #if 1
  80.     if (write(fd, &ie, sizeof(ie)) == -1)
  81.       perror("set gain");
  82.  
  83.     fprintf(stderr, "eff.id: %d\n", effect.id);
  84. #endif
  85.     /* Play three times */
  86.     //play.id = 0;
  87.     play.type = EV_FF;
  88.     play.code = effect.id;
  89.     play.value = 3;
  90.  
  91.       write(fd, (const void*) &play, sizeof(play));
  92.     //.....
  93.     /* Stop an effect */
  94.     stop.type = EV_FF;
  95.     stop.code = effect.id;
  96.     stop.value = 0;
  97.  
  98.     //sleep(1);
  99.     write(fd, (const void*) &stop, sizeof(stop));
  100.     usleep(12*1000);
  101.     }
  102.  
  103.  
  104.   return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement