Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you are asking for is <em>possible</em> but with much work on your part.</p> <hr> <p><strong>Approach 1 (think big):</strong></p> <p>What you are looking for is a software implementation equivelant to how PAL's (programmable array logic) and CPLD's (complex programmable logic device) operate. If you read this article, you will get some ideas on how this is done in hardware: </p> <p><a href="http://en.wikipedia.org/wiki/Programmable_logic_device" rel="nofollow">Wikipedia article on PLD's</a></p> <p>A PAL can create arbitray combinational logic rules between a set of inputs and outputs, i.e. anything you can express as a logical equation of AND's, OR's and NOT's can be programmed. It is programmed by "burning" a set of fuses that connect the inputs to logics gates and then to outputs. What is uploaded to these devices is just a set of 0's and 1's.</p> <p>You could implement such a thing in software with an array of 0's and 1's to represent the fuses. The hard code would run over the array and calculate the output. You would need to develop the method to load the array with the correct fuses.</p> <p>A common method by which PAL's are programmed is with the language VHDL. The compiler for this language takes an expression like yours and translates it to the set of AND's, OR's and NOT's that the PAL can use. A search will yield endless discussion of this language, for example:</p> <p><a href="http://www.seas.upenn.edu/~ese171/vhdl/vhdl_primer.html" rel="nofollow">A VHDL tutorial</a></p> <p>You would have to create the compiler that takes the text input and determines the fuses. You would be undertaking some significant tasks: </p> <ul> <li><p>a domain specific language parser (<a href="http://www.antlr.org/" rel="nofollow">I can recommend ANTLR</a>), </p></li> <li><p>a PAL compiler (I can't recommend anyone do this themselves), and </p></li> <li><p>the Arduino code to emulate a PAL.</p></li> </ul> <p>By the time you create your own equivalent of VHDL and PAL's, you'll probably be thinking you could have just put a PAL chip on a proto board and been done.</p> <hr> <p><strong>Approach 2 (more practical):</strong></p> <p>The simpler method to achieve the same result is a truth table. A truth table is the equivalent of some set of logical expressions. To implement a truth table, translate your logic expressions to a table with one row for each input case. Here is an example of a truth table of two inputs that drive an output:</p> <pre><code>IN1 IN2 OUT 0 0 1 0 1 0 1 0 1 1 1 1 </code></pre> <p>The code for such a truth table implementation looks like this:</p> <pre><code>const int IN1 = 6; const int IN2 = 7; const int OUTA = 13; byte table[4] = {1, 0, 1, 1}; void loop() { int in1; int in2; byte outbit; size_t ix; // in1 = digitalRead(IN1); in2 = digitalRead(IN2); ix = in2 * 2 + in1; outbit = table[ix]; digitalWrite(OUTA, outbit); } </code></pre> <p>The complete expression of the logic rules is the array of 4 bytes. To "program" an new output equation, you just send a new set of 4 values, for your specific equation you send "0 0 1 0". Your program would receive the list of values and store them in the table array. As soon as the new values/rules are stored, the function of the output would change.</p> <p>This type of programmable logic is feasible as long as you have enough RAM to store the table. For 4 inputs you just need 16 values, for 5 you need only 32.</p> <hr> <p>Your question raises a keen point in electronics design: you need to pick the right device for the right problem. Code is not always better or easier. Avoid the hammer-nail trap (when your only tool is a hammer every problem looks like a nail). A PAL/CPLD and microcontroller are a powerful combination.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload