ApelPro

TestIC

Mar 7th, 2021
656
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define AND2OUT 2
  2. #define AND2A 4
  3. #define AND2B 3
  4. #define AND1OUT 5
  5. #define AND1A 7
  6. #define AND1B 6
  7.  
  8. #define OR2A 10
  9. #define OR2B 9
  10. #define OR2OUT 8
  11.  
  12. #define MAX_GATES 10
  13.  
  14. typedef struct gate_t {
  15.   byte inA, inB;
  16.   byte out;
  17.   byte* inValA, *inValB;
  18.   byte outVal;
  19. } gate_t; // t jelöli, hogy típus
  20.  
  21. typedef struct gates_t {
  22.     gate_t* gates[MAX_GATES];
  23.     byte numberOfGates;
  24. } gates_t;
  25.  
  26. void init_gate( struct gate_t* gate, byte inA, byte inB, byte out ) {
  27.   gate->inA = inA;
  28.   gate->inB = inB;
  29.   gate->out = out;
  30.   gate->inValA = NULL;
  31.   gate->inValB = NULL;
  32.   gate->outVal = NULL;
  33. }
  34.  
  35. void init_gates( struct gates_t* gates ) {
  36.   gates->numberOfGates = 0;
  37. }
  38.  
  39. bool add_gates( struct gates_t* gates, struct gate_t* gate ) {
  40.   if ( gates->numberOfGates < MAX_GATES ) {
  41.     gates->gates[gates->numberOfGates] = gate;
  42.     gates->numberOfGates++;
  43.     return true;
  44.   }
  45.   return false;
  46. }
  47.  
  48. void run_gates( struct gates_t *gates ) {
  49.   for ( int i = 0; i < gates->numberOfGates; i++ ) {
  50.     digitalWrite( (gates->gates[i])->inA, *((gates->gates[i])->inValA) );
  51.     digitalWrite( (gates->gates[i])->inB, *((gates->gates[i])->inValB) );
  52.     (gates->gates[i])->outVal = digitalRead( (gates->gates[i])->out );
  53.    
  54.     Serial.println( "i = " + String( i ) + ":" );
  55.     Serial.print( *((gates->gates[i])->inValA) );    
  56.     Serial.print( *((gates->gates[i])->inValB) );
  57.     Serial.print( " => " );
  58.     Serial.println( (gates->gates[i])->outVal );
  59.    
  60.     delay( 1000 );
  61.   }
  62. }
  63.  
  64. gates_t gates;
  65. gate_t AND1;
  66. gate_t AND2;
  67. gate_t OR2;
  68.  
  69. byte A = 0, B = 0, C = 0, D = 0;
  70.  
  71. void setup()
  72. {
  73.   pinMode(AND2A, OUTPUT);
  74.   pinMode(AND2B, OUTPUT );
  75.   pinMode(AND2OUT, INPUT );
  76.  
  77.   pinMode(AND1A, OUTPUT);
  78.   pinMode(AND1B, OUTPUT );
  79.   pinMode(AND1OUT, INPUT );
  80.    
  81.   pinMode( OR2OUT, INPUT );
  82.   pinMode( OR2A, OUTPUT );
  83.   pinMode( OR2B, OUTPUT );
  84.  
  85.   Serial.begin( 115200 );
  86.  
  87.   init_gate( &AND1, AND1A, AND1B, AND1OUT );
  88.   init_gate( &AND2, AND2A, AND2B, AND2OUT );
  89.   init_gate( &OR2, OR2A, OR2B, OR2OUT );
  90.  
  91.   OR2.inValA = &AND1.outVal;
  92.   OR2.inValB = &AND2.outVal;
  93.   AND1.inValA = &A;
  94.   AND1.inValB = &B;
  95.   AND2.inValA = &C;
  96.   AND2.inValB = &D;
  97.  
  98.   init_gates( &gates );
  99.   add_gates( &gates, &AND1 );
  100.   add_gates( &gates, &AND2 );
  101.   add_gates( &gates, &OR2 );
  102.  
  103.   A = 0;
  104.   B = 1;
  105.   C = 1;
  106.   D = 0;
  107. }
  108.  
  109. void loop()
  110. {  
  111.   run_gates( &gates );
  112. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×