Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. #include "MicroBit.h"
  2. #include <string>
  3. #include <vector>
  4. #include "btree.hpp"
  5.  
  6. MicroBit    uBit;
  7. MicroBitImage tick= MicroBitImage("0,0,0,0,0\n0,0,0,0,1\n0,0,0,1,0\n1,0,1,0,0\n0,1,0,0,0\n");
  8. MicroBitImage dotImg= MicroBitImage("0,0,0,0,0\n0,0,0,0,0\n0,0,1,0,0\n");
  9. MicroBitImage dashImg= MicroBitImage("0,0,0,0,0\n0,0,0,0,0\n1,1,1,1,1\n");
  10. MicroBitImage antenna("255,0,255,0,255\n 0,255,255,255,0\n 0,0,255,0,0\n 0,0,255,0,0\n 0,0,255,0,0\n");
  11. btree morsetree;
  12.  
  13. using namespace std;
  14.  
  15.  
  16. // Sender microBit
  17. bool sender = false;
  18. // Reciever microBit
  19. bool receiver = false;
  20.  
  21. // Bool to show if the program is already running or not.
  22. bool running = false;
  23. // If the button has been pressed or not (state).
  24. bool pressed = false;
  25. // overall program elapsed time.
  26. uint64_t currentTime = system_timer_current_time();
  27. // How long button A has been pressed for
  28. uint64_t difference = 0;
  29. //store in string
  30. std::string message = "" ;
  31. std::string sequence = "";
  32.  
  33. //declare dot and dash as char
  34. char dot = '.';
  35. char dash = '-';
  36.  
  37.  
  38. void sending()
  39. {
  40.     uBit.display.scroll("Sender"); //You are S (Sender)
  41.  
  42.     bool pressed = false;
  43.     bool transmit = false;
  44.  
  45.     while (1)
  46.     {
  47.         uint64_t reading = system_timer_current_time();
  48.  
  49.         while(!uBit.buttonA.isPressed())
  50.         {
  51.  
  52.             uint64_t wait = system_timer_current_time() - reading;
  53.  
  54.             // Space between inputted chars based on morse
  55.             if(wait > 750 && transmit == true)
  56.             {
  57.                 uBit.display.print(antenna);
  58.                 // Digital Value set to 1 to send pulse over wire
  59.                 //uBit.sleep(1700);
  60.                 uBit.radio.datagram.send("3");
  61.                 uBit.display.clear();
  62.                 // Set to 0 to stop sending pulse over wire
  63.                 transmit = false;
  64.  
  65.                 break;
  66.             }
  67.  
  68.         }
  69.  
  70.         reading = system_timer_current_time();
  71.  
  72.         while (uBit.buttonA.isPressed())
  73.         {
  74.             pressed = true;
  75.         }
  76.  
  77.         // Delta is assigned loop time - reading
  78.         uint64_t delta = system_timer_current_time() - reading;
  79.  
  80.         if (pressed)
  81.         {
  82.             if (delta < 500)
  83.             {
  84.                
  85.                 uBit.display.print(".");
  86.                 uBit.radio.datagram.send("1");
  87.                 uBit.sleep(250);
  88.                 transmit = true;
  89.             }
  90.             else if (delta > 500 && delta < 1500)
  91.             {
  92.                 uBit.display.print("-");
  93.                 uBit.radio.datagram.send("2");
  94.                 uBit.sleep(500);
  95.                 transmit = true;
  96.             }
  97.  
  98.         else if (uBit.buttonB.isPressed)
  99.         {
  100.             uBit.display.scroll("Sent");
  101.             uBit.radio.datagram.send("3");
  102.             uBit.sleep(750);
  103.             break;
  104.         }
  105.  
  106.             pressed = false;
  107.             uBit.display.clear();
  108.         }
  109.     }
  110. }
  111.  
  112.  
  113. void onData(MicroBitEvent)
  114. {
  115.     ManagedString s = uBit.radio.datagram.recv();
  116.     if (s == "1"){
  117.         uBit.display.print(dotImg);
  118.         message += dot ;
  119.         uBit.sleep(300);
  120.     }
  121.     else if (s == "2"){
  122.         uBit.display.print(dashImg);
  123.         message += dash;
  124.         uBit.sleep(300);
  125.     }
  126.     else if (s == "3"){
  127.         char temp = morsetree.code(message.c_str());
  128.         sequence += temp;
  129.         message.clear();
  130.         uBit.display.scroll(sequence.c_str());
  131.         uBit.sleep(1000);
  132.     }
  133.     uBit.display.clear();
  134. }
  135.  
  136. void Son(){
  137.     uBit.messageBus.listen(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, onData);
  138.     uBit.display.clear();
  139. }
  140. int main()
  141. {
  142.     uBit.init();
  143.     uBit.radio.enable();
  144.  
  145.     // If the program is being started up depict microBits to sender / reciever
  146.     while (!running) {
  147.         if (uBit.buttonA.isPressed() || uBit.buttonB.isPressed()) {
  148.             // If button A has been pressed, assign it to be Dad.
  149.             sender = uBit.buttonA.isPressed() ? true : false;
  150.             // If button B has been pressed, assign it to be Son.
  151.             receiver = uBit.buttonB.isPressed() ? true : false;
  152.             // Prints out S or R on the sender (Dad) / reciever (Son) microBit.
  153.             uBit.display.print(sender ? "S" : "R");
  154.             uBit.sleep(1000);
  155.             running = true;
  156.         }
  157.     }
  158.    
  159.     if(receiver){
  160.         Son();
  161.     }
  162.     if(sender){
  163.         Dad();
  164.     }
  165.     release_fiber();
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement