implement simulator config file
Stefan Schuermans

Stefan Schuermans commited on 2017-06-04 18:03:13
Showing 11 changed files, with 170 additions and 17 deletions.


change usage on command line - use named options
implement output of simulator config file
... ...
@@ -1 +1,2 @@
1 1
 /*.etp
2
+/*.etps
... ...
@@ -1,6 +1,7 @@
1 1
 DRAWINGS := $(wildcard *.dxf)
2 2
 
3 3
 CONFIGS := $(DRAWINGS:.dxf=.etp)
4
+SIMCONFIGS := $(DRAWINGS:.dxf=.etps)
4 5
 
5 6
 CFGGEN := ../src/etherpix_config_gen
6 7
 
... ...
@@ -8,11 +9,11 @@ CFGGEN := ../src/etherpix_config_gen
8 9
 .SUFFIXES:
9 10
 .SECONDARY:
10 11
 
11
-all: $(CONFIGS)
12
+all: $(CONFIGS) $(SIMCONFIGS)
12 13
 
13
-%.etp: %.dxf $(CFGGEN)
14
-	$(CFGGEN) $< $@
14
+%.etp %.etps: %.dxf $(CFGGEN)
15
+	$(CFGGEN) -d $< -c $*.etp -s $*.etps
15 16
 
16 17
 clean:
17
-	rm -f $(CONFIGS)
18
+	rm -f $(CONFIGS) $(SIMCONFIGS)
18 19
 
... ...
@@ -103,3 +103,18 @@ bool Box::getIntersection(const Box *pBox, Box *pIntersect) const
103 103
   return true;
104 104
 }
105 105
 
106
+void Box::getRelative(const Point &abs, Point &rel) const
107
+{
108
+  Point delta = mTR - mBL;
109
+  if (delta.mX > 0.0) {
110
+    rel.mX = (abs.mX - mBL.mX) / delta.mX;
111
+  } else {
112
+    rel.mX = 0.0; // box width zero - relative position cannot be computed
113
+  }
114
+  if (delta.mY > 0.0) {
115
+    rel.mY = (abs.mY - mBL.mY) / delta.mY;
116
+  } else {
117
+    rel.mY = 0.0; // box height zero - relative position cannot be computed
118
+  }
119
+}
120
+
... ...
@@ -32,6 +32,7 @@ public:
32 32
   void include(const Box &b);
33 33
   bool isIntersecting(const Box *pBox) const;
34 34
   bool getIntersection(const Box *pBox, Box *pIntersect) const;
35
+  void getRelative(const Point &abs, Point &rel) const;
35 36
   Point mBL, mTR;
36 37
 };
37 38
 
... ...
@@ -60,6 +60,13 @@ void Chain::writePixels(std::ostream & strm) const
60 60
     itP->writePixel(strm);
61 61
 }
62 62
 
63
+void Chain::writeSimPixels(std::ostream & strm, const Box & boundsVideo) const
64
+{
65
+  std::vector<Pixel>::const_iterator itP;
66
+  for (itP = mPixels.begin(); itP != mPixels.end(); ++itP)
67
+    itP->writeSimPixel(strm, boundsVideo);
68
+}
69
+
63 70
 size_t Chain::countPixels() const
64 71
 {
65 72
   return mPixels.size();
... ...
@@ -44,6 +44,7 @@ public:
44 44
                unsigned int width, unsigned int height,
45 45
                unsigned int distriNo, unsigned int chainNo);
46 46
   void writePixels(std::ostream & strm) const;
47
+  void writeSimPixels(std::ostream & strm, const Box & boundsVideo) const;
47 48
   size_t countPixels() const;
48 49
 private:
49 50
   unsigned int mPixelCnt;
... ...
@@ -88,6 +88,17 @@ void Distri::writePixels(std::ostream & strm) const
88 88
   }
89 89
 }
90 90
 
91
+void Distri::writeSimPixels(std::ostream & strm, const Box & boundsVideo) const
92
+{
93
+  std::vector<Chain>::const_iterator itC;
94
+  unsigned int c;
95
+  for (itC = mChains.begin(), c = 0; itC != mChains.end(); ++itC, ++c) {
96
+    strm << "output " << mNo << "," << c << " =";
97
+    itC->writeSimPixels(strm, boundsVideo);
98
+    strm << std::endl;
99
+  }
100
+}
101
+
91 102
 size_t Distri::countPixels() const
92 103
 {
93 104
   size_t pixels = 0;
... ...
@@ -45,6 +45,7 @@ public:
45 45
   void writeDistri(std::ostream & strm) const;
46 46
   void writeMapping(std::ostream & strm) const;
47 47
   void writePixels(std::ostream & strm) const;
48
+  void writeSimPixels(std::ostream & strm, const Box & boundsVideo) const;
48 49
   size_t countPixels() const;
49 50
 private:
50 51
   unsigned int mNo, mChainCnt, mPixelCnt;
... ...
@@ -421,15 +421,15 @@ static int createDistri(Distri &distri, const Layer *pLayer,
421 421
  * @param[in] chains number of chains (outputs) per distributor
422 422
  * @param[in] pixels number of pixels in one chain
423 423
  *                                    (i.e. connected to one output)
424
+ * @param[out] boundsVideo bounds of video screen object
424 425
  * @param[out] distris distributor objects
425 426
  * @return true on success, false on error
426 427
  */
427 428
 static bool makeObjects(unsigned int width, unsigned int height,
428 429
                         unsigned int chains, unsigned int pixels,
429
-                        std::vector<Distri> &distris)
430
+                        Box &boundsVideo, std::vector<Distri> &distris)
430 431
 {
431 432
   // get origin and size of pixels in video
432
-  Box boundsVideo;
433 433
   gLayerVideo.getBounds(boundsVideo);
434 434
   Point ptPixel0(boundsVideo.mBL.mX, boundsVideo.mTR.mY);
435 435
   Point ptPixelSz = boundsVideo.mTR - boundsVideo.mBL;
... ...
@@ -484,6 +484,7 @@ static bool makeObjects(unsigned int width, unsigned int height,
484 484
  * @param[in] width width of video in pixels
485 485
  * @param[in] height height of video in pixels
486 486
  * @param[in] distris distributor objects
487
+ * @param[in] strCfgFileName name of config file
487 488
  * @reutrn SUCCESS on success, other value on error
488 489
  */
489 490
 static int writeCfg(unsigned int width, unsigned int height,
... ...
@@ -522,6 +523,45 @@ static int writeCfg(unsigned int width, unsigned int height,
522 523
   return SUCCESS;
523 524
 }
524 525
 
526
+/**
527
+ * @brief write simulator config file
528
+ * @param[in] distris distributor objects
529
+ * @param[in] boundsVideo bounds of video screen object
530
+ * @param[in] strSimCfgFileName name of simulator config file
531
+ * @reutrn SUCCESS on success, other value on error
532
+ */
533
+static int writeSimCfg(const std::vector<Distri> &distris,
534
+                       const Box & boundsVideo,
535
+                       const std::string & strSimCfgFileName)
536
+{
537
+  // open output file
538
+  std::ofstream strm(strSimCfgFileName.c_str(), std::ios::out);
539
+  if (!strm.is_open()) {
540
+    std::cerr << "could not open \"" << strSimCfgFileName
541
+              << "\" for wrinting" << std::endl;
542
+    return ERR_FILE_OPEN;
543
+  }
544
+
545
+  // distributors
546
+  std::vector<Distri>::const_iterator itD;
547
+  for (itD = distris.begin(); itD != distris.end(); ++itD)
548
+    itD->writeDistri(strm);
549
+  strm << std::endl;
550
+
551
+  // mappings
552
+  for (itD = distris.begin(); itD != distris.end(); ++itD)
553
+    itD->writeMapping(strm);
554
+  strm << std::endl;
555
+
556
+  // simulated pixels
557
+  for (itD = distris.begin(); itD != distris.end(); ++itD)
558
+    itD->writeSimPixels(strm, boundsVideo);
559
+  strm << std::endl;
560
+
561
+  strm.close();
562
+  return SUCCESS;
563
+}
564
+
525 565
 /**
526 566
  * @brief main program
527 567
  * @param[in] argc number of parameters
... ...
@@ -531,17 +571,67 @@ static int writeCfg(unsigned int width, unsigned int height,
531 571
 int main(int argc, char *argv[])
532 572
 {
533 573
   // get parameters
534
-  if (argc != 3) {
574
+  bool arg_err = 0, bDxfFileName = false, bCfgFileName = false,
575
+                    bSimCfgFileName= false;
576
+  std::string strDxfFileName, strCfgFileName, strSimCfgFileName;
577
+  // old usage
578
+  if (argc == 3) {
579
+    std::cerr << "warning: old usage, consider using \"-d\" and \"-c\" options" << std::endl;
580
+    strDxfFileName = argv[1];
581
+    bDxfFileName = true;
582
+    strCfgFileName = argv[2];
583
+    bCfgFileName = true;
584
+  } else {
585
+    int argi;
586
+    for (argi = 1; argi < argc; ++argi) {
587
+      if (strcmp(argv[argi], "-c") == 0) {
588
+        ++argi;
589
+        if (argi < argc) {
590
+          strCfgFileName = argv[argi];
591
+          bCfgFileName = true;
592
+        } else {
593
+          std::cerr << "error: missing argument for \"-c\"" << std::endl;
594
+        }
595
+      } else if (strcmp(argv[argi], "-d") == 0) {
596
+        ++argi;
597
+        if (argi < argc) {
598
+          strDxfFileName = argv[argi];
599
+          bDxfFileName = true;
600
+        } else {
601
+          std::cerr << "error: missing argument for \"-d\"" << std::endl;
602
+        }
603
+      } else if (strcmp(argv[argi], "-s") == 0) {
604
+        ++argi;
605
+        if (argi < argc) {
606
+          strSimCfgFileName = argv[argi];
607
+          bSimCfgFileName = true;
608
+        } else {
609
+          std::cerr << "error: missing argument for \"-s\"" << std::endl;
610
+        }
611
+      } else {
612
+        std::cerr << "error: unknown option \"" << argv[argi] << "\", call without arguments for usage" << std::endl;
613
+        arg_err = true;
614
+      }
615
+    } // for argi
616
+    if (! bDxfFileName) {
617
+      std::cerr << "error: missing input drawing (\"-d\")" << std::endl;
618
+      arg_err = true;
619
+    }
620
+  }
621
+  // output help on error
622
+  if (arg_err) {
535 623
     std::cerr << "EtherPix config file generator "
536 624
               << ETPCG_VER_MAJ << "."
537 625
               << ETPCG_VER_MIN << "."
538 626
               << ETPCG_VER_REV << std::endl
539
-              << "usage: " << argv[0] << " <schematic_drawing.dxf> <config.etp>"
627
+              << "usage: " << argv[0] << " [options]" << std::endl
628
+              << "options: -c <config.etp>       configuration file (output)" << std::endl
629
+              << "         -d <drawing.dxf>      schematic drawing (input)" << std::endl
630
+              << "         -s <sim_config.etps>  simulator configuration (output)" << std::endl
631
+              << "old usage: " << argv[0] << " <schematic_drawing.dxf> <config.etp>"
540 632
               << std::endl;
541 633
     return ERR_USAGE;
542 634
   }
543
-  std::string strDxfFileName = argv[1];
544
-  std::string strCfgFileName = argv[2];
545 635
 
546 636
   // read DXF file
547 637
   unsigned int width, height, chains, pixels;
... ...
@@ -551,16 +641,27 @@ int main(int argc, char *argv[])
551 641
   }
552 642
 
553 643
   // make config file objects from DXF layers
644
+  Box boundsVideo;
554 645
   std::vector<Distri> distris;
555
-  if (! makeObjects(width, height, chains, pixels, distris)) {
646
+  if (! makeObjects(width, height, chains, pixels, boundsVideo, distris)) {
556 647
     return ERR_PROC;
557 648
   }
558 649
 
559 650
   // write config file
651
+  if (bCfgFileName) {
560 652
     ret = writeCfg(width, height, distris, strCfgFileName);
561 653
     if ( ret != SUCCESS) {
562 654
       return ret;
563 655
     }
656
+  }
657
+
658
+  // write simulator config file
659
+  if (bSimCfgFileName) {
660
+    ret = writeSimCfg(distris, boundsVideo, strSimCfgFileName);
661
+    if ( ret != SUCCESS) {
662
+      return ret;
663
+    }
664
+  }
564 665
 
565 666
   return SUCCESS;
566 667
 }
... ...
@@ -35,19 +35,17 @@ int Pixel::pixCoord(const Point &pix0, const Point &pixSz,
35 35
                     unsigned int width, unsigned int height)
36 36
 {
37 37
   // get center of pixel
38
-  Box bounds;
39
-  mpObjPixel->getBounds(bounds);
40
-  Point pt;
41
-  bounds.getCenter(pt);
38
+  mpObjPixel->getBounds(mBounds);
39
+  mBounds.getCenter(mCenter);
42 40
 
43 41
   // get coordinates of pixel
44
-  Point delta = pt - pix0;
42
+  Point delta = mCenter - pix0;
45 43
   mX = (unsigned int)(delta.mX / pixSz.mX);
46 44
   mY = (unsigned int)(delta.mY / pixSz.mY);
47 45
 
48 46
   // check pixel coordinates
49 47
   if (mX >= width || mY >= height) {
50
-    std::cerr << "pixel at " << pt.mX << "," << pt.mY
48
+    std::cerr << "pixel at " << mCenter.mX << "," << mCenter.mY
51 49
               << " is out of video frame (" << mX << "," << mY << ")"
52 50
               << std::endl;
53 51
     return -1;
... ...
@@ -60,3 +58,15 @@ void Pixel::writePixel(std::ostream & strm) const
60 58
   strm << " " << mX << "," << mY;
61 59
 }
62 60
 
61
+void Pixel::writeSimPixel(std::ostream & strm, const Box & boundsVideo) const
62
+{
63
+  Point c, bl, tr;
64
+  boundsVideo.getRelative(mCenter, c);
65
+  boundsVideo.getRelative(mBounds.mBL, bl);
66
+  boundsVideo.getRelative(mBounds.mTR, tr);
67
+  double r1 = (c - bl).abs();
68
+  double r2 = (c - tr).abs();
69
+  double r = r1 > r2 ? r1 : r2;
70
+  strm << " " << c.mX << "," << c.mY << "," << r;
71
+}
72
+
... ...
@@ -22,6 +22,7 @@
22 22
 
23 23
 #include <iostream>
24 24
 
25
+#include "box.h"
25 26
 #include "object.h"
26 27
 #include "point.h"
27 28
 
... ...
@@ -39,8 +40,11 @@ public:
39 40
   int pixCoord(const Point &pix0, const Point &pixSz,
40 41
                unsigned int width, unsigned int height);
41 42
   void writePixel(std::ostream & strm) const;
43
+  void writeSimPixel(std::ostream & strm, const Box & boundsVideo) const;
42 44
 private:
43 45
   const Object *mpObjPixel;
46
+  Box mBounds;
47
+  Point mCenter;
44 48
   unsigned int mX, mY;
45 49
 };
46 50
 
47 51