|
| 1 | +package com.modsim.modules; |
| 2 | + |
| 3 | +import java.awt.Graphics2D; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import com.modsim.modules.parts.LEDRow; |
| 11 | +import com.modsim.modules.parts.Port; |
| 12 | +import com.modsim.modules.ports.Input; |
| 13 | +import com.modsim.res.Colors; |
| 14 | +import com.modsim.simulator.PickableEntity; |
| 15 | +import com.modsim.util.BinData; |
| 16 | +import com.modsim.util.HexReader; |
| 17 | +import com.modsim.util.HexWriter; |
| 18 | + |
| 19 | +public class LEDMatrix extends BaseModule { |
| 20 | + |
| 21 | + private final List<Input> dIn; |
| 22 | + private final Input contIn; |
| 23 | + |
| 24 | + private final List<List<LEDRow>> matrix; |
| 25 | + private int previousRow = 0; |
| 26 | + private boolean persist = false; |
| 27 | + |
| 28 | + LEDMatrix(){ |
| 29 | + w = 150; |
| 30 | + h = 150; |
| 31 | + |
| 32 | + dIn = Collections.unmodifiableList(Arrays.asList(new Input[]{ |
| 33 | + addInput("Input A", -50, Port.DATA), |
| 34 | + addInput("Input B", -25, Port.DATA), |
| 35 | + addInput("Input C", 25, Port.DATA), |
| 36 | + addInput("Input D", 50, Port.DATA), |
| 37 | + })); |
| 38 | + contIn = addInput("Line Select", 0, Port.CTRL); |
| 39 | + |
| 40 | + int y=35; |
| 41 | + // Add display |
| 42 | + matrix = new ArrayList<List<LEDRow>>(); |
| 43 | + for(int i=0; i<16; i++){ |
| 44 | + int x=-46; |
| 45 | + List<LEDRow> row = new ArrayList<LEDRow>(); |
| 46 | + for(int j=0; j<4; j++){ |
| 47 | + row.add(new LEDRow(x,y)); |
| 48 | + x+=31; |
| 49 | + } |
| 50 | + y-=6; |
| 51 | + matrix.add(row); |
| 52 | + } |
| 53 | + |
| 54 | + for (List<LEDRow> list : matrix){ |
| 55 | + for (LEDRow led : list){ |
| 56 | + addPart(led); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + //addPart(new SSText(-45, 15, "LED Matrix", 40, Colors.moduleLabel)); |
| 61 | + propagate(); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + @Override |
| 66 | + public AvailableModules getModType() { |
| 67 | + |
| 68 | + return AvailableModules.LEDMatrix; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void paint(Graphics2D g) { |
| 73 | + // Fill in polygon |
| 74 | + g.setColor(Colors.moduleFill); |
| 75 | + drawBox(g, 10); |
| 76 | + g.setColor(Colors.moduleInset); |
| 77 | + drawTrapezoid(g, 5, 0, 55, 130, 20); |
| 78 | + |
| 79 | + // Show IO |
| 80 | + g.setColor(Colors.modulePorts); |
| 81 | + drawInputs(g); |
| 82 | + drawOutputs(g); |
| 83 | + |
| 84 | + // Show LEDs |
| 85 | + drawParts(g); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void propagate() { |
| 90 | + if(!persist){ |
| 91 | + List<LEDRow> prevleds = matrix.get(previousRow); |
| 92 | + for(int i=0; i<4; i++){ |
| 93 | + prevleds.get(i).setVal(new BinData(0)); |
| 94 | + } |
| 95 | + } |
| 96 | + final int sel = contIn.getVal().getUInt() & 15; |
| 97 | + previousRow = sel; |
| 98 | + List<LEDRow> leds = matrix.get(sel); |
| 99 | + for(int i=0; i<4; i++){ |
| 100 | + leds.get(i).setVal(dIn.get(i).getVal()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public PickableEntity createNew() { |
| 106 | + return new LEDMatrix(); |
| 107 | + } |
| 108 | + |
| 109 | + public void turnOffPersist(){ |
| 110 | + persist = false; |
| 111 | + for(List<LEDRow> row : matrix){ |
| 112 | + for(LEDRow group : row){ |
| 113 | + group.setVal(new BinData(0)); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public void turnOnPersist(){ |
| 119 | + persist = true; |
| 120 | + } |
| 121 | + |
| 122 | + public boolean isPersistEnabled(){ |
| 123 | + return persist; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public HashMap<String, String> dataOut() { |
| 128 | + if (!isPersistEnabled()) return null; |
| 129 | + |
| 130 | + HashMap<String, String> data = new HashMap<>(); |
| 131 | + data.put("persist", "1"); |
| 132 | + |
| 133 | + return data; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void dataIn(HashMap<String, String> data) { |
| 138 | + if (data.containsKey("persist")) { |
| 139 | + String storeStr = data.get("persist"); |
| 140 | + try{ |
| 141 | + if(Integer.parseInt(storeStr)==1) |
| 142 | + turnOnPersist(); |
| 143 | + }catch(NumberFormatException e){ |
| 144 | + //leave persist off |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments