Guino_libray.ino 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. GUINO DASHBOARD TEMPLATE FOR THE ARDUINO.
  3. Done by Mads Hobye as a part of Instructables (AIR Program) & Medea (PhD Student).
  4. Licens: Creative Commons — Attribution-ShareAlike
  5. It should be used with the GUINO Dashboard app.
  6. More info can be found here: www.hobye.dk
  7. # This is the Guino Protocol Library should only be edited if you know what you are doing.
  8. */
  9. #include <EasyTransfer.h>
  10. #include <EEPROM.h>
  11. #define guino_executed -1
  12. #define guino_init 0
  13. #define guino_addSlider 1
  14. #define guino_addButton 2
  15. #define guino_iamhere 3
  16. #define guino_addToggle 4
  17. #define guino_addRotarySlider 5
  18. #define guino_saveToBoard 6
  19. #define guino_setFixedGraphBuffer 8
  20. #define guino_clearLabel 7
  21. #define guino_addWaveform 9
  22. #define guino_addColumn 10
  23. #define guino_addSpacer 11
  24. #define guino_addMovingGraph 13
  25. #define guino_buttonPressed 14
  26. #define guino_addChar 15
  27. #define guino_setMin 16
  28. #define guino_setMax 17
  29. #define guino_setValue 20
  30. #define guino_addLabel 12
  31. #define guino_large 0
  32. #define guino_medium 1
  33. #define guino_small 2
  34. #define guino_setColor 21
  35. boolean guidino_initialized = false;
  36. //This function will write a 2 byte integer to the eeprom at the specified address and address + 1
  37. void EEPROMWriteInt(int p_address, int p_value)
  38. {
  39. byte lowByte = ((p_value >> 0) & 0xFF);
  40. byte highByte = ((p_value >> 8) & 0xFF);
  41. EEPROM.write(p_address, lowByte);
  42. EEPROM.write(p_address + 1, highByte);
  43. }
  44. //This function will read a 2 byte integer from the eeprom at the specified address and address + 1
  45. unsigned int EEPROMReadInt(int p_address)
  46. {
  47. byte lowByte = EEPROM.read(p_address);
  48. byte highByte = EEPROM.read(p_address + 1);
  49. return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
  50. }
  51. //create object
  52. EasyTransfer ET;
  53. struct SEND_DATA_STRUCTURE
  54. {
  55. //put your variable definitions here for the data you want to send
  56. //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  57. char cmd;
  58. char item;
  59. int value;
  60. };
  61. // Find a way to dynamically allocate memory
  62. int guino_maxGUIItems = 100;
  63. int guino_item_counter = 0;
  64. int *guino_item_values[100];
  65. int gTmpInt = 0; // temporary int for items without a variable
  66. boolean internalInit = true; // boolean to initialize before connecting to serial
  67. // COMMAND STRUCTURE
  68. //give a name to the group of data
  69. SEND_DATA_STRUCTURE guino_data;
  70. int eepromKey = 1234;
  71. void guino_update()
  72. {
  73. while(Serial.available())
  74. {
  75. if(ET.receiveData())
  76. {
  77. switch (guino_data.cmd)
  78. {
  79. case guino_init:
  80. guino_item_counter = 0;
  81. guidino_initialized = true;
  82. gInit();
  83. break;
  84. case guino_setValue:
  85. *guino_item_values[guino_data.item] = guino_data.value;
  86. guino_data.cmd = guino_executed;
  87. gItemUpdated(guino_data.item);
  88. break;
  89. case guino_buttonPressed:
  90. gButtonPressed(guino_data.item);
  91. break;
  92. case guino_saveToBoard:
  93. {
  94. gInitEEprom();
  95. for (int i =0; i < guino_item_counter;i++)
  96. {
  97. EEPROMWriteInt(i*2+2, *guino_item_values[i]);
  98. }
  99. }
  100. break;
  101. }
  102. }
  103. }
  104. }
  105. void gInitEEprom()
  106. {
  107. if(EEPROMReadInt(0) != eepromKey)
  108. {
  109. EEPROMWriteInt(0, eepromKey);
  110. for (int i =1; i <guino_maxGUIItems;i++)
  111. {
  112. EEPROMWriteInt(i*2+2, -3276);
  113. }
  114. }
  115. }
  116. void gSetColor(int _red, int _green, int _blue)
  117. {
  118. gSendCommand(guino_setColor, 0, _red);
  119. gSendCommand(guino_setColor, 1, _green);
  120. gSendCommand(guino_setColor, 2, _blue);
  121. }
  122. void gGetSavedValue(int item_number, int *_variable)
  123. {
  124. if(EEPROMReadInt(0) == eepromKey && internalInit)
  125. {
  126. int tmpVar = EEPROMReadInt((item_number)*2+2);
  127. if(tmpVar != -3276)
  128. *_variable = tmpVar;
  129. }
  130. }
  131. void gBegin(int _eepromKey)
  132. {
  133. // Sets all pointers to a temporary value just to make sure no random memory pointers.
  134. for(int i = 0; i < guino_maxGUIItems; i++)
  135. {
  136. guino_item_values[i] = &gTmpInt;
  137. }
  138. eepromKey = _eepromKey;
  139. gInit(); // this one needs to run twice only way to work without serial connection.
  140. internalInit = false;
  141. Serial.begin(115200);
  142. ET.begin(details(guino_data), &Serial);
  143. gSendCommand(guino_executed, 0, 0);
  144. gSendCommand(guino_executed, 0, 0);
  145. gSendCommand(guino_executed, 0, 0);
  146. gSendCommand(guino_iamhere, 0, 0);
  147. }
  148. int gAddButton(char * _name)
  149. {
  150. if(guino_maxGUIItems > guino_item_counter)
  151. {
  152. gSendCommand(guino_addButton,(byte)guino_item_counter,0);
  153. for (int i = 0; i < strlen(_name); i++){
  154. gSendCommand(guino_addChar,(byte)guino_item_counter,(int)_name[i]);
  155. }
  156. guino_item_counter++;
  157. return guino_item_counter-1;
  158. }
  159. return -1;
  160. }
  161. void gAddColumn()
  162. {
  163. gSendCommand(guino_addColumn,0,0);
  164. }
  165. int gAddLabel(char * _name, int _size)
  166. {
  167. if(guino_maxGUIItems > guino_item_counter)
  168. {
  169. gSendCommand(guino_addLabel,(byte)guino_item_counter,_size);
  170. for (int i = 0; i < strlen(_name); i++){
  171. gSendCommand(guino_addChar,(byte)guino_item_counter,(int)_name[i]);
  172. }
  173. guino_item_counter++;
  174. return guino_item_counter-1;
  175. }
  176. return -1;
  177. }
  178. int gAddSpacer(int _size)
  179. {
  180. if(guino_maxGUIItems > guino_item_counter)
  181. {
  182. gSendCommand(guino_addSpacer,(byte)guino_item_counter,_size);
  183. guino_item_counter++;
  184. return guino_item_counter-1;
  185. }
  186. return -1;
  187. }
  188. int gAddToggle(char * _name, int * _variable)
  189. {
  190. if(guino_maxGUIItems > guino_item_counter)
  191. {
  192. guino_item_values[guino_item_counter] =_variable ;
  193. gGetSavedValue(guino_item_counter, _variable);
  194. gSendCommand(guino_addToggle,(byte)guino_item_counter,*_variable);
  195. for (int i = 0; i < strlen(_name); i++){
  196. gSendCommand(guino_addChar,(byte)guino_item_counter,(int)_name[i]);
  197. }
  198. guino_item_counter++;
  199. return guino_item_counter-1;
  200. }
  201. return -1;
  202. }
  203. int gAddFixedGraph(char * _name,int _min,int _max,int _bufferSize, int * _variable, int _size)
  204. {
  205. if(guino_maxGUIItems > guino_item_counter)
  206. {
  207. gAddLabel(_name,guino_small);
  208. guino_item_values[guino_item_counter] =_variable ;
  209. gGetSavedValue(guino_item_counter, _variable);
  210. gSendCommand(guino_addWaveform,(byte)guino_item_counter,_size);
  211. gSendCommand(guino_setMax,(byte)guino_item_counter,_max);
  212. gSendCommand(guino_setMin,(byte)guino_item_counter,_min);
  213. gSendCommand(guino_setFixedGraphBuffer,(byte)guino_item_counter,_bufferSize);
  214. guino_item_counter++;
  215. return guino_item_counter-1;
  216. }
  217. return -1;
  218. }
  219. int gAddMovingGraph(char * _name,int _min,int _max, int * _variable, int _size)
  220. {
  221. if(guino_maxGUIItems > guino_item_counter)
  222. {
  223. gAddLabel(_name,guino_small);
  224. guino_item_values[guino_item_counter] =_variable ;
  225. gGetSavedValue(guino_item_counter, _variable);
  226. gSendCommand(guino_addMovingGraph,(byte)guino_item_counter,_size);
  227. gSendCommand(guino_setMax,(byte)guino_item_counter,_max);
  228. gSendCommand(guino_setMin,(byte)guino_item_counter,_min);
  229. guino_item_counter++;
  230. return guino_item_counter-1;
  231. }
  232. return -1;
  233. }
  234. int gUpdateLabel(int _item, char * _text)
  235. {
  236. gSendCommand(guino_clearLabel,(byte)_item,0);
  237. for (int i = 0; i < strlen(_text); i++){
  238. gSendCommand(guino_addChar,(byte)_item,(int)_text[i]);
  239. }
  240. }
  241. int gAddRotarySlider(int _min,int _max, char * _name, int * _variable)
  242. {
  243. if(guino_maxGUIItems > guino_item_counter)
  244. {
  245. guino_item_values[guino_item_counter] =_variable ;
  246. gGetSavedValue(guino_item_counter, _variable);
  247. gSendCommand(guino_addRotarySlider,(byte)guino_item_counter,*_variable);
  248. gSendCommand(guino_setMax,(byte)guino_item_counter,_max);
  249. gSendCommand(guino_setMin,(byte)guino_item_counter,_min);
  250. for (int i = 0; i < strlen(_name); i++){
  251. gSendCommand(guino_addChar,(byte)guino_item_counter,(int)_name[i]);
  252. }
  253. guino_item_counter++;
  254. gUpdateValue(_variable);
  255. return guino_item_counter-1;
  256. }
  257. return -1;
  258. }
  259. int gAddSlider(int _min,int _max, char * _name, int * _variable)
  260. {
  261. if(guino_maxGUIItems > guino_item_counter)
  262. {
  263. guino_item_values[guino_item_counter] =_variable ;
  264. gGetSavedValue(guino_item_counter, _variable);
  265. gSendCommand(guino_addSlider,(byte)guino_item_counter,*_variable);
  266. gSendCommand(guino_setMax,(byte)guino_item_counter,_max);
  267. gSendCommand(guino_setMin,(byte)guino_item_counter,_min);
  268. for (int i = 0; i < strlen(_name); i++){
  269. gSendCommand(guino_addChar,(byte)guino_item_counter,(int)_name[i]);
  270. }
  271. guino_item_counter++;
  272. gUpdateValue(_variable);
  273. return guino_item_counter-1;
  274. }
  275. return -1;
  276. }
  277. void gUpdateValue(int _item)
  278. {
  279. gSendCommand(guino_setValue,_item, *guino_item_values[_item]);
  280. }
  281. void gUpdateValue(int * _variable)
  282. {
  283. int current_id = -1;
  284. for(int i = 0; i < guino_item_counter; i++)
  285. {
  286. if(guino_item_values[i] == _variable)
  287. {
  288. current_id = i;
  289. gUpdateValue(current_id);
  290. }
  291. }
  292. // if(current_id != -1)
  293. }
  294. void gSendCommand(byte _cmd, byte _item, int _value)
  295. {
  296. if(!internalInit && (guidino_initialized || guino_executed || _cmd == guino_iamhere) )
  297. {
  298. guino_data.cmd = _cmd;
  299. guino_data.item = _item;
  300. guino_data.value = _value;
  301. ET.sendData();
  302. }
  303. }