Advertisement
michalmonday

Norwegian encoding for malduino elite

Jun 10th, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.16 KB | None | 0 0
  1. /*
  2.  * Author: Seytonic
  3.  *         https://twitter.com/seytonic
  4.  *         https://www.youtube.com/seytonic
  5.  * GIT:
  6.  *         https://github.com/Seytonic/Duckduino-microSD
  7.  */
  8.  
  9. #define UseMultiLangMethod_WindowsOnly false //by setting this to true alt+numpad method will be enabled for chars like "\/*;.," etc. meaning it will work with most encodings without the need for reflashing (suitable and recommended only for Windows though)
  10.  
  11. #include <SPI.h>
  12. #include <SD.h>
  13. #include <string.h>
  14. #include "Keyboard.h"
  15.  
  16. #define ENCODING_BYTE_DESIRED 0
  17. #define ENCODING_BYTE_USED 1
  18. #define ENCODING_BYTE_MODIFIER 2
  19.  
  20. //no.properties Exact lenght: 71 (100 is just because of some paranoid need to have safe additional space :P)
  21. byte Encoding[3][100] = {
  22.     {0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x4D, 0x51, 0x57, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x69, 0x6D, 0x71, 0x77, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0xA0, 0xA3, 0xA4, 0xA7, 0xAB, 0xB5, 0xBD, 0xC5, 0xC6, 0xD8, 0xDF, 0xE5, 0xE6, 0xF0, 0xF8, 0xFE},
  23.     {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x5C, 0x38, 0x39, 0x5C, 0x2D, 0x2C, 0x2F, 0x2E, 0x37, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2E, 0x2C, 0xEC, 0x30, 0xEC, 0x2D, 0x32, 0x61, 0x6D, 0x71, 0x77, 0x79, 0x7A, 0x38, 0x3D, 0x39, 0x5D, 0x2F, 0x3D, 0x61, 0x69, 0x6D, 0x71, 0x77, 0x79, 0x7A, 0x37, 0x7E, 0x30, 0x5D, 0x20, 0x33, 0x34, 0x7E, 0x34, 0x6D, 0x7E, 0x5B, 0x22, 0x3B, 0x73, 0x5B, 0x22, 0x64, 0x3B, 0x74},
  24.     {0x81, 0x81, 0x81, 0x86, 0x81, 0x81, 0x00, 0x81, 0x81, 0x81, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x81, 0x00, 0x81, 0x81, 0x81, 0x86, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x86, 0x00, 0x86, 0x81, 0x81, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86, 0x00, 0x86, 0x86, 0x00, 0x86, 0x81, 0x81, 0x00, 0x86, 0x00, 0x81, 0x81, 0x81, 0x86, 0x00, 0x00, 0x86, 0x00, 0x86}
  25. };
  26.  
  27. byte KEYPAD[10] = {
  28.   234,
  29.   225,
  30.   226,
  31.   227,
  32.   228,
  33.   229,
  34.   230,
  35.   231,
  36.   232,
  37.   233
  38. };
  39.  
  40. File myFile;
  41. boolean first = true;
  42.  
  43.  
  44. void setup() {
  45.   String dip = ""; // Name of the file that will be opened
  46.  
  47.   // Sets the given pins as switches for the dip switches
  48.   pinMode(6, INPUT_PULLUP);
  49.   pinMode(7, INPUT_PULLUP);
  50.   pinMode(8, INPUT_PULLUP);
  51.   pinMode(9, INPUT_PULLUP);
  52.  
  53.   // Switches are checked, dip string is contructed
  54.   if (digitalRead(6) == LOW){dip += "1";} else {dip += "0";}
  55.   if (digitalRead(7) == LOW){dip += "1";} else {dip += "0";}
  56.   if (digitalRead(8) == LOW){dip += "1";} else {dip += "0";}
  57.   if (digitalRead(9) == LOW){dip += "1";} else {dip += "0";}
  58.  
  59.   dip += ".txt";
  60.  
  61.  
  62.   if (!SD.begin(4)) {
  63.     return;
  64.   }
  65.  
  66.   // Desired file is opened
  67.   myFile = SD.open(dip);
  68.   if (myFile) {
  69.     Keyboard.begin();
  70.    
  71.     String line = "";
  72.     while (myFile.available()) {
  73.       char m = myFile.read();
  74.       if (m == '\n'){
  75.         Line(line);
  76.         line = "";
  77.         }
  78.         else if((int) m != 13)
  79.         {
  80.           line += m;
  81.         }
  82.     }
  83.     Line(line);
  84.    
  85.     myFile.close();
  86.   } else {
  87.   }
  88.  
  89.   Keyboard.end();
  90. }
  91.  
  92. void Line(String l)
  93. {
  94.   int space_1 = l.indexOf(" ");
  95.   if (space_1 == -1)
  96.   {
  97.     Press(l);
  98.   }
  99.   else if (l.substring(0,space_1) == "STRING")
  100.   {
  101.     Print(l.substring(space_1 + 1));
  102.   }
  103.   else if (l.substring(0,space_1) == "DELAY")
  104.   {
  105.     int delaytime = l.substring(space_1 + 1).toInt();
  106.     delay(delaytime);
  107.   }
  108.   else if(l.substring(0,space_1) == "REM"){}
  109.   else
  110.   {
  111.       String remain = l;
  112.  
  113.       while(remain.length() > 0)
  114.       {
  115.         int latest_space = remain.indexOf(" ");
  116.         if (latest_space == -1)
  117.         {
  118.           Press(remain);
  119.           remain = "";
  120.         }
  121.         else
  122.         {
  123.           Press(remain.substring(0, latest_space));
  124.           remain = remain.substring(latest_space + 1);
  125.         }
  126.         delay(5);
  127.       }
  128.   }
  129.  
  130.   Keyboard.releaseAll();
  131. }
  132.  
  133.  
  134. void Press(String b)
  135. {
  136.   if(b.length() == 1)
  137.   {
  138.     char c = b[0];
  139.     Keyboard.press(c);
  140.   }
  141.   else if (b.equals("ENTER"))
  142.   {
  143.     Keyboard.press(KEY_RETURN);
  144.   }
  145.   else if (b.equals("CTRL"))
  146.   {
  147.     Keyboard.press(KEY_LEFT_CTRL);
  148.   }
  149.     else if (b.equals("SHIFT"))
  150.   {
  151.     Keyboard.press(KEY_LEFT_SHIFT);
  152.   }
  153.     else if (b.equals("ALT"))
  154.   {
  155.     Keyboard.press(KEY_LEFT_ALT);
  156.   }
  157.     else if (b.equals("GUI"))
  158.   {
  159.     Keyboard.press(KEY_LEFT_GUI);
  160.   }
  161.     else if (b.equals("UP") || b.equals("UPARROW"))
  162.   {
  163.     Keyboard.press(KEY_UP_ARROW);
  164.   }
  165.     else if (b.equals("DOWN") || b.equals("DOWNARROW"))
  166.   {
  167.     Keyboard.press(KEY_DOWN_ARROW);
  168.   }
  169.     else if (b.equals("LEFT") || b.equals("LEFTARROW"))
  170.   {
  171.     Keyboard.press(KEY_LEFT_ARROW);
  172.   }
  173.     else if (b.equals("RIGHT") || b.equals("RIGHTARROW"))
  174.   {
  175.     Keyboard.press(KEY_RIGHT_ARROW);
  176.   }
  177.     else if (b.equals("DELETE"))
  178.   {
  179.     Keyboard.press(KEY_DELETE);
  180.   }
  181.     else if (b.equals("PAGEUP"))
  182.   {
  183.     Keyboard.press(KEY_PAGE_UP);
  184.   }
  185.     else if (b.equals("PAGEDOWN"))
  186.   {
  187.     Keyboard.press(KEY_PAGE_DOWN);
  188.   }
  189.     else if (b.equals("HOME"))
  190.   {
  191.     Keyboard.press(KEY_HOME);
  192.   }
  193.     else if (b.equals("ESC"))
  194.   {
  195.     Keyboard.press(KEY_ESC);
  196.   }
  197.     else if (b.equals("INSERT"))
  198.   {
  199.     Keyboard.press(KEY_INSERT);
  200.   }
  201.     else if (b.equals("TAB"))
  202.   {
  203.     Keyboard.press(KEY_TAB);
  204.   }
  205.     else if (b.equals("END"))
  206.   {
  207.     Keyboard.press(KEY_END);
  208.   }
  209.     else if (b.equals("CAPSLOCK"))
  210.   {
  211.     Keyboard.press(KEY_CAPS_LOCK);
  212.   }
  213.     else if (b.equals("F1"))
  214.   {
  215.     Keyboard.press(KEY_F1);
  216.   }
  217.     else if (b.equals("F2"))
  218.   {
  219.     Keyboard.press(KEY_F2);
  220.   }
  221.     else if (b.equals("F3"))
  222.   {
  223.     Keyboard.press(KEY_F3);
  224.   }
  225.     else if (b.equals("F4"))
  226.   {
  227.     Keyboard.press(KEY_F4);
  228.   }
  229.     else if (b.equals("F5"))
  230.   {
  231.     Keyboard.press(KEY_F5);
  232.   }
  233.     else if (b.equals("F6"))
  234.   {
  235.     Keyboard.press(KEY_F6);
  236.   }
  237.     else if (b.equals("F7"))
  238.   {
  239.     Keyboard.press(KEY_F7);
  240.   }
  241.     else if (b.equals("F8"))
  242.   {
  243.     Keyboard.press(KEY_F8);
  244.   }
  245.     else if (b.equals("F9"))
  246.   {
  247.     Keyboard.press(KEY_F9);
  248.   }
  249.     else if (b.equals("F10"))
  250.   {
  251.     Keyboard.press(KEY_F10);
  252.   }
  253.     else if (b.equals("F11"))
  254.   {
  255.     Keyboard.press(KEY_F11);
  256.   }
  257.     else if (b.equals("F12"))
  258.   {
  259.     Keyboard.press(KEY_F12);
  260.   }
  261. }
  262.  
  263. void loop() {
  264.   // nothing happens after setup
  265. }
  266.  
  267.  
  268. void Print(String inStr)
  269. {
  270.   int enc_index;
  271.   for(byte i=0; i< inStr.length(); i++)
  272.   {
  273.     if (UseMultiLangMethod_WindowsOnly && ((!isalnum(inStr[i]) && inStr[i] != ' ') || IsException(inStr[i]))) //if character is punctuation or requires different button to be pressed in different keyboard language settings then use alt+numpad method
  274.     //if(IsCharSpecial(inStr[i]))
  275.     {
  276.       //Serial.print("special char = ");
  277.       //Serial.println(inStr[i]);
  278.      
  279.       byte hundreds = (byte)inStr[i] / 100;
  280.       byte dozens = ((byte)inStr[i] - (hundreds*100)) / 10;
  281.       byte singles = (byte)inStr[i] - (hundreds*100) - (dozens*10);
  282.  
  283.       //Serial.print(hundreds);
  284.       //Serial.print(dozens);
  285.       //Serial.println(singles);
  286.  
  287.       Keyboard.press(KEY_LEFT_ALT);
  288.       PressRelease((char)KEYPAD[hundreds], 5);
  289.       PressRelease((char)KEYPAD[dozens], 5);
  290.       PressRelease((char)KEYPAD[singles], 5);
  291.       Keyboard.releaseAll();
  292.       continue;
  293.     }
  294.  
  295.  
  296.  
  297.    
  298.     enc_index = GetKeyIndex(inStr.charAt(i), Encoding[ENCODING_BYTE_DESIRED]);
  299.     if(enc_index < 256)
  300.     {    
  301.       if(Encoding[ENCODING_BYTE_MODIFIER][enc_index] > 0)
  302.       {
  303.         Keyboard.press(Encoding[ENCODING_BYTE_MODIFIER][enc_index]);
  304.         delay(5);
  305.       }
  306.       Keyboard.press(Encoding[ENCODING_BYTE_USED][enc_index]);
  307.       delay(5);    
  308.     }
  309.     else
  310.     {
  311.       Keyboard.press(inStr.charAt(i));
  312.       delay(5);    
  313.     }
  314.     Keyboard.releaseAll();
  315.   }
  316. }
  317.  
  318. int GetKeyIndex(byte c, byte* char_array)
  319. {
  320.   for(byte i=0;i<strlen(char_array);i++)
  321.   {
  322.     if(c == char_array[i])
  323.     {
  324.       return i;
  325.     }
  326.   }
  327.   return 256;
  328. }
  329.  
  330. void PressRelease(char c, byte timeDelay)
  331. {
  332.   Keyboard.press(c);
  333.   delay(timeDelay);
  334.   Keyboard.release(c);
  335. }
  336.  
  337.  
  338. /*
  339. Exceptions are the letters that require different button being pressed in different lang settings
  340. (so if letter == exception then use alt+numpad method, otherwise use the normal typing because it's faster to press 1 instead of 4 buttons)
  341. these exceptions are listed to make the typing process faster because using alt+numpad method for all the characters appears to be too slow
  342.  
  343. german - yz
  344. french - qamwz
  345. dutch - qamwz
  346. turkish - i
  347. azerbaijani - totally uncompatible
  348. */
  349.  
  350. #define EXCEPTIONS_SIZE 7
  351. char exceptions[EXCEPTIONS_SIZE] = {
  352.   'y',
  353.   'z',
  354.   'q',
  355.   'a',
  356.   'm',
  357.   'w',
  358.   'i',
  359. };
  360.  
  361. /*
  362. bool IsCharSpecial(char c)
  363. {
  364.   byte b = (byte)c;
  365.   if((b == 32 || (b >= 49 && b <= 57) || (b >= 65 && b <= 90) || (b >= 97 && b <= 122)) && IsntException(b))
  366.   {
  367.     return false;
  368.   }
  369.   return true;
  370. }
  371. */
  372.  
  373. bool IsException(char c)
  374. {
  375.   for(byte i=0; i<EXCEPTIONS_SIZE; i++)
  376.   {
  377.     if(c == exceptions[i])
  378.     {
  379.       return true;
  380.     }
  381.   }
  382.   return false;
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement