Tuesday, January 24, 2012

Get HTML in C#

In many situations, I needed to fetch a simple HTML (or other type) document over the HTTP protocol in C#. So here is my final solution to the problem, that mostly will work!
Sometimes it'll give you a hard time, because some websites don't like the Header, but it's great to fetch "real" HTML from a simple server.


Usings:
 using System.Net;
using System.IO;

Code:
  String getHtml(String URL)
        {
            string result = "";
            try
            {
                // Get HTML data
                WebClient client = new WebClient();
                Stream data = client.OpenRead(URL);
                StreamReader reader = new StreamReader(data);
                string str = "";
                str = reader.ReadLine();

                while (str != null)
                {
                    result += str;
                    str = reader.ReadLine();
                }
                data.Close();
            }
            catch (WebException exp)
            {
               return exp.Message;
            }
            return result;
        }

Saturday, January 14, 2012

System engineering the life, the universe and everything!

ATTENTION: This post was written in a phase of general anger and disagreement with a normal person. If you consider yourself easy to be offended, maybe you shouldn't read the text below...

Hi!
As you might have experienced, it is quite easy to tell apart "nerds" from "normal people" by just comparing their sight on life, the universe and everything.
A simple explanation to the rather extremous views of some would be that they think too much about software and to less about real life, but in reality, nerds only use their knowledge on software and logic structures to understand their surroundings.
It might no be true for everyone, but the further you go into logic structures, the more likely you will recognize them in the real life. Many thing just demand you to think about them. Take for example the placement of a banana in a fridge. A normal person would just stuff it somewhere, where enough space is free, but I would place it in the highest point available, because too low temperatures would destroy the flavor of the fruit. What might be seen as an exaggerated and overcomplicated use of thermal physics (cold air goes downwards), is for people like me just a milisecond of thought. Also other situations in everyday life like conversation are handled like this. Why do I instantly have an opinion on mostly everything I get to know? Well, because I can't help but seeing it as a logic process.
Why do I hate DMTF tones? Because they are not in harmony.
Many people would think about this and come to the conclusion, that I see everything as a scientific problem, but that is not true.
I live my life as I do without feeling like solving equations and stuff.
I am able to do these thing without noticing and because I don't have to think about everything I do (it is done in the background and it does not take hours) I can live more satisfied than someone thinking about simple questions all the time.
This can sometimes lead to problems as some people aren't able to talk about other things than their limited problems.
I don't like complaining about what is existing. I like DOING CHANGE things.
Same thing applies to our politicians. Oh god, we have a decrease in tax income. Oh my god, our roads are getting broken. While a normal nerd would just come up with a soulition these people just keep on complaining.
Here in Germany, you can see nerds getting into politics and I'm sure you can see it in other places too, but politics is not enough. We need to convince other people to stop coplaining and start DOING.
If you come to the conclusion, that there is a problem with your life, then get solving it and don't waste time complaining.
That is the reason, why we act differently!

Saturday, January 7, 2012

Laser pointers and video games

Hi again,
in the last week i had a couple of interesting discussions about laser pointers. Since most of these discussions were with video game fans, this is what this post will be about.
In many first person shooters, you are able to mount a laser pointer on your weapon. They then create a red beam of light which helps you aim.
Now one game that allows this is the FPS Crysis. A youtube walkthrougher named Majorslackvideos (if you like video games, be sure to check out his channel) mentioned, that in real life, enemies would be able to see your laser pointer. That is true for real life red laser pointers, but these would be incredibly hard to see, since they do not create a beam in the air at such low power. Anyway, you could use them with a scope to get a red dot like sight.
Then he mentioned infrared and this is in fact a way to get a game-like laser pointer. Infrared light is invisible to the human eye (or can you see your tv remote), but extremely bright to night vision devices since they rely on the infrared radiation of the environment as image source.
In Crysis, you play a supersoldier with a highly developed nanosuit and one of the implemented features is in fact a night vision device.
With a infrared laser, you would create a very bright point on the image. Now use modern image
processing techniques and overlay the brightest point on the image with the live-action feed. Voila, you've got a laser pointer thats only visible to you and night vision viewers. If you mount a second bright IR light source on your weapon and now draw a line between the two brightest points then you get exactly the look and feel from the game.
So why did we talk about this?
Well, using the laser pointer gives perfect aim to every weapon in the game and this is not fair.
So for future use, we agreed on only using the laser pointer in Nightvision mode, which decreases the level of detail you are able to see. A highly scientific experiment later, we were quite happy with this solution. Now we only need to write a mod that auto-switches to night-vision mode, if you use the laser pointer.
I hoped you liked this post.
Apexys

