Changeset 1972:68110cc2b9c0 in roaraudio for libroarlight


Ignore:
Timestamp:
06/12/09 02:07:56 (15 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

wrote basic code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroarlight/pwm.c

    r1970 r1972  
    2525#include "libroarlight.h" 
    2626 
     27uint16_t _g_roar_lpwm16[] = { 
     28    0x0000, 0x0001, 0x0101, 0x0111,  0x1111, 0x1115, 0x1515, 0x1555, 
     29    0x5555, 0x5557, 0x5757, 0x5777,  0x7777, 0x777F, 0x7F7F, 0x7FFF, 
     30    0xFFFF 
     31                            }; 
     32 
     33int roar_light_pwm_new (struct roar_lpwm_state * state, int bits ) { 
     34 if ( state == NULL ) 
     35  return -1; 
     36 
     37 if ( bits < 1 || bits > 32 ) 
     38  return -1; 
     39 
     40 state->bits = bits; 
     41 
     42 return roar_light_pwm_set(state, 0); 
     43} 
     44 
     45int roar_light_pwm_set (struct roar_lpwm_state * state, int value) { 
     46 if ( state == NULL ) 
     47  return -1; 
     48 
     49 if ( value < 0 || value > state->bits ) 
     50  return -1; 
     51 
     52 state->value = value; 
     53 
     54 return 0; 
     55} 
     56 
     57int roar_light_pwm_send(struct roar_lpwm_state * state, struct roar_vio_calls * vio, size_t len) { 
     58 char          * buf; 
     59 int16_t       * buf16; 
     60 size_t          todo = len; 
     61 uint64_t        s; 
     62 
     63 if ( state == NULL ) 
     64  return -1; 
     65 
     66 if ( vio == NULL ) 
     67  return -1; 
     68 
     69 if ( state->bits != 16 ) 
     70  return -1; 
     71 
     72 if ( (buf = malloc(len)) == NULL ) 
     73  return -1; 
     74 
     75 buf16 = (int16_t *) buf; 
     76 
     77 while (todo > 1) { 
     78  if ( state->fill < 16 ) { 
     79   s          = _g_roar_lpwm16[state->value]; 
     80   s        <<= state->fill; 
     81   state->s  |= s; 
     82  } 
     83 
     84  *buf16 = state->s & 0xFFFF; 
     85  state->s    >>= 16; 
     86  state->fill  -= 16; 
     87 
     88  buf16++; 
     89  todo -= 2; 
     90 } 
     91 
     92 if ( todo ) { 
     93  buf[len-1]    = state->s & 0xFF; 
     94  state->s    >>= 8; 
     95  state->fill  -= 8; 
     96 } 
     97 
     98 if ( roar_vio_write(vio, buf, len) != len ) { 
     99  free(buf); 
     100  return -1; 
     101 } 
     102 
     103 free(buf); 
     104 
     105 return 0; 
     106} 
     107 
    27108//ll 
Note: See TracChangeset for help on using the changeset viewer.