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.

92 lines
2.9KB

  1. // Copyright 2013 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Storage manager.
  28. #include "yarns/storage_manager.h"
  29. #include "yarns/midi_handler.h"
  30. #include "yarns/multi.h"
  31. namespace yarns {
  32. void StorageManager::SaveMulti(uint8_t slot) {
  33. stream_buffer_.Rewind();
  34. multi.Serialize(&stream_buffer_);
  35. storage_.Save(stream_buffer_.bytes(), stream_buffer_.position(), 1 + slot);
  36. }
  37. bool StorageManager::LoadMulti(uint8_t slot) {
  38. // Dummy serialization of the multi to know its size.
  39. stream_buffer_.Rewind();
  40. multi.Serialize(&stream_buffer_);
  41. uint32_t expected_size = stream_buffer_.position();
  42. if (!storage_.Load(stream_buffer_.mutable_bytes(), expected_size, 1 + slot)) {
  43. return false;
  44. } else {
  45. DeserializeMulti();
  46. return true;
  47. }
  48. }
  49. void StorageManager::SaveCalibration() {
  50. stream_buffer_.Rewind();
  51. multi.SerializeCalibration(&stream_buffer_);
  52. storage_.Save(stream_buffer_.bytes(), stream_buffer_.position(), 0);
  53. }
  54. bool StorageManager::LoadCalibration() {
  55. stream_buffer_.Rewind();
  56. multi.SerializeCalibration(&stream_buffer_);
  57. uint32_t expected_size = stream_buffer_.position();
  58. if (!storage_.Load(stream_buffer_.mutable_bytes(), expected_size, 0)) {
  59. return false;
  60. } else {
  61. stream_buffer_.Rewind();
  62. multi.DeserializeCalibration(&stream_buffer_);
  63. return true;
  64. }
  65. }
  66. void StorageManager::SysExSendMulti() {
  67. stream_buffer_.Rewind();
  68. multi.Serialize(&stream_buffer_);
  69. midi_handler.SysExSendPackets(
  70. stream_buffer_.bytes(),
  71. stream_buffer_.position());
  72. }
  73. void StorageManager::DeserializeMulti() {
  74. stream_buffer_.Rewind();
  75. multi.Deserialize(&stream_buffer_);
  76. }
  77. /* extern */
  78. StorageManager storage_manager;
  79. } // namespace yarns