Ruby script - HTC Rezound

I am in an introduction to programming class and have some questions on a script I am working on. Anyone familiar with the Ruby
(.rb) language. Thanks
Sent from my Nexus 7 using xda app-developers app

Not really sure why this is in the Rezound QA...
I'm not familiar with Ruby (other than just looking at some code a few times)... but what's the issue you're having exactly?

lol I figured I would get that exact response, but it seemed like a good place to ask to be honest. I have this set of code here, Im not cut out for programming and was looking for help this is the pseudocode for the project I am working on.
MAXLINELENGTH = 40
codes = ['.-', '-...', '-.-.', '-..', '.', '..-.',
'--.', '....', '..', '.---', '-.-', '.-..',
'--', '-.', '---', '.--.', '--.-', '.-.',
'...', '-', '..-', '...-', '.--', '-..-',
'-.--', '--..','.----', '..---', '...--',
'....-', '.....', '-....', '--...', '---..',
'----.', '-----']
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
f1 = File.open("index.txt", "w")
f2 = File.open("index.txt", "r")
Initialize line_length = 0
while character = fin.getc
if index = chars.index(character.upcase)
code.getc
Print code to fout
Print " " to fout
line_length = line_length + code.length + 2
end
elsif character == " "
Print " " to. fout
line_length += code.lenght + 4
end
if line_length >= MAXLINELENGTH
Print new line character to fout.print "\n"
Reset line_length to 0
end
end
fout.close
fin.close
Basically the goal is to print letters and numbers to a documnet labeled idex.txt but the user input characters would be translated in morse code in the txt document

zkrp5108 said:
lol I figured I would get that exact response, but it seemed like a good place to ask to be honest. I have this set of code here, Im not cut out for programming and was looking for help this is the pseudocode for the project I am working on.
MAXLINELENGTH = 40
codes = ['.-', '-...', '-.-.', '-..', '.', '..-.',
'--.', '....', '..', '.---', '-.-', '.-..',
'--', '-.', '---', '.--.', '--.-', '.-.',
'...', '-', '..-', '...-', '.--', '-..-',
'-.--', '--..','.----', '..---', '...--',
'....-', '.....', '-....', '--...', '---..',
'----.', '-----']
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
f1 = File.open("index.txt", "w")
f2 = File.open("index.txt", "r")
Initialize line_length = 0
while character = fin.getc
if index = chars.index(character.upcase)
code.getc
Print code to fout
Print " " to fout
line_length = line_length + code.length + 2
end
elsif character == " "
Print " " to. fout
line_length += code.lenght + 4
end
if line_length >= MAXLINELENGTH
Print new line character to fout.print "\n"
Reset line_length to 0
end
end
fout.close
fin.close
Basically the goal is to print letters and numbers to a documnet labeled idex.txt but the user input characters would be translated in morse code in the txt document
Click to expand...
Click to collapse
So your taking input and converting to Morse code then logging it to a text document? Never tried that language but it looks similar to visual basic.
If so, I don't see a loop set up to parse through the input string. You will need to iterate through each character, then I'm assuming there is a separate class (or function?) that will return the Morse code for each character.
Sorry I've only ever used c++ and java
Sent from my ADR6425LVW using xda app-developers app

Squirrel1620 said:
So your taking input and converting to Morse code then logging it to a text document? Never tried that language but it looks similar to visual basic.
If so, I don't see a loop set up to parse through the input string. You will need to iterate through each character, then I'm assuming there is a separate class (or function?) that will return the Morse code for each character.
Sorry I've only ever used c++ and java
Sent from my ADR6425LVW using xda app-developers app
Click to expand...
Click to collapse
Yea Im really bad at this, I was super excited to learn it but its not going well. I cant think in a rules and order kinda thing, I'm just more free thinking I need to be able to change stuff to however I want. So I just don't understand what I am looking at lol.

zkrp5108 said:
Yea Im really bad at this, I was super excited to learn it but its not going well. I cant think in a rules and order kinda thing, I'm just more free thinking I need to be able to change stuff to however I want. So I just don't understand what I am looking at lol.
Click to expand...
Click to collapse
Well I'm self taught I guess it's just a personality thing lol. Basically think of coding like this : write this program : make a pb+j sandwich, literally write down EVERY step you need to do in order to make it... Kinda weird analogy, but depending on how deep you go that could end up being tens of thousands of lines of code.
So in your case write down on paper EVERY step you need to do to get morse code from the input and write it to a text file. That will be your pseudocode now you need to translate that into code. Remember Google is your friend when you need to find the syntax needed to do what you want.
Wish I had more free time and experience in ruby to help
Sent from my ADR6425LVW using xda app-developers app

Easiest thing to do, will be to loop through the entire input string and parse each letter at a time. Had to do a morse-code conversion program in several older languages (Pascal, Fortran, Ada, C, C++) for my Programming Languages course last semester. Pseudocode was something like:
Get input from user (or parse an input file).
Store input into a string (or a string array if multiple lines).
Loop through string and check each letter individually and look up what the morse-code equivalent is.
Store the morse code equivalent in a new string inside the loop.
When you hit the end of the string (end of the loop), save the morse string to a file.
In the loop, checking each individual letter, a case statement (or if else-if else's) will be the easiest (more typing, but easier logic).
So, for example, here's a C++ version that I wrote for my class last semester:
Code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string search(char character);
int main()
{
string encryptionString = "";
string morseString = "";
cout << "Enter a sentence, containing only letters and blanks \nand ending with a period, to convert to morse code: " << endl;
getline(cin, encryptionString);
//remove ending period, if it exists
if(encryptionString[encryptionString.length() - 1] == '.')
{
encryptionString.erase(encryptionString.length() - 1);
}//if
for(int i = 0; i < encryptionString.length(); i++)
{
encryptionString[i] = toupper(encryptionString[i]);
}//for
int index = 0;
while(index < encryptionString.length())
{
if(encryptionString[index] != ' ' || encryptionString[index] != '\n')
{
cout << " ";
morseString = search(encryptionString[index]);
cout << morseString;
}
else
{
cout << endl;
morseString = "";
}
index++;
}//while
cin.ignore();
cout << "\n\nPress enter to exit...";
cin.get();
return 0;
}//main
string search(char character)
{
string returnString = "";
if(character >= 65 || character <= 90 )
{
switch(character)
{
case 'A':
returnString = ".-";
break;
case 'B':
returnString = "-...";
break;
case 'C':
returnString = "-.-.";
break;
case 'D':
returnString = "-..";
break;
case 'E':
returnString = ".";
break;
case 'F':
returnString = "..-.";
break;
case 'G':
returnString = "--.";
break;
case 'H':
returnString = "....";
break;
case 'I':
returnString = "..";
break;
case 'J':
returnString = ".---";
break;
case 'K':
returnString = "-.-";
break;
case 'L':
returnString = ".-..";
break;
case 'M':
returnString = "--";
break;
case 'N':
returnString = "-.";
break;
case 'O':
returnString = "---";
break;
case 'P':
returnString = ".--.";
break;
case 'Q':
returnString = "--.-";
break;
case 'R':
returnString = ".-.";
break;
case 'S':
returnString = "...";
break;
case 'T':
returnString = "-";
break;
case 'U':
returnString = "..-";
break;
case 'V':
returnString = "...-";
break;
case 'W':
returnString = ".--";
break;
case 'X':
returnString = "-..-";
break;
case 'Y':
returnString = "-.--";
break;
case 'Z':
returnString = "--..";
break;
}//switch
}//if
return returnString;
}//search
For you, you would instead print to a file instead of print to screen. You'll need to look up the exact syntax for Ruby, but should be easy.

Related

[Q] How to trigger a onKeyShortCut event

I find that there is a function onKeyShortCut in every View Component, but when I read the SDK document, I find that the function can only be trigger by the function setShortcut in menuitem component, is there any other way to trigger this event, for example, through send a keyEvent.
Solution to the keyShortcut
cuibuaa said:
I find that there is a function onKeyShortCut in every View Component, but when I read the SDK document, I find that the function can only be trigger by the function setShortcut in menuitem component, is there any other way to trigger this event, for example, through send a keyEvent.
Click to expand...
Click to collapse
After I check the souce code of the android platform, I find that when a keyEvent happened, the event passed to ViewRoot before to the windows, and then the windows pass it to the child component in the, and finally to the Activity. From the code in ViewRootImpl.java, I find the condition to trigger this event.
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.isCtrlPressed()
&& event.getRepeatCount() == 0
&& !KeyEvent.isModifierKey(event.getKeyCode())) {
if (mView.dispatchKeyShortcutEvent(event)) {
finishInputEvent(q, true);
return;
}
}
It means if a KeyEvent includes Ctrl keycode, repeatCount = 0, and no other function keys, it will deliver it to the View component. So I make a KeyEvent like the following
KeyEvent keyevent_parse = new KeyEvent(System.currentTimeMillis(),System.currentTimeMillis(),KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_V,0,KeyEvent.META_CTRL_MASK);
Then it works, you can use this to release a parse function to any textView component.

[Q] Help developing a looping video live wallpaper

I'm a very new Java developer, having started learning it last month specifically for this project. I have fifteen years of development experience, but it has almost all been web related (html, JS, JQuery, ColdFusion, etc), so this is a major paradigm change that I'm having trouble wrapping my head around.
Anyway, I'm attempting to create a movie-based live wallpaper to sell on the app store. I have a 15 second mpg (or 450 png frames) derived from some rendered artwork I did, the bottom 35% of which has motion (the rest remains relatively static). I'd like code flexible enough to handle future animations as well, though, as I just rediscovered Vue and may do other videos where the entire frame has motion.
My initial attempts are detailed on my Stack Overflow question at: (link removed due to forum rules; findable with the title: How do you create a video live wallpaper).
That post, in short, boils down to having tried these different approaches:
Load frames into a bitmap array and display on canvas in loop; excellent FPS but hundreds of MB of memory use.
Load frames into byteArray as jpgs and decode during display; clocking in at only 10 FPS at 60% cpu usage on powerful hardware, but with good memory usage.
Load tiled sprite with all 450 frames in AndEngine as a texture and display; went oom while trying to allocate 200 MB of memory.
AndEngine again. Load tiled jpg with 10 frames into sprite, load next tiled jpg into a second sprite, every 400ms hide one sprite and display the second, then load the upcoming jpg into the hidden sprite; rinse, repeat. Attempting to decode in a makeshift buffer, essentially.
I feel like maybe method 4 has promise and am including the code I'm using below. However, every time the sprites are swapped out the screen freezes for as long as a second or two. I tried adding timers between every line of code to determine what's taking so much time, but they almost always come back with barely a millisecond or two taken, leaving me confused about where the freeze is occurring. But I don't understand AndEngine well yet (or even Java) so I may be doing something completely boneheaded.
I'd welcome any thoughts, whether a refinement on an existing method or a completely new idea. I've had a horrible time trying to find tutorials on doing this, and questions I find here and on SO generally don't offer much encouragement. I just want to get this thing finished so I can concentrate on the heart of this project: the art. Thanks!
As an aside, how much work would this be (ie: how much would it cost) for an experienced developer to create a template for me? I wouldn't mind paying a small amount for something I can keep using with future animations.
Code:
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
scene = new Scene();
initializePreferences();
// Water
waterTexture = new BitmapTextureAtlas(this.getTextureManager(), 1200, 950, TextureOptions.BILINEAR);
waterRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(waterTexture, this.getAssets(), "testten1.jpg", 0, 0, 2, 5);
waterTexture.load();
waterTexture2 = new BitmapTextureAtlas(this.getTextureManager(), 1200, 950, TextureOptions.BILINEAR);
waterRegion2 = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(waterTexture2, this.getAssets(), "testten2.jpg", 0, 0, 2, 5);
waterTexture2.load();
water = new AnimatedSprite(0, 0, waterRegion, this.getVertexBufferObjectManager());
water2 = new AnimatedSprite(0, 0, waterRegion2, this.getVertexBufferObjectManager());
scene.attachChild(water);
water.animate(40);
mHandler.postDelayed(mUpdateDisplay, 400);
}
private final Handler mHandler = new Handler();
private final Runnable mUpdateDisplay = new Runnable() {
[user=439709]@override[/user]
public void run() {
changeWater();
}
};
public void changeWater() {
mHandler.removeCallbacks(mUpdateDisplay);
mHandler.postDelayed(mUpdateDisplay, 400);
if (curWaterTexture == 1) {
Log.w("General", "Changed texture to 2 with resource: " + curWaterResource);
curWaterTexture = 2;
scene.attachChild(water2);
water2.animate(40);
scene.detachChild(water);
curWaterResource = curWaterResource + 1;
if (curWaterResource > 4) curWaterResource = 1;
String resourceName = "testten" + curWaterResource + ".jpg";
waterRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(waterTexture, this.getAssets(), resourceName, 0, 0, 2, 5);
waterTexture.load();
water = new AnimatedSprite(0, 0, waterRegion, this.getVertexBufferObjectManager());
} else {
Log.w("General", "Changed texture to 1 with resource: " + curWaterResource);
curWaterTexture = 1;
scene.attachChild(water);
water.animate(40);
scene.detachChild(water2);
curWaterResource = curWaterResource + 1;
if (curWaterResource > 4) curWaterResource = 1;
String resourceName = "testten" + curWaterResource + ".jpg";
waterRegion2 = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(waterTexture2, this.getAssets(), resourceName, 0, 0, 2, 5);
waterTexture2.load();
water2 = new AnimatedSprite(0, 0, waterRegion2, this.getVertexBufferObjectManager());
}
}

Need help with voice recognition application dev

I'm developing an augmentative communication application for use on Kindle Fire. I'm using Fire HD 6 as my test device. I'm working in Xamarin, C#.
I know there is a speech recognizer on the device as the microphone icon appears on the keyboard and I can use it to populate the search window. However, my andoid speech recognizer code is not working. I get the "recognizer not present" error. Here is the code that I'm working with:
public class VoiceRecognition : Activity
{
private static String TAG = "VoiceRecognition";
private const int VOICE_RECOGNITION_REQUEST_CODE = 1234;
private ListView mList;
public Handler mHandler;
private Spinner mSupportedLanguageView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
mHandler = new Handler();
SetContentView(Resource.Layout.Main);
Button speakButton = FindViewById<Button>(Resource.Id.btnRecord);
// Check to see if a recognition activity is present
PackageManager pm = PackageManager;
IList<ResolveInfo> activities = pm.QueryIntentActivities(new Intent(RecognizerIntent.ActionRecognizeSpeech), 0);
if (activities.Count != 0)
speakButton.Click += speakButton_Click;
else
{
speakButton.Enabled = false;
speakButton.Text = "Recognizer not present";
}
}
Click to expand...
Click to collapse
This code is obviously not going to work, but I don't know where to go from here. How can I access the voice recognizer on this device?
Thanks!

How does one use a seekbar to cycle through an array?

I am currently trying to make a seekbar cycle through colors on progress(when moved). I have an xml file with some defined colors within it, this is what i have tried so far.
seekbarcolor.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int seekbarprogress = 0;
@override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekbarprogress = progress;
int colorset = Color.argb(0xff, progress*0x10000, progress*0x100, progress)+0xff000000;
drawPaint.setColor(colorset);
}
This code generates and uses only 1 base color. I want to instead pull from an xml file or an array defined in the class. How do i do this?

Processing MotionEvent from stylus on Android

Hello everybody
Unfortunately, I have too few post to make a thread in the " Android Software Development" forum. I would be very happy if this post can be moved to it.
I'm using an Android 5.1 tablet with a stylus (also supporting pressure). The below code shows how I process a motion event from the stylus. I have read about it in the Android documentation but it is still not that clear to me what exactly happens.
Especially historySize and pointerCount are unclear to me. Why is there a pointerCount, i.e. several position and pressure values? That a history has a certain size (i.e. historySize) is clear to me but why do we have this history?
So let's say I have one event from my stylus. In my understanding this event should just produce one position and pressure value but with the below code it can (and will) generate several values. Why?
Also the timestamps are not that clear to me. All values in the pointerCount-Loop have the same timestamp (timestampEvent) but every value in the history has a timestampOffset. How can I get the difference in milliseconds between the timestampOffset and timestampEvent?
Or should the stylus events processed in another way than I do?
Thank you very much for the answers and have a nice weekend.
Code:
public static void processMotionEvent(MotionEvent ev) {
long timestampEvent = ev.getEventTime();
String action = MotionEvent.actionToString(ev.getAction());
float offsetX = ev.getRawX()-ev.getX();
float offsetY = ev.getRawY()-ev.getY();
final int historySize = ev.getHistorySize();
final int pointerCount = ev.getPointerCount();
for (int h = 0; h < historySize; h++) {
long timestampOffset = ev.getHistoricalEventTime(h);
for (int p = 0; p < pointerCount; p++) {
float positionX = ev.getHistoricalX(p, h) + offsetX;
float positionY = ev.getHistoricalY(p, h) + offsetY;
float pressure = ev.getHistoricalPressure(p, h);
}
}
for (int p = 0; p < pointerCount; p++) {
float positionX = ev.getX(p) + offsetX;
float positionY = ev.getY(p) + offsetY;
float pressure = ev.getPressure();
}
}

Categories

Resources