Changeset 1002

Show
Ignore:
Timestamp:
11/17/08 11:41:40 (7 weeks ago)
Author:
liamMT
Message:

- stub out OscRangeHelper? class to deal with numeric OSC patterns

Location:
firmware/branches/cpp
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • firmware/branches/cpp/core/makingthings/osc_cpp.cpp

    r971 r1002  
    77#include <string.h> 
    88#include <stdlib.h> 
     9#include <stdio.h> 
     10#include <ctype.h> 
    911 
    1012extern "C" { 
     
    7779} 
    7880 
    79 bool OSCC::receivePacket( int channel, char* packet, int length ) 
     81bool OSCC::receivePacket( OscTransport t, char* packet, int length ) 
    8082{ 
    8183  // Got a packet.  Unpacket. 
     
    115117} 
    116118 
     119OscRangeHelper::OscRangeHelper( OscMessage* msg, int element, int max, int min ) 
     120{ 
     121  remaining = 0; 
     122  single = -1; 
     123  if( !msg->address ) 
     124    return; 
     125  const char* p = strchr(msg->address, '/'); // should give us the very first char of the OSC message 
     126  if( !p++ ) // step to the beginning of the address element 
     127    return; 
     128  int j; 
     129  for( j = 0; j < element; j++ ) 
     130  { 
     131    p = strchr( p, '/'); 
     132    if(!p++) 
     133      return; 
     134  } 
     135   
     136  int n = 0; 
     137  int digits = 0; 
     138  // from OSC_NumberMatch() 
     139  while ( isdigit( *p ) ) 
     140  { 
     141    digits++; 
     142    n = n * 10 + ( *p++ - '0' ); 
     143  } 
     144 
     145  bits = -1; 
     146  if ( n >= max ) 
     147    return; // -1; 
     148 
     149  switch ( *p ) 
     150  { 
     151    case '*': 
     152    case '?': 
     153    case '[': 
     154    case '{': 
     155    { 
     156      int i; 
     157      int b = 0; 
     158      char s[ 5 ]; 
     159      for ( i = max - 1; i >= min ; i-- ) 
     160      { 
     161        b <<= 1; 
     162        sprintf( s, "%d", i ); 
     163        // if ( Osc_PatternMatch( p, s ) ) 
     164        // { 
     165        //   b |= 1; 
     166        //   remaining++; 
     167        // } 
     168      } 
     169      bits = b; 
     170      current = max; 
     171      return; // -1; 
     172    } 
     173    default: 
     174      if ( digits == 0 ) 
     175        return; // -1; 
     176      else 
     177        single = n; 
     178        return; // n; 
     179  } 
     180} 
     181 
     182bool OscRangeHelper::hasNextIndex( ) 
     183{ 
     184  return remaining > 0; 
     185} 
     186 
     187int OscRangeHelper::nextIndex( ) 
     188{ 
     189  int retval = 0; 
     190  if( single != -1 ) 
     191  { 
     192    remaining = 0; 
     193    return single; 
     194  } 
     195  bool cont = true; 
     196  while ( bits > 0 && remaining && cont ) 
     197  {  
     198    if ( bits & 1 ) 
     199    { 
     200      retval = current; 
     201      remaining--; 
     202      cont = false; 
     203    } 
     204    current--; 
     205    bits >>= 1; 
     206  } 
     207  return retval; 
     208} 
     209 
    117210int OscHandler::propertyLookup( const char* propertyList[], char* property ) 
    118211{ 
  • firmware/branches/cpp/core/makingthings/osc_cpp.h

    r971 r1002  
    88#define OSC_MAX_HANDLERS 56 
    99#define OSC_MSG_MAX_DATA_ITEMS 20 
    10 // #define OSC_MSG_MAX_LEN 1024 
    1110#define OSC_MAX_MESSAGE_IN   400 
    1211#define OSC_MAX_MESSAGE_OUT  600 
     
    4645}; 
    4746 
     47class OscRangeHelper 
     48{ 
     49public: 
     50  OscRangeHelper( OscMessage* msg, int element, int max, int min = 0 ); 
     51  bool hasNextIndex( ); 
     52  int nextIndex( ); 
     53private: 
     54  int remaining, bits, current, single; 
     55}; 
     56 
    4857class OscHandler 
    4958{ 
    5059public: 
     60  // mandatory 
    5161  virtual int onNewMsg( OscMessage* msg, OscTransport t, int src_addr, int src_port ) = 0; 
    5262  virtual int onQuery( int element ) = 0; 
    5363  virtual const char* name( ) = 0; 
     64  // optional 
     65  bool autoSend( OscTransport t ) { (void)t; return false; } 
    5466protected: 
    5567  int propertyLookup( const char* propertyList[], char* property ); 
     
    8193  OSCC* instance( ) 
    8294  { 
    83     if( _instance ) 
     95    if( !_instance ) 
    8496      _instance = new OSCC( ); 
    8597    return _instance; 
     
    89101  OSCC( ) { } 
    90102  OSCC* _instance; 
    91   bool receivePacket( int channel, char* packet, int length ); 
     103  bool receivePacket( OscTransport t, char* packet, int length ); 
    92104  char* writePaddedString( char* buffer, int* length, char* string ); 
    93105  char* writePaddedBlob( char* buffer, int* length, char* blob, int blen ); 
  • firmware/branches/cpp/libraries/appled/appled_.cpp

    r971 r1002  
    138138  (void)src_port; 
    139139  int replies = 0; 
    140   bool ok; 
    141   int index = msg->addressElementAsInt( 1, &ok ); // address element 1 should be our index 
    142   if( !ok ) 
    143     return replies; 
    144140  char* property = msg->addressElementAsString( 2 ); // address element 2 should be the property 
    145141  if( !property ) 
     
    150146    return replies; 
    151147   
    152   AppLed led(index); 
    153   if( msg->data_count ) // this is a setter 
     148  int index; 
     149  OscRangeHelper rangeHelper(msg, 1, APPLED_COUNT); // element 1 should be our index 
     150  while( rangeHelper.hasNextIndex() ) 
    154151  { 
    155     int value = msg->dataItemAsInt(0); 
    156     switch( prop_index ) 
     152    index = rangeHelper.nextIndex(); 
     153    if( index < 0 || index >= APPLED_COUNT ) 
     154      continue; 
     155    AppLed led(index); 
     156    if( msg->data_count ) // setter...write the value to the led 
    157157    { 
    158       case 0: // state 
    159         led.setState(value); 
    160         break; 
     158      int value = msg->dataItemAsInt(0); 
     159      switch( prop_index ) 
     160      { 
     161        case 0: // state 
     162          led.setState(value); 
     163          break; 
     164      } 
    161165    } 
    162   } 
    163   else // this is a getter 
    164   { 
    165     int value; 
    166     switch( prop_index ) 
     166    else // getter...return a message 
    167167    { 
    168       case 0: // state 
    169         value = led.getState( ); 
    170         // create new message to send it back 
    171         break; 
     168      int value; 
     169      switch( prop_index ) 
     170      { 
     171        case 0: // state 
     172          value = led.getState( ); 
     173          // create new message to send it back 
     174          replies++; 
     175          break; 
     176      } 
    172177    } 
    173178  } 
     
    190195} 
    191196 
    192 // Need a list of property names 
    193 // MUST end in zero 
    194 // static char* AppLedOsc_Name = "appled"; 
    195 // static char* AppLedOsc_PropertyNames[] = { "active", "state", 0 }; // must have a trailing 0 
    196 //  
    197 // int AppLedOsc_PropertySet( int index, int property, int value ); 
    198 // int AppLedOsc_PropertyGet( int index, int property ); 
    199 //  
    200 // // Returns the name of the subsystem 
    201 // const char* AppLedOsc_GetName( ) 
    202 // { 
    203 //   return AppLedOsc_Name; 
    204 // } 
    205 //  
    206 // // Now getting a message.  This is actually a part message, with the first 
    207 // // part (the subsystem) already parsed off. 
    208 // int AppLedOsc_ReceiveMessage( int channel, char* message, int length ) 
    209 // { 
    210 //   int status = Osc_IndexIntReceiverHelper( channel, message, length,  
    211 //                                            APPLED_COUNT, AppLedOsc_Name, 
    212 //                                            AppLedOsc_PropertySet, AppLedOsc_PropertyGet,  
    213 //                                            AppLedOsc_PropertyNames ); 
    214 //  
    215 //   if ( status != CONTROLLER_OK ) 
    216 //     return Osc_SendError( channel, AppLedOsc_Name, status ); 
    217 //   return CONTROLLER_OK; 
    218 // } 
    219 //  
    220 // // Set the index LED, property with the value 
    221 // int AppLedOsc_PropertySet( int index, int property, int value ) 
    222 // { 
    223 //   switch ( property ) 
    224 //   { 
    225 //     case 0:  
    226 //       AppLed_SetActive( index, value ); 
    227 //       break;       
    228 //     case 1: 
    229 //       AppLed_SetState( index, value ); 
    230 //       break; 
    231 //   } 
    232 //   return CONTROLLER_OK; 
    233 // } 
    234 //  
    235 // // Get the index LED, property 
    236 // int AppLedOsc_PropertyGet( int index, int property ) 
    237 // { 
    238 //   int value = 0; 
    239 //   switch ( property ) 
    240 //   { 
    241 //     case 0: 
    242 //       value = AppLed_GetActive( index ); 
    243 //       break; 
    244 //     case 1: 
    245 //       value = AppLed_GetState( index ); 
    246 //       break; 
    247 //   } 
    248 //    
    249 //   return value; 
    250 // } 
    251  
    252197#endif // OSC