Advertisement
gm310509

20 bit encoder test

Mar 25th, 2025
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | Source Code | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int checkRange(const char * varName, int x, int low, int high) {
  5.   if (x < low || x > high) {
  6.     printf("Out of range %s=%d, [%d-%d]\n", varName, x, low, high);
  7.     return 1;
  8.   }
  9.   return 0;
  10. }
  11.  
  12.  
  13. void testPack (int a, int b, int c, int aMax, int bMax, int cMax) {
  14.   aMax += 1;        // Increment the maximums to allow for the entire range (e.g. a max of 9 is 10 values if 0 is included).
  15.   bMax += 1;
  16.   cMax += 1;
  17.   long pack = ((a * bMax) + b) * cMax + c;
  18.  
  19.   printf("Packed value: %d (0x%x)\n", pack, pack);
  20.    
  21.  
  22.   long wrk = pack;
  23.   int cUnpack = wrk % cMax;
  24.   wrk = wrk / cMax;
  25.   int bUnpack = wrk % bMax;
  26.   wrk = wrk / bMax;
  27.   int aUnpack = wrk;
  28.  
  29.   printf("Unpacked:\na=%d, b=%d, c=%d\n", aUnpack, bUnpack, cUnpack);
  30.  
  31. }
  32.  
  33.  
  34. int main (int argc, char * argv[]) {
  35.   printf("Encoder 20 bit.\n");
  36.  
  37.   if (argc != 4) {
  38.     printf("Expected 3 numeric arguments\n");
  39.     printf("%s a b c\n", argv[0]);
  40.     printf(" a: [0-40]\n");
  41.     printf(" b: [0-41]\n");
  42.     printf(" c: [0-443]\n");
  43.     exit(1);
  44.   }
  45.  
  46.   int a = atoi(argv[1]);
  47.   int b = atoi(argv[2]);
  48.   int c = atoi(argv[3]);
  49.  
  50.   printf("a=%d, b=%d, c= %d\n", a, b, c);
  51.  
  52.   int err = checkRange("a", a, 0, 40);
  53.   err |= checkRange("b", b, 0, 41);
  54.   err |= checkRange("c", c, 0, 443);
  55.  
  56.   if (err) {
  57.     exit(1);
  58.   }
  59.  
  60.   testPack (a, b, c, 40, 41, 443);
  61.  
  62.   exit(0);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement