Hello Cemetech, recently I have been working on a relatively simple method for linking two calculators together. This is intended for a game, where the user can send something to another user that is connected but they can only do it once. So far here is what I came up with. (Ignore Celtic CE related commands, they're just used for asthetics)
Code:
The first portion of the program checks to see if two calculators are connected.
E is the error counting, A is the "unique identity" for each calculator, B holds what A was before checking the connection.
Code:
If another calculator is connected, the variable A is received and the value is flipped right after and should be different from B. If no calculator is connected, the getCalc( command does nothing and is flipped anyways, and it would be the same as B which the connection doesn't work. The calculator tries 5 times in total before giving up.
In this next portion of code, The variable V is the value the user wants to send to the other calculator. After, V is stored to D so the calculator can compare later. πe² is stored to B to tell the other calculator the value is set and ready to receive from the linked calculator. After it gives the other calculator 5 seconds by default to receive the other variable before continuing.
Code:
This next section of code attempts to verify that the receiving calculator actually grabbed the value the sender just made. On the receiving calculator, it sets that same πe² value to C once the variable V is received. So now the sender is checking to see if C is that special value or not. It tries twice and if it doesn't get anything, it gives up. Note for this, there is no Wait commands for delays.
Code:
The last bit of code for the sender starts another delay so the receiving calculator can finalize the transmission with a delay.
Code:
Now for the receiving calculator, it waits one second and then attempts to retrieve the V and B variables from the sender. If the sender is ready for the receiver to retrieve the variables, it sets B to the special value. If both B is equal to πe² and V is a non-zero value, then it continues the program. Otherwise it attempts to do it again twice before it gives up.
Code:
The last remainder of code waits to check the "handshake" variable from the sender. It verifies by checking if the sender calculator knows that the receiving calculator retrieved the V variable or not. It attempts this twice before giving up.
Code:
So far this what I came up with, I'm not sure if there is a better way to go at this especially with the limitations of BASIC. If you have any suggestions, feel free to make revisions or comment down below!
Code:
0->E
ClrHome
Lbl 0
Output(5,9,"CONNECTING
Wait 1
If not(max(A={0,1:0->A
A->B
GetCalc(A:not(A->A
If A!=B:Then
E+1->E:Output(6,9,Ans
End
If E=5:Then
ClrHome
Output(5,12,"FAILED
Return
End
If A!=B:Goto 0
0->V:0->B:0->C:5->S:0->E
Lbl M
ClrHome
det(12,255,255,0,0
Disp "LINK TEST PROTOCOL
det(12,0,0,255,255
Disp "1. Send","2. Receive","3. Quit
Repeat max(Ans={92,93,94
getKey
End:Ans->K
If K=94:Goto Q
{255,0
If K=93:{0,248
det(12,Ans(1),Ans(2),255,255
If K=92:Goto A
If K=93:Goto B
Lbl Q
Return
Lbl A
1->A
Input "Value: ",V
V->D
pi[e]^^2->B
Lbl VS
ClrHome
Output(5,11,"WAITING
Output(6,11,S
Wait S
ClrHome
Output(5,9,"VERIFYING
0->V:0->C:GetCalc(C:GetCalc(V
If C!=pi[e]^^2 or V!=D:Then
S+1->S:E+1->E
End
If E=2:Then
ClrHome
Output(5,8,"DISCONNECTED
Return
End
If C!=pi[e]^^2 or V!=D
Goto VS
0->E
ClrHome
Output(5,6,"SENDING HANDSHAKE
Wait S
ClrHome
Output(5,8,"SEND SUCCESS
Return
Lbl B
ClrHome
Output(5,10,"RECEIVING
Wait 1
If E:Output(6,10,E
GetCalc(V:GetCalc(B
If V and B=pi[e]^^2:Then
Output(7,10,"
Output(6,10,V
pi[e]^^2->C
Else
E+1->E
End
If E=3:Then
ClrHome
Output(5,8,"DISCONNECTED
Return
End
If not(V) or B!=pi[e]^^2
Goto B
0->E
Lbl VB
Output(5,3,"WAITING FOR HANDSHAKE
Wait S
ClrHome
Output(5,5,"RECEIVING HANDSHAKE
If E:Output(6,4,E
0->C:GetCalc(C
If C!=pi[e]^^2:Then
E+1->E:S+2->S
End
If E=2:Then
ClrHome
Output(5,8,"DISCONNECTED
Return
End
If C!=pi[e]^^2
Goto VB
ClrHome
Output(5,9,"FINALIZING
Wait S:ClrHome
Output(5,11,"SUCCESS
Return
The first portion of the program checks to see if two calculators are connected.
E is the error counting, A is the "unique identity" for each calculator, B holds what A was before checking the connection.
Code:
0->E
ClrHome
Lbl 0
Output(5,9,"CONNECTING
Wait 1
If not(max(A={0,1:0->A
A->B
GetCalc(A:not(A->A
If A!=B:Then
E+1->E:Output(6,9,Ans
End
If E=5:Then
ClrHome
Output(5,12,"FAILED
Return
End
If A!=B:Goto 0
If another calculator is connected, the variable A is received and the value is flipped right after and should be different from B. If no calculator is connected, the getCalc( command does nothing and is flipped anyways, and it would be the same as B which the connection doesn't work. The calculator tries 5 times in total before giving up.
In this next portion of code, The variable V is the value the user wants to send to the other calculator. After, V is stored to D so the calculator can compare later. πe² is stored to B to tell the other calculator the value is set and ready to receive from the linked calculator. After it gives the other calculator 5 seconds by default to receive the other variable before continuing.
Code:
Lbl A
1->A
Input "Value: ",V
V->D
pi[e]^^2->B
Lbl VS
ClrHome
Output(5,11,"WAITING
Output(6,11,S
Wait S
This next section of code attempts to verify that the receiving calculator actually grabbed the value the sender just made. On the receiving calculator, it sets that same πe² value to C once the variable V is received. So now the sender is checking to see if C is that special value or not. It tries twice and if it doesn't get anything, it gives up. Note for this, there is no Wait commands for delays.
Code:
ClrHome
Output(5,9,"VERIFYING
0->V:0->C:GetCalc(C:GetCalc(V
If C!=pi[e]^^2 or V!=D:Then
S+1->S:E+1->E
End
If E=2:Then
ClrHome
Output(5,8,"DISCONNECTED
Return
End
If C!=pi[e]^^2 or V!=D
Goto VS
The last bit of code for the sender starts another delay so the receiving calculator can finalize the transmission with a delay.
Code:
ClrHome
Output(5,6,"SENDING HANDSHAKE
Wait S
ClrHome
Output(5,8,"SEND SUCCESS
Return
Now for the receiving calculator, it waits one second and then attempts to retrieve the V and B variables from the sender. If the sender is ready for the receiver to retrieve the variables, it sets B to the special value. If both B is equal to πe² and V is a non-zero value, then it continues the program. Otherwise it attempts to do it again twice before it gives up.
Code:
ClrHome
Output(5,10,"RECEIVING
Wait 1
If E:Output(6,10,E
GetCalc(V:GetCalc(B
If V and B=pi[e]^^2:Then
Output(7,10,"
Output(6,10,V
pi[e]^^2->C
Else
E+1->E
End
If E=3:Then
ClrHome
Output(5,8,"DISCONNECTED
Return
End
If not(V) or B!=pi[e]^^2
Goto B
0->E
The last remainder of code waits to check the "handshake" variable from the sender. It verifies by checking if the sender calculator knows that the receiving calculator retrieved the V variable or not. It attempts this twice before giving up.
Code:
0->E
Lbl VB
Output(5,3,"WAITING FOR HANDSHAKE
Wait S
ClrHome
Output(5,5,"RECEIVING HANDSHAKE
If E:Output(6,4,E
0->C:GetCalc(C
If C!=pi[e]^^2:Then
E+1->E:S+2->S
End
If E=2:Then
ClrHome
Output(5,8,"DISCONNECTED
Return
End
If C!=pi[e]^^2
Goto VB
ClrHome
Output(5,9,"FINALIZING
Wait S:ClrHome
Output(5,11,"SUCCESS
Return
So far this what I came up with, I'm not sure if there is a better way to go at this especially with the limitations of BASIC. If you have any suggestions, feel free to make revisions or comment down below!