Thursday, September 1, 2011

ASCS PART 1: A simple computing system

Hi!

Computers are one of the most important parts of our every day life.
Not only that we actively use them to do things like communicate, entertain,
educate or blog, as I am currently doing, they also control the transport of goods,
they help emergency teams to rescue people in danger and do a lot more.

But what would happen, if, someday in the future, our economy will break?
No more cheap parts from China, no more cheap microprocessors, no more cheap computers.
We would definitely have a really big problem.

But not only the possible breakdown of the economic system is a reason for me to do this project.
I also want to learn, how a processor REALLY works.
Yes, it uses logic gates, it uses a synchronous design and so on.
But how would I build a computer, if I were to do so my self?

This is, what this series of blogs will be about.
It is definitely planned as a long-time project,
so I will interrupt the series for updates on other projects.

So, what will this first blog be about?
Well, we need to define, what the computer will be able to do.
I will restrict this on only the instructions and the main build.

So, what will our system look like?
Mostly like this:


















Simple and easy.

So, now we just need the mentioned 16 instructions mentioned in the ALU.
Guess what the next blog will be about.



Thursday, August 11, 2011

A simple 3x5 binary font generator

Hi!

In my LED-matrix-project, I needed some kind of possibility to display text.
Since the flash memory of an AVR, especially from the ATTiny series, is somewhat limited,
I had to find a good way to store the characters without using up to much of the space.
After a small discussion on www.mikrocontroller.net, I had everything I needed to make it.
 Well, everything but the font.
As recommended, I'm using a 16-bit mono-color font, which means, I'm storing all the information needed in just two bytes. This makes it a bit hard to understand by just looking at the binary, so I wrote a small program
to generate the chars for me.

Using this and a routine from the forum-user "Peter Dannegger", I can now display the characters on the matrix. Here is the function, which is meant to be used together with the code I posted earlier:

void writeSign(uint8_t px, uint8_t py, uint16_t pattern, uint8_t color){
   for (int x = 0; x < 3; x++) {
      for (int y = 0; y < 5; y++) {
         if (pattern & 1) // LSB first
            ht1632_plot(px +x, py +y, color);
         else
            ht1632_plot(px +x, py +y, BLACK);  
         pattern >>= 1; // next bit
      }
   }
}



You can download the program to generate the 16-bit fonts here:
http://sourceforge.net/projects/binaryfontmaker/

Apexys

Wednesday, August 10, 2011

DE-DP14211 LED-matrix

Hi!
I've been looking quite a while for cheap LED matrices now and I finally found some.
SureElectronics has some really good ones, in all variants you could want.
Finally, I bought two DE-DP14211 matrices.
Featuring a 32x16 tricolor LED matrix, 4-bit PWM dimming and a SPI-like interface, they only cost about 25€ per item.





Well, I bought them without reading the datasheet, because what was named SPI-like was different enough from normal SPI, that you couldn't use the AVRs hardware SPI to control these devices.

After googling the misery, I found an Arduino library that could control the matrix.
Now, since I'm not using the Arduino bootloader, I had to change a few things.

First of all, you need to pick the functions, you need.
In my case, I only wanted the parts to be able to set or delete a pixel.
So what did I need?

First of all, go through the compiler definitions.
We won't use the DEBUGPRINT function, so don't copy it.
Here is what you need:


/*
* commands written to the chip consist of a 3 bit "ID", followed by
* either 9 bits of "Command code" or 7 bits of address + 4 bits of data.
*/
#define HT1632_ID_CMD 4 /* ID = 100 - Commands */
#define HT1632_ID_RD 6 /* ID = 110 - Read RAM */
#define HT1632_ID_WR 5 /* ID = 101 - Write RAM */


#define HT1632_CMD_SYSDIS 0x00 /* CMD= 0000-0000-x Turn off oscil */
#define HT1632_CMD_SYSON 0x01 /* CMD= 0000-0001-x Enable system oscil */
#define HT1632_CMD_LEDOFF 0x02 /* CMD= 0000-0010-x LED duty cycle gen off */
#define HT1632_CMD_LEDON 0x03 /* CMD= 0000-0011-x LEDs ON */
#define HT1632_CMD_BLOFF 0x08 /* CMD= 0000-1000-x Blink ON */
#define HT1632_CMD_BLON 0x09 /* CMD= 0000-1001-x Blink Off */
#define HT1632_CMD_SLVMD 0x10 /* CMD= 0001-0xxx-x Slave Mode */
#define HT1632_CMD_MSTMD 0x18 /* CMD= 0001-10xx-x Use on-chip clock */
#define HT1632_CMD_EXTCLK 0x1C /* CMD= 0001-11xx-x Use external clock */
#define HT1632_CMD_COMS00 0x20 /* CMD= 0010-ABxx-x commons options */
#define HT1632_CMD_COMS01 0x24 /* CMD= 0010-ABxx-x commons options */
#define HT1632_CMD_COMS10 0x28 /* CMD= 0010-ABxx-x commons options */
#define HT1632_CMD_COMS11 0x2C /* CMD= 0010-ABxx-x commons options */
#define HT1632_CMD_PWM 0xA0 /* CMD= 101x-PPPP-x PWM duty cycle */


