Pārlūkot izejas kodu

cmDspBuiltIn.c, cmDspValue.h/c : text in 'label' object can now be updated with any variable type.

master
kevin 4 gadus atpakaļ
vecāks
revīzija
90bfc6c05b
3 mainītis faili ar 141 papildinājumiem un 10 dzēšanām
  1. 67
    10
      src/dsp/cmDspBuiltIn.c
  2. 73
    0
      src/dsp/cmDspValue.c
  3. 1
    0
      src/dsp/cmDspValue.h

+ 67
- 10
src/dsp/cmDspBuiltIn.c Parādīt failu

@@ -1702,7 +1702,8 @@ cmDspClass_t*  cmMeterClassCons( cmDspCtx_t* ctx )
1702 1702
 enum
1703 1703
 {
1704 1704
   kInLbId,
1705
-  kAlignLbId
1705
+  kAlignLbId,
1706
+  kTextLbId
1706 1707
 };
1707 1708
 
1708 1709
 cmDspClass_t _cmLabelDC;
@@ -1710,27 +1711,68 @@ cmDspClass_t _cmLabelDC;
1710 1711
 typedef struct
1711 1712
 {
1712 1713
   cmDspInst_t inst;
1714
+  cmChar_t* label;
1713 1715
 } cmDspLabel_t;
1714 1716
 
1715 1717
 cmDspInst_t*  _cmDspLabelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
1716 1718
 {
1717
-  cmDspVarArg_t args[] =
1719
+  const unsigned  argCnt = 3;
1720
+  cmDspVarArg_t   args[argCnt+1];
1721
+  cmChar_t*       label  = NULL;
1722
+  unsigned        align  = kLeftAlignDuiId;
1723
+  va_list         vl1;
1724
+  
1725
+  va_copy(vl1,vl);
1726
+
1727
+  if( va_cnt < 1 )
1718 1728
   {
1719
-    { "in",   kInLbId,    0, 0,  kInDsvFl | kStrzDsvFl  | kReqArgDsvFl,  "LabelText" },
1720
-    { "align",kAlignLbId, 0, 0,  kInDsvFl | kUIntDsvFl  | kOptArgDsvFl,  "Alignment 0=right 1=left 2=center" },
1721
-    { NULL, 0, 0, 0, 0 }
1722
-  };
1729
+    va_end(vl1);
1730
+    cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'label' constructor argument list must contain at least one argument.");
1731
+    return NULL;
1732
+  }
1733
+
1734
+  // get the default label
1735
+  const char* clabel = va_arg(vl,const char*);
1736
+  if( clabel != NULL && strlen(clabel) > 0 )
1737
+  {
1738
+    label = cmLhAllocStr(ctx->lhH,clabel);
1739
+    printf("%s\n",label);
1740
+  }
1741
+
1742
+  // if an alignment id was provided
1743
+  if( va_cnt > 1 )
1744
+    align = va_arg(vl,double);
1745
+
1746
+  // setup the arg. config. array.
1747
+  cmDspArgSetup(ctx,args+0,"in",   cmInvalidId, kInLbId,    0,0, kInDsvFl | kTypeDsvMask, "Input to set label" );
1748
+  cmDspArgSetup(ctx,args+1,"align",cmInvalidId, kAlignLbId, 0,0, kInDsvFl | kUIntDsvFl,   "Justification: 0=right 1=center 2=left" );
1749
+  cmDspArgSetup(ctx,args+2,"text", cmInvalidId, kTextLbId,  0,0, kInDsvFl | kStrzDsvFl,   "Label text");
1750
+  cmDspArgSetupNull(args + argCnt);
1723 1751
 
1724
-  cmDspLabel_t* p = cmDspInstAlloc(cmDspLabel_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
1752
+  // create the instance
1753
+  cmDspLabel_t* p = cmDspInstAlloc(cmDspLabel_t,ctx,classPtr,args,instSymId,id,storeSymId,0,vl1);
1725 1754
 
1726
-  cmDspSetDefaultDouble(ctx, &p->inst, kAlignLbId,  0.0, kLeftAlignDuiId);
1755
+  p->label = label;
1727 1756
 
1757
+  // set the default variable values
1758
+  cmDspSetDefaultDouble(ctx, &p->inst, kAlignLbId,  0.0, align);
1759
+  cmDspSetDefaultStrcz( ctx, &p->inst, kTextLbId,  NULL, label==NULL ? "" : label );
1760
+  
1728 1761
   // create the UI control
1729
-  cmDspUiLabelCreate(ctx,&p->inst,kInLbId,kAlignLbId);
1762
+  cmDspUiLabelCreate(ctx,&p->inst,kTextLbId,kAlignLbId);
1730 1763
 
1764
+  va_end(vl1);
1765
+  
1731 1766
   return &p->inst;
1732 1767
 }
1733 1768
 
1769
+cmDspRC_t _cmDspLabelFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
1770
+{
1771
+  cmDspLabel_t* p = (cmDspLabel_t*)inst;
1772
+  cmLhFree(ctx->lhH,p->label);
1773
+  return kOkDspRC;
1774
+}
1775
+
1734 1776
 cmDspRC_t _cmDspLabelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
1735 1777
 {
1736 1778
   cmDspApplyAllDefaults(ctx,inst);
@@ -1739,6 +1781,21 @@ cmDspRC_t _cmDspLabelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t*
1739 1781
 
1740 1782
 cmDspRC_t _cmDspLabelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
1741 1783
 {
1784
+  const unsigned bN = 128;
1785
+  cmChar_t b[ bN+1 ];
1786
+
1787
+  // if this event is arriving on the 'in' port ...
1788
+  if( evt->dstVarId == kInLbId )
1789
+  {
1790
+    cmDspLabel_t* p = (cmDspLabel_t*)inst;
1791
+    // ... then convert it to a string
1792
+    cmDsvToString( evt->valuePtr, p->label, b, bN );
1793
+
1794
+    // and set the 'label' variable 
1795
+    return cmDspSetStrcz(ctx, inst, kTextLbId, b );
1796
+  }
1797
+
1798
+  
1742 1799
   return cmDspSetEvent(ctx,inst,evt);
1743 1800
 }
1744 1801
 
@@ -1747,7 +1804,7 @@ cmDspClass_t*  cmLabelClassCons( cmDspCtx_t* ctx )
1747 1804
   cmDspClassSetup(&_cmLabelDC,ctx,"Label",
1748 1805
     NULL,
1749 1806
     _cmDspLabelAlloc,
1750
-    NULL,
1807
+    _cmDspLabelFree,
1751 1808
     _cmDspLabelReset,
1752 1809
     NULL,
1753 1810
     _cmDspLabelRecv,

+ 73
- 0
src/dsp/cmDspValue.c Parādīt failu

@@ -1527,3 +1527,76 @@ void cmDsvPrint( const cmDspValue_t* vp, const cmChar_t* label, cmRpt_t* rpt )
1527 1527
     }
1528 1528
   }
1529 1529
 }
