Menu, Anyone?

Today I added some menu buttons with a cool-ish wiggle affect when they get clicked.

This commit is a bit complicated, but essentially I added four menu buttons that you can click on. When you do click on one, it wiggles before the screen goes to that option. If you “hover” over a button, then it gets bigger. These are a few things that I believe would be needed for any game, so I was adamant to include them here.

Consider this, in any game you play, a button may be there, but dimmed out because it is not available. Or perhaps it will toggle to a different color or shape. These are pretty important things in a game’s arsenal, so I wanted to work with and on them here.

I wont bore you with all the code, but you can check out my GitLab for all the details. However, earlier, when making the water drum bobble, I said that I could have just made a separate sprite for the different halves of the drum, and moved the top half instead of making separate sprites to show the motion. Well, to put that to practice, I did that here with these buttons.

Watch what happens when you click one:

if (m_Active) {

// Since the button was pressed, let’s do some motion!
m_Motion = m_Motion + elapsedTime;
float motionCount = m_Motion;

if (motionCount == 1) {
m_Position.x = m_Position.x + 2.5;
m_Position.y = m_Position.y + 2.5;
m_Sprite.setPosition(m_Position);
} else if (motionCount <= .15) {
m_Position.x = m_Position.x + 2.5;
m_Position.y = m_Position.y + 2.5;
m_Sprite.setPosition(m_Position);
} else if (motionCount <= .30) {
m_Position.x = m_Position.x – 5.0;
m_Position.y = m_Position.y – 5.0;
m_Sprite.setPosition(m_Position);
} else if (motionCount <= .45) {
m_Position.x = m_Position.x + 5.0;
m_Position.y = m_Position.y;
m_Sprite.setPosition(m_Position);
} else if (motionCount <= .60) {
m_Position.x = m_Position.x – 5.0;
m_Position.y = m_Position.y + 5.0;
m_Sprite.setPosition(m_Position);
} else if (motionCount <= .75) {
m_Position.x = m_Position.x + 2.5;
m_Position.y = m_Position.y + 2.5;
m_Sprite.setPosition(m_Position);
} else if (motionCount <= .90) {
m_Position.x = m_Position.x;
m_Position.y = m_Position.y -5.0;
m_Sprite.setPosition(m_Position);
} else {
m_Position.x = m_Position.x;
m_Position.y = m_Position.y;
m_Sprite.setPosition(m_Position);
}
} else {

// What else do we need to do with this?
}

As you can see, I am using the accumulative time (given in fractions of seconds, sent every frame) to control the direction and movement of the button. As it sits now, the button goes up and right, down and left, to the right, up and left, down, and then back to center before it is done with it’s “pressed” function. You could make this random, or what every you like, I just kept it simple with a set pattern.

Interesting to note, the button doesn’t always end up perfectly back at the start this way. Why is that? Well, because every frame may take different lengths of time, it may do more frames in the up and to the right section than the number of frames it takes for the down and to the left section.

A good clean up for this would be to set the button back to it’s original position when done, rather than letting it end where it was. However, after clicking the button, the screen changes, so I didn’t need to worry about that.

Be sure to check out the game! Freestyle is more or less playable at present. Just drumming around!

Linux – keep it simple.

Advertisements

BLOX2: Overclocking the GPU on a 64 bit kernel!

gpuoc

Another update to the BLOX2 (BLU Life One X2) kernel, I just overclocked the GPU from 450 MHz to 500 MHz! You can check out the commit on my GitLab. It’s all part of my video series for modifying 64 bit kernels, which you can check out on my YouTube channel: https://www.youtube.com/channel/UCnGqG_jyyXmTzdamBpKfeHA

Linux – keep it simple.

Bluetooth Breakdown: Sending gatttool commands from my Ubuntu Touch Phone!

Here I am, typing commands from my phone and controlling a Bluetooth Feather!

Of course, the moment of truth! The goal of this whole Bluetooth breakdown project was to figure out how to make my home made auto start work on my Ubuntu Touch phone. Well, here it is, working!

Although the interface could use a face lift.

Last we looked at this, we saw that we could write custom scripts to send the commands from the desktop computer to the BLE auto start, or use interactive mode of gatttool and do the work there. Now we can actually do the same from the terminal of the cell phone.

While this technically counts. We now need a snazzy program that just works with clicky buttons and that sort of thing. So, I suppose that will be the next phase of this project! Stay tuned for adventure!

Linux – keep it simple.

SFML error: ‘string’ was not declared in this scope

2018-06-11-115438_1280x1024_scrot

In the memory game, a large right hand gives you a demo of the drum order for the round.

Work continues on my water drumming game. I’ve been learning a lot by creating this game, which was outside of the scope and tutorials found in my beginner C++ class. I’m pretty sure that my code looks as ugly as the artwork, but it is functional, which is a great feeling!

One error that I ran into confused me for a bit:

DrumGame.cpp:130:5: error: ‘string’ was not declared in this scope
string demoMode = “”;

But I have used string before and had no issues. Specifically, it came down to the fact that I’m using sfml. For some reason, there is a sf::String, and std::string. Notice the capitalization. According to SFML’s documentation:

Utility string class that automatically handles conversions between types and encodings.

sf::String is a utility string class defined mainly for convenience.

It is a Unicode string (implemented using UTF-32), thus it can store any character in the world (European, Chinese, Arabic, Hebrew, etc.).

It automatically handles conversions from/to ANSI and wide strings, so that you can work with standard string classes and still be compatible with functions taking a sf::String.

So, you either need to use String, so you can use SFML’s String, or you need to append std:: to the front of string so you can used the standard string.

Either way, the latest commit is up, where you can select “memory” from the menu, and it will demonstrate the pattern you need to repeat. Right now, since you can’t play back the pattern by clicking, it only shows you the pattern of one. The idea is once you complete that pattern, it adds another drum to your pattern for you to play next.

Stay tuned for part 2 where I will *hopefully* finish the memory mode.

Linux – keep it simple.

Bluetooth Breakdown: Sending my own commands with gatttool

Yes, there is purposely one too many t’s in GattTool.

bt_success

Last time we looked at the Bluetooth Low Energy (BLE) packets being sent from my Android app to my home made auto start. Here’s what we saw:

214231313a – Button 1 pressed
214231303b – Button 1 released
2142323139 – Button 2 pressed
214232303a – Button 2 released

With a little more digging, I also found that the “handle” is important, and per the Bluetooth packet breakdown in WireShark, that handle is 0x001a. Further, my particular Bluetooth feather’s MAC address is F4:64:8E:90:D8:C3.

With all of this in mind, I entered gatttool in the interactive mode, but I kept having trouble connecting to my device. With further research, I found an article on WordPress by a guy named Jack. He got me pointed in the right direction!

What I needed to do before connecting was this:

# btmgmt le on
# btmgmt bredr off

To set the computer’s Bluetooth manager to the proper Low Energy reading/writing state. Then I entered interactive mode while monitoring my auto start with a USB serial monitor. Here’s what I typed:

root@alaskalinuxuser-OptiPlex-7010:/home/alaskalinuxuser# gatttool -t random -b F4:64:8E:90:D8:C3 -I
[F4:64:8E:90:D8:C3][LE]> connect
Attempting to connect to F4:64:8E:90:D8:C3
Connection successful
[F4:64:8E:90:D8:C3][LE]> char-write-cmd 0x001a 214231313a
[F4:64:8E:90:D8:C3][LE]> char-write-cmd 0x001a 214231303b
[F4:64:8E:90:D8:C3][LE]> char-write-cmd 0x001a 2142323139
[F4:64:8E:90:D8:C3][LE]> char-write-cmd 0x001a 214232303a
[F4:64:8E:90:D8:C3][LE]> char-write-cmd 0x001a 214232303a
[F4:64:8E:90:D8:C3][LE]> char-write-cmd 0x001a 214231303b
[F4:64:8E:90:D8:C3][LE]> char-write-cmd 0x001a 2142323139
[F4:64:8E:90:D8:C3][LE]> char-write-cmd 0x001a 214232303a
[F4:64:8E:90:D8:C3][LE]> char-write-cmd 0x001a 2142323139
[F4:64:8E:90:D8:C3][LE]> disconnect

(gatttool:22352): GLib-WARNING **: Invalid file descriptor.

[F4:64:8E:90:D8:C3][LE]> disconnect
[F4:64:8E:90:D8:C3][LE]> exit
root@alaskalinuxuser-OptiPlex-7010:/home/alaskalinuxuser#

And here is what I saw on the monitor:

Less than 13v, Pin A3: 0.00
Less than 13v, Pin A3: 0.00
Less than 13v, Pin A3: 0.00
Less than 13v, Pin A3: 0.00
Button 1 released
Less than 13v, Pin A3: 0.00
startTimer 4
Less than 13v, Pin A3: 0.00
startTimer 3
Less than 13v, Pin A3: 0.00
startTimer 2
Less than 13v, Pin A3: 0.00

Success! Great! I was able to enter interactive mode, connect, and send the right commands! This is great!

Then I switched to non-interactive mode, by writing a custom script or two. This is the first one, to start the auto start:

#!/bin/bash

btmgmt le on
btmgmt bredr off
gatttool -t random -b F4:64:8E:90:D8:C3 –char-write-req -a 0x001a -n 214231313a
gatttool -t random -b F4:64:8E:90:D8:C3 –char-write-req -a 0x001a -n 214231303b
#gatttool -t random -b F4:64:8E:90:D8:C3 –char-write-req -a 0x001a -n 2142323139
#gatttool -t random -b F4:64:8E:90:D8:C3 –char-write-req -a 0x001a -n 214232303a
exit 0

And one to stop the auto start:

#!/bin/bash

btmgmt le on
btmgmt bredr off
#gatttool -t random -b F4:64:8E:90:D8:C3 –char-write-req -a 0x001a -n 214231313a
#gatttool -t random -b F4:64:8E:90:D8:C3 –char-write-req -a 0x001a -n 214231303b
gatttool -t random -b F4:64:8E:90:D8:C3 –char-write-req -a 0x001a -n 2142323139
gatttool -t random -b F4:64:8E:90:D8:C3 –char-write-req -a 0x001a -n 214232303a
exit 0

And a quick test was successful! Now I can send my commands to start or stop the auto start from the command line with my computer. Now it’s just time to set it up on my Ubuntu Touch phone!

Linux – keep it simple.

Some Wobble To Your Bobble?

In the continuing efforts to make a fun game that tests my (very limited and basic) skills as a C++ programmer, as well as to further the bounds of SFML, I am expanding the Water Drum Game. You should check out my latest commit to see how I put some wobble into the water drum bobble!

wiggle_drum

The overall code is fairly simple, but comes in two main parts. Here is some relevant code from the WaterDrum.cpp:

void WaterDrum::update(bool stillActive, float elapsedTime, float sResolution) {

if (stillActive) {

m_Motion = m_Motion + elapsedTime;
float motionCount = m_Motion;

// Update the sprite if struck.
if (motionCount == 1.75) {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_still.png”));
} else if (motionCount <= .25) {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_1.png”));
} else if (motionCount <= .5) {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_2.png”));
} else if (motionCount <= .75) {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_3.png”));
} else if (motionCount <= 1) {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_4.png”));
} else if (motionCount <= 1.25) {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_5.png”));
} else if (motionCount <= 1.5) {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_6.png”));
} else {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_still.png”));
}
} else {
m_Sprite.setTexture(TextureHolder::GetTexture(
“graphics/waterdrum_still.png”));

}

if (m_Motion >= 1.75)
{
m_Motion = 0;
m_Active = false;
}

// Apperantly we have to update the scale when we change the sprite.
m_Sprite.setScale((sResolution*.2)/500,(sResolution*.2)/500);
m_Sprite.setPosition(m_Position);

 

}

And here is the part where we call it in the DrumGame.cpp:

for (int i = 0; i < numDrums; i++) {
waterDrums[i].update(waterDrums[i].isActive(), dtAsSeconds, resolution.x);
}

As long as the window is open, on every pass of code, it will run this for loop to check and update every water drum. It passes three variables:

  1. isActive: This was set to true for each drum when it was “struck” or clicked on. After it is done “bobbling” it will be set to false.
  2. dtAsSeconds: This is a value of a fraction of a second that it took to run the code between frames. This is really important for making a realistic “flowing” graphic.
  3. resolution.x: This value is used to send the screen size x variable, so calculations can be made for scaling the art of each sequence of drum.