// Dual 3216 LED Matrix
// If you have only one set these to:
// X_MAX=31
// Y_MAX=15
// CHIP_MAX=4

#define X_MAX 63 // 0 based X
#define Y_MAX 15 // 0 based Y
#define CHIP_MAX 4*2 // Number of HT1632C Chips
// 4 each board * 2 boards

// possible values for a pixel;
#define BLACK 0
#define GREEN 1
#define RED 2
#define ORANGE 3 


#define calcBit(y) (8>>(y&3)) 






 Next is the variable definition:
  /*
* Set these constants to the values of the pins connected to the SureElectronics Module
*/

static const byte ht1632_data = 53; // Data pin (pin 7)
static const byte ht1632_clk = 52; // Data pin (pin 2)
static const byte ht1632_wrclk = 51; // Write clock pin (pin 5)
static const byte ht1632_cs = 50; // Chip Select (pin 1)
// The should be a common GND.
// The module with all LEDs like draws about 200mA,
// which makes it PROBABLY powerable via Arduino +5V

//(Apexys comment: The module will draw up to 1.5 Amps with all LEDs on,
// so forget about powering it with an Arduino and get it a good 
// switching PSU!) 

/*
* we keep a copy of the display controller contents so that we can
* know which bits are on without having to (slowly) read the device.
* Note that we only use the low four bits of the shadow ram, since
* we're shadowing 4-bit memory. This makes things faster, but we
* COULD do something with the other half of our bytes !
*/

byte ht1632_shadowram[63][CHIP_MAX] = {0};


Followed by the functions

 void OutputCLK_Pulse(void) //Output a clock pulse
void ht1632_chipselect(int select) //Select a chip
void ht1632_writebits (byte bits, byte firstbit) //Write bits on SPI
static void ht1632_sendcmd (byte chipNo, byte command) Send Command
static void ht1632_senddata (byte chipNo, byte address, byte data) //Data
void ht1632_clear() //Clear Display 
byte xyToIndex(byte x, byte y) //Get RAM position for a pixel
 int get_pixel(byte x, byte y) //Get a pixel's color at a position 
 void ht1632_plot (byte x, byte y, byte color) //Set a pixel
static void ht1632_initialize() //Init the display 


Now, simply replace byte with uint8_t.
Two last things are to do: You need to define four pins of any port as output and you need  a function to replace the digitalWrite function of the Arduino library. Here it is:

//Replace the Arduinos digitalWrite() function
void digitalWrite(uint8_t port, uint8_t value){
switch(port){
case 0:{
if(value){
PORTC |= (1<<PC0);
}
else{
PORTC &= ~(1<<PC0);
}
}
break;
case 1:{
if(value){
PORTC |= (1<<PC1);
}
else{
PORTC &= ~(1<<PC1);
}
}
break;
case 2:{
if(value){
PORTC |= (1<<PC2);
}
else{
PORTC &= ~(1<<PC2);
}
}
break;
case 3:{
if(value){
PORTC |= (1<<PC3);
}
else{
PORTC &= ~(1<<PC3);
}
}
break;
}
}


Now simply change the static const byte for the port values to something like

#define ht1632_cs 0
#define ht1632_clk 1
#define ht1632_wrclk 2
#define ht1632_data 3


And add a statement like


#define LOW 0
#define HIGH 1 


And your done!

Now you can set a pixel with ht1632_plot(x,y,color) and you can clear the matrix by calling
ht1632_clear().

Now, back to development!

Apexys 

Sunday, May 15, 2011

The iron sulfide thermite

Hi!
This is the second one of my promised series of blog post!
It's about a little experiment I did some weeks ago.
I wanted to cover iron stick with liquid iron sulfide,
so I could bring a nice layer of sulfur on some copper in an electrolysis.
To have some fun, I added the thermite process to the sulfurisation.
Here is how it looked like:


Well, quite impressive.
I didn't use the iron sulfide, but gave it to my friend C3H5N3O9 so he could make his copper-sulfur-plate.
I hope you liked this non-electronics-related experiment.

Apexys