1530
+
1531
+void cmDsvToString( const cmDspValue_t* vp, const cmChar_t* label, cmChar_t* s, unsigned sN )
1532
+{
1533
+  vp = _vcptr(vp); 
1534
+
1535
+  const char* noLbl="";
1536
+  const char* lbl  = label==NULL? noLbl : label;
1537
+
1538
+  if( cmDsvIsMtx(vp) )
1539
+  {
1540
+    unsigned i,j;
1541
+    unsigned rn = cmDsvCols(vp);
1542
+    unsigned cn = cmDsvRows(vp);
1543
+    for(i=0; i<rn; ++i)
1544
+    {
1545
+      for(j=0; j<cn && sN>2; ++j)
1546
+      {
1547
+        switch( cmDsvBasicType(vp) )
1548
+        {
1549
+          case kCharDsvFl:   snprintf(s,sN,"%s%c ",lbl,vp->u.m.u.cp[  (j*rn) + i ]); break;
1550
+          case kUCharDsvFl:  snprintf(s,sN,"%s%c ",lbl,vp->u.m.u.ucp[ (j*rn) + i ]); break;
1551
+          case kShortDsvFl:  snprintf(s,sN,"%s%i ",lbl,vp->u.m.u.sp[  (j*rn) + i ]); break;
1552
+          case kUShortDsvFl: snprintf(s,sN,"%s%i ",lbl,vp->u.m.u.usp[ (j*rn) + i ]); break;
1553
+          case kLongDsvFl:   snprintf(s,sN,"%s%li ",lbl,vp->u.m.u.lp[  (j*rn) + i ]); break;
1554
+          case kULongDsvFl:  snprintf(s,sN,"%s%li ",lbl,vp->u.m.u.ulp[ (j*rn) + i ]); break;
1555
+          case kIntDsvFl:    snprintf(s,sN,"%s%i ",lbl,vp->u.m.u.ip[  (j*rn) + i ]); break;
1556
+          case kUIntDsvFl:   snprintf(s,sN,"%s%i ",lbl,vp->u.m.u.up[  (j*rn) + i ]); break;
1557
+          case kFloatDsvFl:  snprintf(s,sN,"%s%f ",lbl,vp->u.m.u.fp[  (j*rn) + i ]); break;
1558
+          case kDoubleDsvFl: snprintf(s,sN,"%s%f ",lbl,vp->u.m.u.dp[  (j*rn) + i ]); break;
1559
+          case kSampleDsvFl: snprintf(s,sN,"%s%f ",lbl,vp->u.m.u.ap[  (j*rn) + i ]); break;
1560
+          case kRealDsvFl:   snprintf(s,sN,"%s%f ",lbl,vp->u.m.u.rp[  (j*rn) + i ]); break;
1561
+          case kStrzDsvFl:   snprintf(s,sN,"%s%s ",lbl,vp->u.m.u.zp[  (j*rn) + i ]); break;
1562
+          case kJsonDsvFl:   cmJsonLeafToString(vp->u.m.u.jp[ (j*rn) + i ],s,sN);   break;    
1563
+          default:
1564
+            { assert(0); }
1565
+        }
1566
+
1567
+        unsigned n = strlen(s);
1568
+        sN -= n;
1569
+        s  += n;
1570
+        
1571
+        
1572
+      }
1573
+      if( sN > 2 )
1574
+        snprintf(s,sN,"\n");
1575
+    }
1576
+  }
1577
+  else
1578
+  {
1579
+    switch( cmDsvBasicType(vp) )
1580
+    {
1581
+      case kBoolDsvFl:   snprintf(s,sN,"%s%s ",lbl,vp->u.b ? "true" : "false"); break;
1582
+      case kCharDsvFl:   snprintf(s,sN,"%s%c ",lbl,vp->u.c);  break;
1583
+      case kUCharDsvFl:  snprintf(s,sN,"%s%c ",lbl,vp->u.uc); break;
1584
+      case kShortDsvFl:  snprintf(s,sN,"%s%i ",lbl,vp->u.s);  break;
1585
+      case kUShortDsvFl: snprintf(s,sN,"%s%i ",lbl,vp->u.us); break;
1586
+      case kLongDsvFl:   snprintf(s,sN,"%s%li ",lbl,vp->u.l);  break;
1587
+      case kULongDsvFl:  snprintf(s,sN,"%s%li ",lbl,vp->u.ul); break;
1588
+      case kIntDsvFl:    snprintf(s,sN,"%s%i ",lbl,vp->u.i);  break;
1589
+      case kUIntDsvFl:   snprintf(s,sN,"%s%i ",lbl,vp->u.u);  break;
1590
+      case kFloatDsvFl:  snprintf(s,sN,"%s%f ",lbl,vp->u.f);  break;
1591
+      case kDoubleDsvFl: snprintf(s,sN,"%s%f ",lbl,vp->u.d);  break;
1592
+      case kSampleDsvFl: snprintf(s,sN,"%s%f ",lbl,vp->u.a);  break;
1593
+      case kRealDsvFl:   snprintf(s,sN,"%s%f ",lbl,vp->u.r);  break;
1594
+      case kPtrDsvFl:    snprintf(s,sN,"%s%p ",lbl,vp->u.vp); break;
1595
+      case kStrzDsvFl:   snprintf(s,sN,"%s%s ",lbl,vp->u.z);  break;
1596
+      case kSymDsvFl:    snprintf(s,sN,"%s%i ",lbl,vp->u.u); break;
1597
+      case kJsonDsvFl:   cmJsonLeafToString(vp->u.j,s,sN);   break;    
1598
+      default:
1599
+        { assert(0); }
1600
+    }
1601
+  }
1602
+}

+ 1
- 0
src/dsp/cmDspValue.h Parādīt failu

@@ -331,6 +331,7 @@ extern "C" {
331 331
   cmDsvRC_t cmDsvDeserializeJson(     cmDspValue_t* vp, cmJsonH_t jsH );
332 332
 
333 333
   void     cmDsvPrint( const cmDspValue_t* vp, const cmChar_t* label,  cmRpt_t* rpt );
334
+  void     cmDsvToString( const cmDspValue_t* vp, const cmChar_t* label,  cmChar_t* s, unsigned sN );
334 335
 
335 336
   //)
336 337
   

Notiek ielāde…
Atcelt
Saglabāt