HapticLib  0.7
Haptic Feedback Library for embedded systems
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
hapticLib.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Leonardo Guardati <leonardo@guardati.it>
3  *
4  * Permission to use, copy, modify, and/or distribute this
5  * software for any purpose with or without fee is hereby
6  * granted, provided that the above copyright notice and this
7  * permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS
10  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
11  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
12  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
15  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
17  * USE OR PERFORMANCE OF THIS SOFTWARE.
18  *
19  */
47 #define HL_SYSTEM_FILE
48 
49 #include "hapticLib.h"
50 
52 
70 system_desc SystemDesc;
71 
109 haptor_desc *hl_configure(uint32_t pwmFreq, uint8_t samplesDelay, uint8_t numHaptors) {
110 
111  uint8_t i = 0;
112 
113  // Input validation MISSING
114 
117 
118 #ifdef HL_DEBUG
119  USART_Configuration();
120 #endif
121 
122  if( numHaptors > MAX_HAPTORS )
123  {
124 #ifdef HL_DEBUG
125  send_string("hl_configure(): This platform doesn't support \0");
126  send_int( numHaptors );
127  send_string(" haptors. (MAX=\0");
129  send_string(".\r\n\0");
130 #endif
131  return NULL;
132  }
133 
134  SystemDesc.num_haptors = numHaptors;
135 
136  SystemDesc.pwm_freq = pwmFreq;
137  SystemDesc.samples_delay = samplesDelay;
138 
139  for( i=0 ; i < SystemDesc.num_haptors ; i++ )
140  SystemDesc.patterns[i].name = Null;
141 
142  // todo clarify the interface with Platform specific module
143  // for haptor initialization.
144  TIM_Configuration(pwmFreq);
145 
146  // todo this is the temporary hardware activation...
147  // until a clear interface is defined.
148  for( i=0 ; i< SystemDesc.num_haptors ; i++ )
149  {
150  SystemDesc.haptors[i].id = i+1; //temporary
151 
152  if( TIM_Channel_Enable(i+1) )
153  {
154 #ifdef HL_DEBUG
155  send_string("hl_configure(): Problem activating Hardware PWM!\r\n\0");
156 #endif
157  return NULL;
158  }
159  }
160 
161  for( i=0 ; i < SystemDesc.num_haptors ; i++ )
162  {
163  SystemDesc.haptors[i].min_duty = 0x0000;
164  SystemDesc.haptors[i].max_duty = 0xFFFF;
165  SystemDesc.haptors[i].activePattern = NULL;
166  SystemDesc.haptors[i].nextHaptor = NULL;
167  }
168 
169  return SystemDesc.haptors;
170 }
171 
210 {
211  uint8_t i=0;
212 
213  // Input validation
214  if( Null >= patternName || patternName >= Num_Patterns_Available)
215  {
216 #ifdef HL_DEBUG
217  send_string("hl_initPattern(): patternName is Invalid!\r\n\0");
218 #endif
219  return NULL;
220  }
221 
222  // Search an available pattern slot and initialize it.
223  for( i=0 ; i < SystemDesc.num_haptors ; i++ )
224  {
225  if( SystemDesc.patterns[i].name == Null )
226  {
227  SystemDesc.patterns[i].name = patternName;
228  SystemDesc.patterns[i].userParams = userParams;
229  SystemDesc.patterns[i].continuator = NULL;
230  SystemDesc.patterns[i].activeHaptorList = NULL;
231 
232  return &SystemDesc.patterns[i];
233  }
234  }
235 
236  // Else error.
237 #ifdef HL_DEBUG
238  send_string("hl_initPattern(): No more Pattern can be created, every haptor is busy!\r\n\0");
239 #endif
240  return NULL;
241 
242 }
243 
281 {
282  haptor_desc *tmp = NULL;
283 
284  // Input validation: haptor
285  if( ( newHaptor == NULL ) || ( newHaptor->activePattern != NULL ) )
286  {
287 #ifdef HL_DEBUG
288  send_string("hl_addHaptor(): haptor invalid or busy with another pattern!\r\n\0");
289 #endif
290  return NULL;
291  }
292 
293  // Input validation: pattern
294  if( ( pattern == NULL ) || \
295  ( Null >= pattern->name ) || ( pattern->name >= Num_Patterns_Available))
296  {
297 #ifdef HL_DEBUG
298  send_string("hl_addHaptor(): This pattern is not valid or not initialized!\r\n\0");
299 #endif
300  return NULL;
301  }
302 
303  // Add the haptor to the pattern's haptor list!
304  tmp = pattern->activeHaptorList;
305 
306  if( pattern->activeHaptorList == NULL )
307  pattern->activeHaptorList = newHaptor;
308  else
309  {
310  tmp = pattern->activeHaptorList;
311  while( tmp->nextHaptor )
312  tmp = tmp->nextHaptor;
313  tmp->nextHaptor = newHaptor;
314  }
315 
316  newHaptor->activePattern = pattern;
317  newHaptor->nextHaptor = NULL;
318 
319  return pattern;
320 }
321 
364 uint8_t hl_startPattern(pattern_desc * pattern)
365 {
366  //Input Validation
367  if( ( pattern == NULL ) || \
368  ( Null >= pattern->name ) || ( pattern->name >= Num_Patterns_Available))
369  {
370 #ifdef HL_DEBUG
371  send_string("hl_startPattern(): This pattern is not valid or not initialized.\r\n\0");
372 #endif
373  return 1;
374  }
375 
376  if( patternMap[pattern->name](pattern) )
377  {
378 #ifdef HL_DEBUG
379  send_string("hl_startPattern(): Problem in pattern Initiator.\r\n\0");
380 #endif
381  return 1;
382  }
383 
384  return 0;
385 }
386 
412 uint8_t hl_stopPattern(pattern_desc *pattern)
413 {
414  haptor_desc *tmp = NULL;
415  haptor_desc *tmp2 = NULL;
416 
417  // Input validation
418  if( (pattern == NULL) || \
419  ( Null >= pattern->name ) || ( pattern->name >= Num_Patterns_Available))
420  {
421 #ifdef HL_DEBUG
422  send_string("hl_stopPattern(): This pattern is not valid or not initialized.\r\n\0");
423 #endif
424  return 1;
425  }
426 
427  if( pattern->continuator == NULL )
428  {
429 #ifdef HL_DEBUG
430  send_string("hl_stopPattern(): This pattern is not active.\r\n\0");
431 #endif
432  return 1;
433  }
434 
435  if( pattern->activeHaptorList == NULL )
436  {
437 #ifdef HL_DEBUG
438  send_string("hl_stopPattern(): This pattern has no haptor linked to it (STRANGE).\r\n\0");
439 #endif
440  return 1;
441  }
442 
443  tmp = pattern->activeHaptorList;
444  tmp2 = NULL;
445  do
446  {
447  TIM_Channel_DutyChanger( 0, tmp->id );
448 
449  tmp2 = tmp->nextHaptor;
450 
451  tmp->nextHaptor = NULL;
452  tmp->activePattern = NULL;
453 
454  tmp = tmp2;
455  }
456  while( tmp );
457 
458  pattern->name = Null;
459  pattern->userParams = NULL;
460  pattern->continuator = NULL;
461  pattern->activeHaptorList = NULL;
462 
463  return 0;
464 }