Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1.  
  2. public class NewBssrMasksGenerator {
  3.  
  4.     // Include here all port that output Data bits.
  5.     enum Port {
  6.         A,
  7.         B
  8.     }
  9.  
  10.     // A port pin identifier.
  11.     static class Pin {
  12.         final Port port;
  13.         final int pin_index;  // MSB=15, LSB=0;
  14.  
  15.         public Pin(Port port, int pin_index) {
  16.             this.port = port;
  17.             this.pin_index = pin_index;
  18.         }
  19.  
  20.         public String toString() {
  21.             return "P" + port.name() + pin_index;
  22.         }
  23.     }
  24.  
  25.     // Id addition to the 16 bit parallel data, we also
  26.     // want to reset the WR pin.
  27.     static final Pin WR_PIN = new Pin(Port.A, 9);
  28.  
  29.     // Maps the 16 data bit index to pin.
  30.     static final Pin[] DATA_PINS = new Pin[]{
  31.             new Pin(Port.A, 0),   // D0
  32.             new Pin(Port.A, 1),   // D1
  33.             new Pin(Port.A, 2),   // D2
  34.             new Pin(Port.A, 3),   // D3
  35.  
  36.             new Pin(Port.A, 4),   // D4
  37.             new Pin(Port.A, 5),   // D5
  38.             new Pin(Port.A, 6),   // D6
  39.             new Pin(Port.A, 7),   // D7
  40.  
  41.             new Pin(Port.B, 15),  // D8
  42.             new Pin(Port.B, 13),  // D9
  43.             new Pin(Port.B, 4),   // D10
  44.             new Pin(Port.B, 5),   // D11
  45.  
  46.             new Pin(Port.B, 8),   // D12
  47.             new Pin(Port.B, 9),   // D13
  48.             new Pin(Port.B, 10),  // D14
  49.             new Pin(Port.B, 12),  // D15
  50.     };
  51.  
  52.     // Maps a color value from src_bits to dst_bits.
  53.     private static int resize_color_channel(int srv_val, int src_bits, int dst_bits) {
  54.         final double ratio = (double) srv_val / ((1 << src_bits) - 1);
  55.         final long result = Math.round(((1 << dst_bits) - 1) * ratio);
  56.         return (int) result;
  57.     }
  58.  
  59.     // Map a 8 bit rgb332 color to 16 bit rgb565 color.
  60.     private static int color8_to_color16(int c8) {
  61.         final int r3 = (c8 >> 5) & 0x7;
  62.         final int g3 = (c8 >> 2) & 0x7;
  63.         final int b2 = c8 & 0x3;
  64.  
  65.         final int r5 = resize_color_channel(r3, 3, 5);
  66.         final int g6 = resize_color_channel(g3, 3, 6);
  67.         final int b5 = resize_color_channel(b2, 2, 5);
  68.  
  69.         final int rgb565 = r5 << 11 | g6 << 5 | b5;
  70.         return rgb565;
  71.     }
  72.  
  73.     // Given a 16 bits data value and a port, returns the BSSR mask to set
  74.     // the pins of that port.
  75.     private static long value_to_bssr_mask(int uint16_value, Port port) {
  76.         // If WR is on this port, we return a mask that resets it.
  77.         long bssr_bits = (port == WR_PIN.port) ? 1l << (WR_PIN.pin_index + 16) : 0;
  78.         for (int bit = 0; bit < 16; bit++) {
  79.             if (DATA_PINS[bit].port != port) {
  80.                 continue;
  81.             }
  82.             final int pin_index = DATA_PINS[bit].pin_index;
  83.             if ((uint16_value & (1 << bit)) == 0) {
  84.                 bssr_bits |= 1l << (pin_index + 16);  // set pin low
  85.             } else {
  86.                 // Mark for high level
  87.                 bssr_bits |= 1l << pin_index;  // set pin high
  88.             }
  89.         }
  90.         return bssr_bits;
  91.     }
  92.  
  93.     // A common method to output the table data.
  94.     private static void generate_table_data(int uint16_values[], String table_name_prefix, Port port) {
  95.         System.out.printf("const uint32_t %s_port_%s[] = {\n", table_name_prefix, port.name().toLowerCase());
  96.         for (int i = 0; i < 256; i++) {
  97.             long bssr_mask = value_to_bssr_mask(uint16_values[i], port);
  98.             System.out.printf("0x%08x,", bssr_mask);
  99.             if (i % 4 == 3) {
  100.                 System.out.printf("  // 0x%02x - 0x%02x\n", i - 3, i);
  101.             }
  102.         }
  103.         System.out.println("};");
  104.     }
  105.  
  106.     // Direct output table. No color mapping. Needed for TFT commands.
  107.     private static void generate_direct_table(Port port) {
  108.         int values[] = new int[256];
  109.         for (int i = 0; i < 256; i++) {
  110.             values[i] = i;
  111.         }
  112.         generate_table_data(values, "direct_bssr_table", port);
  113.     }
  114.  
  115.     // Output of color16 mapped from color8.
  116.     private static void generate_color_table(Port port) {
  117.         int values[] = new int[256];
  118.         for (int i = 0; i < 256; i++) {
  119.             values[i] = color8_to_color16(i);;
  120.         }
  121.         generate_table_data(values, "color_bssr_table", port);
  122.     }
  123.  
  124.     public static void main(String ignored[]) {
  125.         System.out.println("// Pin map:");
  126.         System.out.printf("//   WR    %4s\n", WR_PIN);
  127.         for (int i = 0; i < 16; i++) {
  128.             System.out.printf("//   D%-2d   %4s\n" , i, DATA_PINS[i]);
  129.         }
  130.         for (Port port : Port.values()) {
  131.             System.out.println();
  132.             generate_direct_table(port);
  133.         }
  134.         for (Port port : Port.values()) {
  135.             System.out.println();
  136.             generate_color_table(port);
  137.         }
  138.     }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement