pig2vcd.c
Go to the documentation of this file.
00001 /*
00002 This is free and unencumbered software released into the public domain.
00003 
00004 Anyone is free to copy, modify, publish, use, compile, sell, or
00005 distribute this software, either in source code form or as a compiled
00006 binary, for any purpose, commercial or non-commercial, and by any
00007 means.
00008 
00009 In jurisdictions that recognize copyright laws, the author or authors
00010 of this software dedicate any and all copyright interest in the
00011 software to the public domain. We make this dedication for the benefit
00012 of the public at large and to the detriment of our heirs and
00013 successors. We intend this dedication to be an overt act of
00014 relinquishment in perpetuity of all present and future rights to this
00015 software under copyright law.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00018 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00019 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00020 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00021 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00022 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00023 OTHER DEALINGS IN THE SOFTWARE.
00024 
00025 For more information, please refer to <http://unlicense.org/>
00026 */
00027 
00028 /*
00029 This version is for pigpio version 3+
00030 */
00031 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <stdint.h>
00035 #include <unistd.h>
00036 #include <time.h>
00037 #include <sys/types.h>
00038 #include <sys/stat.h>
00039 #include <sys/time.h>
00040 #include <fcntl.h>
00041 
00042 #include "pigpio.h"
00043 
00044 /*
00045 This software converts pigpio notification reports
00046 into a VCD format understood by GTKWave.
00047 */
00048 
00049 #define RS (sizeof(gpioReport_t))
00050 
00051 static char * timeStamp()
00052 {
00053    static char buf[32];
00054 
00055    struct timeval now;
00056    struct tm tmp;
00057 
00058    gettimeofday(&now, NULL);
00059 
00060    localtime_r(&now.tv_sec, &tmp);
00061    strftime(buf, sizeof(buf), "%F %T", &tmp);
00062 
00063    return buf;
00064 }
00065 
00066 int symbol(int bit)
00067 {
00068    if (bit < 26) return ('A' + bit);
00069    else          return ('a' + bit - 26);
00070 }
00071 
00072 int main(int argc, char * argv[])
00073 {
00074    int b, r, v;
00075    uint32_t t0;
00076    uint32_t lastLevel, changed;
00077 
00078    gpioReport_t report;
00079 
00080    r=read(STDIN_FILENO, &report, RS);
00081 
00082    if (r != RS) exit(-1);
00083 
00084    printf("$date %s $end\n", timeStamp());
00085    printf("$version pig2vcd V1 $end\n");
00086    printf("$timescale 1 us $end\n");
00087    printf("$scope module top $end\n");
00088 
00089    for (b=0; b<32; b++)
00090       printf("$var wire 1 %c %d $end\n", symbol(b), b);
00091         
00092    printf("$upscope $end\n");
00093    printf("$enddefinitions $end\n");
00094          
00095    t0 = report.tick;
00096    lastLevel =0;
00097 
00098    while ((r=read(STDIN_FILENO, &report, RS)) == RS)
00099    {
00100       if (report.level != lastLevel)
00101       {
00102          printf("#%u\n", report.tick - t0);
00103 
00104          changed = report.level ^ lastLevel;
00105 
00106          lastLevel = report.level;
00107 
00108          for (b=0; b<32; b++)
00109          {
00110             if (changed & (1<<b))
00111             {
00112                if (report.level & (1<<b)) v='1'; else v='0';
00113 
00114                printf("%c%c\n", v, symbol(b));
00115             }
00116          }
00117       }
00118    }
00119    return 0;
00120 }
00121 


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Thu Jun 6 2019 20:43:57