A 3x3x3 LED cube, originally based on Paul Wilhelms project with USB and a lot of new features.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ledcube.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * økoyono MiniLEDCube
  3. *
  4. * By Kai Lauterbach (klaute at web dot de) 11/2011
  5. *
  6. * Based on http://mosfetkiller.de/?s=miniledcube
  7. *
  8. * License: General Public License (GPL v3)
  9. *
  10. */
  11. #include "ledcube.h"
  12. /*! \brief The setFrame function.
  13. * \param frame The 32bit frame data. Bit 0-8 equals layer one; bit 9 - 17 euqals layer two; bit 18 - 26 equals layer three. the 5 MSB is the lifetime of the current frame in ISR calls (300Hz).
  14. * \return NOT_CONNECTED_ERROR or the return value of the usb_control_msg function.
  15. */
  16. int lc_setFrame(unsigned long frame)
  17. {
  18. if (_lc_handle == NULL)
  19. return NOT_CONNECTED_ERROR;
  20. if (frame < 0 || frame > 0xffffffff)
  21. return PARAMETER_FRAME_OUT_OF_RANGE;
  22. int low = frame & 0xffff;
  23. int high = (frame & 0xffff0000) >> 16;
  24. int ret = usb_control_msg(_lc_handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_FRAME, low, 0, _lc_buffer, 0, 300);
  25. ret += usb_control_msg(_lc_handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_FRAME, high, 1, _lc_buffer, 0, 300);
  26. return ret;
  27. }
  28. /*! \brief The setMode function.
  29. * \param mode The firmware mode. MODE_ANIMATION_STOP; MODE_ANIMATION_SINGLE; MODE_ANIMATION_LOOP
  30. * \return NOT_CONNECTED_ERROR or the return value of the usb_control_msg function.
  31. */
  32. int lc_setMode(int mode)
  33. {
  34. if (_lc_handle == NULL)
  35. return NOT_CONNECTED_ERROR;
  36. if (mode < MODE_ANIMATION_STOP || mode > MODE_ANIMATION_LOOP)
  37. return PARAMETER_MODE_OUT_OF_RANGE;
  38. return usb_control_msg(_lc_handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_MODE, mode, 0, _lc_buffer, 0, 300);
  39. }
  40. /*! \brief The saveFrame function.
  41. * \param frame The 32bit frame data. Bit 0-8 equals layer one; bit 9 - 17 euqals layer two; bit 18 - 26 equals layer three. The 5 MSB is the lifetime of the current frame in ISR calls (300Hz) multiplied by 8.
  42. * \param delay The lifetime (onyl 5 bits) of the given frame in ISR calls multiplied by 8.
  43. * \param index The position in the EEPROM (0 to 31) of the internal animation content.
  44. * \return NOT_CONNECTED_ERROR or the return value of the usb_control_msg function.
  45. */
  46. int lc_saveFrame(unsigned long frame, int delay, int index)
  47. {
  48. if (_lc_handle == NULL)
  49. return NOT_CONNECTED_ERROR;
  50. if (frame < 0 || frame > 0xffffffff)
  51. return PARAMETER_FRAME_OUT_OF_RANGE;
  52. if (delay < 0 || delay > 31)
  53. return PARAMETER_DELAY_OUT_OF_RANGE;
  54. if (index < 0 || index > 31)
  55. return PARAMETER_INDEX_OUT_OF_RANGE;
  56. frame = frame + (delay << 27);
  57. lc_setFrame(frame);
  58. return usb_control_msg(_lc_handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_EEPROM_STORE_FRAME, 0, index, _lc_buffer, 0, 300);
  59. }
  60. /*! \brief The init function.
  61. * \return SUCCESSFULLY_CONNECTED or DEVICE_NOT_FOUND_ERROR.
  62. */
  63. int lc_init()
  64. {
  65. usb_init();
  66. /* compute lc_vid/lc_pid from usbconfig.h so that there is a central source of information */
  67. _lc_vid = _lc_rawVid[1] * 256 + _lc_rawVid[0];
  68. _lc_pid = _lc_rawPid[1] * 256 + _lc_rawPid[0];
  69. /* The following function is in opendevice.c: */
  70. if( usbOpenDevice( &_lc_handle, _lc_vid, _lc_vendor, _lc_pid, _lc_product, NULL, NULL, NULL) != 0)
  71. {
  72. fprintf(stderr, "Could not find USB device \"%s\" with lc_vid=0x%x lc_pid=0x%x\n", _lc_product, _lc_vid, _lc_pid);
  73. return DEVICE_NOT_FOUND_ERROR;
  74. }
  75. return SUCCESSFULLY_CONNECTED;
  76. }
  77. /*! \brief The close function.
  78. * \return NOT_CONNECTED_ERROR or return state of the usb_close function.
  79. */
  80. int lc_close()
  81. {
  82. if (_lc_handle == NULL)
  83. return NOT_CONNECTED_ERROR;
  84. return usb_close(_lc_handle);
  85. }