diff --git a/docs/tutorials/network.rst b/docs/tutorials/network.rst index 99df7e1..4260a12 100644 --- a/docs/tutorials/network.rst +++ b/docs/tutorials/network.rst @@ -1,81 +1,81 @@ -Network -------- +Sieć +---- -It is possible to connect devices together to send and receive -messages to and from each other. This is called a network. A network of -interconnected networks is called an internet. The Internet is an internet -of all the internets. +Możemy połączyć urządzenia w celu wymiany wiadomości między +nimi. Nazywa się to siecią. Sieć połączonych sieci nazywana +jest internetem. Natomiast Internet łączy w sobie wszystkie pozostałe +internety. -Networking is hard and this is reflected in the program described below. -However, the beautiful thing about this project is it contains all the common -aspects of network programming you need to know about. It's also remarkably -simple and fun. +Praca w sieci (networking) jest trudna. Można to zaobserwować na +przykładzie opisanego niżej programu. Jednak niewątpliwą zaletą tego projektu +jest to, że zawiera on wszystkie rozpowszechnione aspekty programowania sieciowego +o których powinieneś wiedzieć. W dodatku jest niezwykle prosty i przyjemny w obsłudze. -But first, let's set the scene... +Ale zacznijmy od początku... -Connection +Połączenie ++++++++++ -Imagine a network as a series of layers. At the very bottom is the most -fundamental aspect of communication: there needs to be some sort of way for -a signal to get from one device to the other. Sometimes this is done via a -radio connection, but in this example we're simply going to use two wires. +Wyobraźmy sobie sieć jako szereg warstw. Na samym dole mamy najbardziej +fundamentalny aspekt komunikacji: musi być jakiś sposób, żeby przekazać sygnał +z jednego urządzenia do drugiego. Czasem jest to wykonywane poprzez połączenie radiowe, +ale w tym przykładzie zwyczajnie użyjemy dwóch przewodów. .. image:: network.png -It is upon this foundation that we can build all the other layers in the -*network stack*. +To właśnie na tym fundamencie możemy układać wszystkie pozostałe warstwy +naszej *sieci*. -As the diagram shows, blue and red micro:bits are connected via crocodile -leads. Both use pin 1 for output and pin 2 for input. The output from one -device is connected to the input on the other. It's a bit like knowing which -way round to hold a telephone handset - one end has a microphone (the input) -and the other a speaker (the output). The recording of your voice via your -microphone is played out of the other person's speaker. If you hold the -phone the wrong way up, you'll get strange results! +Jak widzimy na rysunku, niebieskie urządzenie micro:bits jest połączone z czerwonym +za pośrednictwem krokodylków. W obydwu przypadkach wtyk 1 jest wykorzystywany do +sygnału wyjściowego, natomiast wtyk 2 — wejściowego. Wyjście jednego urządzenia jest +połączone z wejściem drugiego. Przypomina to słuchawkę telefonu: na jednym jej końcu +umiejscowiony jest mikrofon (wyjście), na drugim — głośniczek (wejście). Głos zapisany +za pośrednictwem mikrofonu jest odtwarzany przez głośniczek drugiego urządzenia. Gdybyśmy +trzymali słuchawkę odwrotnie, mogłoby z tego wyjść coś dziwnego! -It's exactly the same in this instance: you must connect the wires properly! +W tym przypadku jest tak samo: konieczne jest poprawne podpięcie przewodów! -Signal +Sygnał ++++++ -The next layer in the *network stack* is the signal. Often this will depend -upon the characteristics of the connection. In our example it's simply -digital on and off signals sent down the wires via the IO pins. +Następną warstwą w naszej *sieci* jest sygnał. Często zależy on od +rodzaju połączenia. W naszym przypadku jest to zwykły sygnał włączenia i +wyłączenia przekazywany za pośrednictwem wtyków w naszych przewodach. -If you remember, it's possible to use the IO pins like this:: +Jak już wiemy, wtyki wyjścia/wejścia można wykorzystać w następujący sposób:: - pin1.write_digital(1) # switch the signal on - pin1.write_digital(0) # switch the signal off - input = pin2.read_digital() # read the value of the signal (either 1 or 0) + pin1.write_digital(1) # włącz sygnał + pin1.write_digital(0) # wyłącz sygnał + input = pin2.read_digital() # odczytaj wartość sygnału (1 lub 0) -The next step involves describing how to use and handle a signal. For that we -need a... + Następnym krokiem będzie opisanie sposobu użycia i kontrolowania sygnału. W tym celu będzie + nam potrzebny... -Protocol +Protokół ++++++++ -If you ever meet the Queen there are expectations about how you ought to -behave. For example, when she arrives you may bow or curtsey, if she offers her -hand politely shake it, refer to her as "your majesty" and thereafter as -"ma'am" and so on. This set of rules is called the royal protocol. A protocol -explains how to behave given a specific situation (such as meeting the -Queen). A protocol is pre-defined to ensure everyone understands what's going -on before a given situation arises. +Gdybyś kiedyś spotkał się z Królową, musiałbyś sprostać pewnym oczekiwaniom. +Na przykład, po jej przyjeździe musiałbyś się ukłonić, a gdyby podała ci rękę, +należałoby kulturalnie ją uścisnąć. Zwracać się do Królowej przyjęto +per "wasza wysokość", następnie per "proszę pani" itd. Ten zestaw zasad nazywany jest +Królewskim Protokołem. Protokół wyjaśnia jak zachować się w odpowiedniej sytuacji +(np. na spotkaniu z Królową). Protokół jest z góry określony w celu zagwarantowania +jasności sytuacji zanim miałaby ona wystąpić. .. image:: queen.jpg -It is for this reason that we define and use protocols for communicating -messages via a computer network. Computers need to agree before hand how to -send and receive messages. Perhaps the best known protocol is the -hypertext transfer protocol (HTTP) used by the world wide web. +Definiujemy i wykorzystujemy protokoły po to, żeby móc przekazywać wiadomości +za pośrednictwem sieci komputerowych. Komputery muszą z góry uzgodnić, jak +wysyłać i przyjmować wiadomości. Prawdopodobnie najbardziej znanym protokołem +jest protokół transmisji hipertekstu (HTTP), wykorzystywany przez sieć www. -Another famous protocol for sending messages (that pre-dates computers) is -Morse code. It defines how to send character-based messages via on/off signals -of long or short durations. Often such signals are played as bleeps. Long -durations are called dashes (``-``) whereas short durations are dots (``.``). -By combining dashes and dots Morse defines a way to send characters. For -example, here's how the standard Morse alphabet is defined:: +Innym znanym protokołem wysyłania wiadomości (który istniał jeszcze przed komputerami) +jest alfabet Morse'a. Określa on sposób wysyłania wiadomości złożonych z liter poprzez +sygnały o krótkim lub długim czasie trwanie. Często takie sygnały są odtwarzane jako +dźwięki. Sygnały o długim czasie trwania nazywane są kreskami ("-"), podczas gdy +te krótkie określane są kropkami ("."). Poprzez połączenie kresek z kropkami Morse +umożliwia wysyłanie liter. Poniżej zamieszczony jest standardowy alfabet Morsa:: .- A --- J ... S .---- 1 ----. 9 -... B -.- K - T ..--- 2 ----- 0 @@ -87,69 +87,69 @@ example, here's how the standard Morse alphabet is defined:: .... H --.- Q --.. Z ---.. 8 .. I .-. R -Given the chart above, to send the character "H" the signal is switched on four -times for a short duration, indicating four dots (``....``). For the letter -"L" the signal is also switched on four times, but the second signal has a -longer duration (``.-..``). +Jak wynika z powyższego wykresu, żeby wysłać literę "H", należy czterokrotnie +wysłać krótki sygnał, który odpowiada czterem kropkom ("...."). W przypadku +litery "L" sygnał również jest wysyłany cztery razy, jednak drugie włączenie +trwa dłużej (".-.."). -Obviously, the timing of the signal is important: we need to tell a dot from a -dash. That's another point of a protocol, to agree such things so everyone's -implementation of the protocol will work with everyone elses. In this instance -we'll just say that: +Oczywiście czas trwania sygnału ma znaczenie: musimy odróżnić kropkę od kreski. +Na tym polega kolejny cel protokołu — żeby uzgodnić wspólne dla wszystkich zasady +wdrażania protokołu. +W tym przypadku powiemy tylko, że: -* A signal with a duration less than 250 milliseconds is a dot. -* A signal with a duration from 250 milliseconds to less than 500 milliseconds is a dash. -* Any other duration of signal is ignored. -* A pause / gap in the signal of greater than 500 milliseconds indicates the end of a character. +* Sygnał trwający mniej niż 250 milisekund jest kropką. +* Sygnał trwający od 250 do 499 milisekund jest kreską. +* Każdy sygnał o innym czasie trwania będzie ignorowany. +* Przerwa / odstęp między sygnałami, który przekracza 500 milisekund określa koniec litery. -In this way, the sending of a letter "H" is defined as four "on" signals that -last no longer than 250 milliseconds each, followed by a pause of greater than -500 milliseconds (indicating the end of the character). +W ten sposób, wysłanie litery "H" określane jest jako cztery sygnały trwające +nie więcej niż 250 milisekund każdy, po których następuje przerwa dłuższa +niż 500 milisekund (określająca koniec litery). -Message -+++++++ +Wiadomość ++++++++++ -We're finally at a stage where we can build a message - a message that actually -means something to us humans. This is the top-most layer of our *network -stack*. +Wreszcie dotarliśmy do chwili, gdy możemy stworzyć wiadomość, która +ma dla ludzi określone znaczenie. Jest to najwyższa warstwa w +naszej *sieci*. -Using the protocol defined above I can send the following sequence of signals -down the physical wire to the other micro:bit:: +Wykorzystując zdefiniowany wyżej protokół, mogę wysłać za pośrednictwem +przewodu następującą sekwencję sygnałów do innego urządzenia micro:bit:: ...././.-../.-../---/.--/---/.-./.-../-.. -Can you work out what it says? +Czy możesz odczytać te wiadomość? -Application -+++++++++++ +Aplikacja ++++++++++ -It's all very well having a network stack, but you also need a way to -interact with it - some form of application to send and receive messages. -While HTTP is interesting *most* people don't know about it and let their -web-browser handle it - the underlying *network stack* of the world wide web -is hidden (as it should be). +Posiadanie sieci jest dobre, jednak potrzebujemy również sposobu, żeby wejść +z nią w interakcję. Można to zrobić z pomocą aplikacji, która wysyła i przyjmuje +wiadomości. HTTP jest ciekawe, jednak *większość* ludzi nie wie o nim, pozostawiając +jego obsługę przeglądarce - sieć leżąca u podstaw sieci ogólnoświatowej +jest ukryta (tak jak być powinno). -So, what sort of application should we write for the BBC micro:bit? How should -it work, from the user's point of view? +A więc, jakiego rodzaju aplikację powinniśmy napisać dla urządzenia BBC micro:bit? +Jak powinna ona działać, patrząc z perspektywy użytkownika? -Obviously, to send a message you should be able to input dots and dashes (we -can use button A for that). If we want to see the message we sent or just -received we should be able to trigger it to scroll across the display (we can -use button B for that). Finally, this being Morse code, if a speaker is -attached, we should be able to play the beeps as a form of aural feedback while -the user is entering their message. +Żeby wysłać wiadomość, powinniśmy mieć możliwość wprowadzenia kropek i kresek +(w tym celu możemy wykorzystać przycisk A). Żeby zobaczyć wiadomość, którą wysłaliśmy +lub otrzymaliśmy, powinniśmy mieć możliwość odtworzenia jej poprzez przewijanie na +wyświetlaczu (możemy w tym celu wykorzystać przycisk B). Co więcej, jako że jest +to alfabet Morse'a, jeśli mamy podłączony głośniczek, powinniśmy mieć możliwość +odtworzenia sygnałów dźwiękowych w chwili gdy użytkownik wprowadza swoją wiadomość. -The End Result -++++++++++++++ +Efekt końcowy ++++++++++++++ -Here's the program, in all its glory and annotated with plenty of comments so -you can see what's going on:: +Poniżej zamieszczony jest program w całej swojej krasie wraz z dużą ilością +komentarzy, żebyś mógł zobaczyć co się dzieje:: from microbit import * import music - # A lookup table of morse codes and associated characters. + # tabela kodów alfabetu Morse'a i odpowiednio do każdego z nich przypisanego znaku. MORSE_CODE_LOOKUP = { ".-": "A", "-...": "B", @@ -191,12 +191,12 @@ you can see what's going on:: def decode(buffer): - # Attempts to get the buffer of Morse code data from the lookup table. If - # it's not there, just return a full stop. + # Próba pobrania kodu Morse'a z tabeli znaków. Jeżeli + # go tam nie ma, po prostu zwróć kropkę. return MORSE_CODE_LOOKUP.get(buffer, '.') - # How to display a single dot. + # Jak wyświetlić pojedynczą kropkę. DOT = Image("00000:" "00000:" "00900:" @@ -204,7 +204,7 @@ you can see what's going on:: "00000:") - # How to display a single dash. + # Jak wyświetlić pojedynczą kreskę. DASH = Image("00000:" "00000:" "09990:" @@ -212,91 +212,92 @@ you can see what's going on:: "00000:") - # To create a DOT you need to hold the button for less than 250ms. + # Aby utworzyć KROPKĘ musisz przytrzymać przycisk krócej niż 250 milisekund. DOT_THRESHOLD = 250 - # To create a DASH you need to hold the button for less than 500ms. + # Aby utworzyć KRESKĘ musisz przytrzymać przycisk krócej niż 500 milisekund. DASH_THRESHOLD = 500 - # Holds the incoming Morse signals. + # Trzyma przychodzący sygnał Morse'a. buffer = '' - # Holds the translated Morse as characters. + # Trzyma przetłumaczony sygnał Morse'a na znaki. message = '' - # The time from which the device has been waiting for the next keypress. + # Czas od kiedy urządzenie czeka na naciśnięcie następnego klawisza. started_to_wait = running_time() - # Put the device in a loop to wait for and react to key presses. + # Ustaw urządzenie w pętli, aby czekało i zareagowało na naciśnięcie. while True: - # Work out how long the device has been waiting for a keypress. + # Oblicz jak długo urządzenie czekało na naciśnięcie. waiting = running_time() - started_to_wait - # Reset the timestamp for the key_down_time. + # Zresetuj czas dla key_down_time. key_down_time = None - # If button_a is held down, then... + # Jeżeli button_a jest przytrzymany, to wtedy... while button_a.is_pressed(): - # Play a beep - this is Morse code y'know ;-) + # Zabrzęcz - to jest alfabet Morse'a, rozumiesz ;-) music.pitch(880, 10) - # Set pin1 (output) to "on" + # Ustaw pin1 (wyjście) na "on" (ang. włączony) pin1.write_digital(1) - # ...and if there's not a key_down_time then set it to now! + # ... i jeżeli key_down_time jest puste, to wtedy ustaw go na teraz! if not key_down_time: key_down_time = running_time() - # Alternatively, if pin2 (input) is getting a signal, pretend it's a - # button_a key press... + # Alternatywnie, jeżeli pin2 (wejście) jest otrzymuje sygnał, zasymuluj + # naciśnięcie klawisza button_a... while pin2.read_digital(): if not key_down_time: key_down_time = running_time() - # Get the current time and call it key_up_time. + # Pobierz aktualny czas i nazwij go key_up_time. key_up_time = running_time() - # Set pin1 (output) to "off" + # Ustaw pin1 (wyjście) na "off" (ang. wyłączony) pin1.write_digital(0) - # If there's a key_down_time (created when button_a was first pressed - # down). + # Jeżeli key_down_time jest ustawiona (utworzona gdy button_a został + # naciśnięty pierwszy raz). if key_down_time: - # ... then work out for how long it was pressed. + # ... potem oblicz jak długo był wciśnięty. duration = key_up_time - key_down_time - # If the duration is less than the max length for a "dot" press... + # Jeżeli duration (ang. czas trwania) jest mniejszy niż maksymalna długość + # dla naciśnięcia "dot"... (ang. kropka) if duration < DOT_THRESHOLD: - # ... then add a dot to the buffer containing incoming Morse codes - # and display a dot on the display. + # ... wtedy dodaj kropkę do buffer (ang. bufor) zawierającego + # przychodzący kod Morse'a i wyświetl kropkę na wyświetlaczu. buffer += '.' display.show(DOT) - # Else, if the duration is less than the max length for a "dash" - # press... (but longer than that for a DOT ~ handled above) + # W przeciwnym wypadku, ale jeżeli czas trwania jest mniejszy niż maksymalna + # długość dla naciśnięcia "dash"... (ang. kreska) (ale dłuższy niż dla + # KROPKI ~ określonej powyżej) elif duration < DASH_THRESHOLD: - # ... then add a dash to the buffer and display a dash. + # ... wtedy dodaj kreskę do bufora i wyświetl kreskę. buffer += '-' display.show(DASH) - # Otherwise, any other sort of keypress duration is ignored (this isn't - # needed, but added for "understandability"). + # W pozostałych przypadkach, każda inna długość naciśnięcia jest ignorowana + # (to nie jest konieczne, ale zostało dodane dla "zrozumiałości"). else: pass - # The button press has been handled, so reset the time from which the - # device is starting to wait for a button press. + # Obsługa przycisku została zakończona, więc zresetuj czas od którego + # urządzenie rozpoczyna czekać na naciśnięcie przycisku. started_to_wait = running_time() - # Otherwise, there hasn't been a button_a press during this cycle of the - # loop, so check there's not been a pause to indicate an end of the - # incoming Morse code character. The pause must be longer than a DASH - # code's duration. + # W przeciwnym razie button_a nie został naciśnięty podczas tego cyklu pętli, + # więc sprawdź czy to nie przerwa wskazująca na koniec przychodzącego kodu + # Morse'a dla znaku. Przerwa musi być dłuższa niż czas dla KRESKI (DASH) elif len(buffer) > 0 and waiting > DASH_THRESHOLD: - # There is a buffer and it's reached the end of a code so... - # Decode the incoming buffer. + # Mamy bufor i przetwarzanie dobiegło końca tak więc... + # Rozkoduj zawartość bufora. character = decode(buffer) - # Reset the buffer to empty. + # Opróżnij bufor do czysta. buffer = '' - # Show the decoded character. + # Pokaż odkodowane znaki. display.show(character) - # Add the character to the message. + # Dodaj znaki do wiadomości. message += character - # Finally, if button_b was pressed while all the above was going on... + # Ostatecznie, jeżeli button_b został naciśnięty podczas trwania powyższego... if button_b.was_pressed(): - # ... display the message, + # ... wyświetl wiadomość, display.scroll(message) - # then reset it to empty (ready for a new message). + # potem opróżnij do czysta (gotowy na nową wiadomość). message = '' -How would you improve it? Can you change the definition of a dot and a dash so -speedy Morse code users can use it? What happens if both devices are sending at -the same time? What might you do to handle this situation? +Jak chciałbyś ulepszyć to? Czy możesz zmienić definicje kropki i kreski tak by +biegły użytkownik alfabetu Morse'a mógł użyć tego? Co się stanie gdy oba urządzenia +będą wysyłać w tym samym czasie? Co możesz zrobić, aby poradzić sobie z tą sytuacją. -.. footer:: The image of Queen Elizabeth II is licensed as per the details here: https://commons.wikimedia.org/wiki/File:Queen_Elizabeth_II_March_2015.jpg +.. footer:: Obraz Królowej Elżbiety II został użyty zgodnie z licencją określoną na stronie: https://commons.wikimedia.org/wiki/File:Queen_Elizabeth_II_March_2015.jpg