Once those variables are received, there are two options for the code. If it is active, then it will calculate the “bobble” by adding the time elapsed together. At designated times (which I could arbitrarily set to anything I want) the different phases of the bobble happen by replacing the graphic with another. I have decided that after being struck the water drum should bobble for 1.75 seconds total. During that time, 6 different art frames are used for the drum to create the bobble.

Note: I could have changed this to be movement of position as well, having a separate graphic for the outer and inner bowls of the drum. It could have been random as well as including different scaling to adjust the size of it. I felt this was easier, since our textureHolder class is keeping the drums in memory, we only have to load them once, and then “swap” them into these useful spots as required, but either would have worked, I think.

Once the time is up, the game sets the visual back to the still view and sets the motion time back to zero, as well as the active flag back to false. Now it is ready to start the process over.

Notice that the code for striking the drum starts this process over, not adding time in an accumulative fashion. This prevents a “forever” bobble of 15 seconds if you were to strike the drum 10 times in two seconds. It only bobbles from the last strike, so it will only bobble a maximum of 1.75 seconds after the last strike.

Either way, it is now semi-playable in a free play mode, so you are free to check it out on GitLab!

Linux – keep it simple.

Bluetooth Breakdown?

So, in the continuing saga of learning about Bluetooth, I am breaking down some transmissions of Bluetooth signal from my Android phone to the Bluetooth auto start that I built.

btauto

  1. On my Android phone, I went to Settings -> Developer Options -> Enable Bluetooth HCI snoop log. Now my Android phone was creating a dump file on the sdcard of all Bluetooth traffic.
  2. I opened my app, connected to the auto start, and promptly started the truck, and shut it down. Then I waited about 3 seconds, started the truck, and shut it down. Then I disconnected and reconnected 4 times to the auto start.
  3. I turned off the Bluetooth snoop log and then transferred the file to my computer for analysis.
  4. Finally, I opened the file with WireShark, and started reviewing the packets.

From what I understand, the ATT protocol is one that sends and receives actual commands to and from the controlling application to the BLE board itself. So, I sorted the output and looked specifically at the ATT protocol packets.

Further research has lead me to believe that I need to look for btatt.opcode, particularly 0x52, which is for the write command. I found 8 packets that held this special designation.

btauto1

In these packets are values. In order of appearance, they are:

21:42:31:31:3a
21:42:31:30:3b
21:42:32:31:39
21:42:32:30:3a
21:42:31:31:3a
21:42:31:30:3b
21:42:32:31:39
21:42:32:30:3a

Which would correspond to my bluetooth device code of:

// Buttons
if (packetbuffer[1] == ‘B’) {
uint8_t buttnum = packetbuffer[2] – ‘0’;
boolean pressed = packetbuffer[3] – ‘0’;
Serial.print (“Button “); Serial.print(buttnum);
if (pressed) {
Serial.println(” pressed”);
//bluart.print(“pressed”);
analogWrite(19, 50);
} else {
Serial.println(” released”);
analogWrite(19, 0);
}
if (buttnum == 1) {
// Turn on vehicle, by turning on accessory, fuel pump,
// and start timer to crank.
digitalWrite(7, LOW);
digitalWrite(16, HIGH);
startTimer = 5; // Delay time before cranking.
startAttempts = 3; // Number of tries to start.
} else if (buttnum == 2) {
// Turn everything off from the button.
digitalWrite(16, LOW);
digitalWrite(15, LOW);
digitalWrite(7, HIGH);
digitalWrite(11, HIGH);
crankTimer = -1;
startTimer = -1;
runTimer = -1;
startAttempts = -1;
Serial.println(” Kill All – Button pressed. “);
}
}

That’s right! Now we can interpret the signals!

21:42:31:31:3a – Button 1 pressed!
21:42:31:30:3b – Button 1 released!
21:42:32:31:39 – Button 2 pressed!
21:42:32:30:3a – Button 2 released!

That’s great! Now we can use a low level tool like gatt to send the command itself, without the whole interface! Now I just need to implement it!

Linux – keep it simple.