Wednesday, October 19, 2016

(Almost) Free PIC Programmer (with limitations)

Microchip has made a $10 PIC board to showcase their online IDE, MPLAB Xpress. The board is called the... MPLAB Xpress Board. (Consequence: unhelpful search results!) How do you make it so an online IDE can program your PIC part? Make the programmer look like a USB mass storage device. Now you can (theoretically) use it on any computer the Internet.

I was lucky and was able to snag one of these board for free! They gave away 2000 of them. Free? I can't pass that up. I had never used PIC and never had any real interest. (8-bit? Pffft! But they do have DIP packages...) Did that stop me from getting something for free? No. No it did not. Well after playing with it and looking into all the neat peripherals that PIC parts have, I decided to make this board more useful. I would have put some pin headers on to be able to separate the programmer from the on-board target device but they didn't ask for my input (weird, right?). But they did put some breakout pads on the back of the board (which are pointed out in that linked-to Hackaday article). Obviously we can't have the programmer trying to program two chips at once. So I took the matter of cutting traces into my own knife-wielding hands. I just need to cut right here...

Oops. Wrong spot. (I may get a little impatient when I'm excited to try out an idea.) Actually right there would be better.



Too late now. Two of those five pads are disconnected because I didn't think twice. Luckily some current-limiting resistors provided a nice alternative place to solder some wires to. Crisis averted. A little hot glue to give some mechanical support to my wires and now I have a free PIC programmer!

The firmware for the programmer side is available on GitHub. Here's one drawback listed in the README: "The programming algorithm is currently supporting only the new 8-bit LVP-ICSP protocol common to the PIC16F188xx (5 digit) devices. It is also assuming a fixed row size of 32 words." So... that's a pretty small subset of devices you can program with this. (I count 7 parts that are PIC16F188xx. You want me to read datasheets to figure out if there's more? No thanks. (Okay. I looked at the PIC16F18325/45's datasheet. I'd guess it's compatible based on row size. But no more!)) But I got it for free so I'm not going to complain. And even if they update the firmware source, how am I supposed to update my programmer? (A PICKit 3. But if I had that...)

I even tried it out on a DIP packaged PIC16F18855. It works great! The programmer also has the necessary pull-up resistor for the MCLR line so you can program the chip all by itself in your breadboard.

It's not running in the picture but that LED will blink.
Those wires go to my hacked programmer.


Eventually I'm going to use my PIC16F18855 to drive a strip of WS2812B LEDs. That'll be for another post.

Other links:
MPLAB Xpress forum (for both the IDE and the board, I guess)
Board Schematic

Saturday, January 23, 2016

Running Asterisk with Google Voice on your Synology Diskstation

I run a Synology Diskstation network-attached storage (NAS) at home (specifically a model DS114). I use it primarily for backups but it is really a little server so you can do almost anything on it. This is especially true if you run packages written by the community (though Synology publishes very good ones, too). I run CrashPlan on it so things I store on it are backed up to "the cloud" automatically (see here and here for instructions). I've plugged in a cheap-o USB audio adapter, connected it to my stereo, and can run Shairport on it (originally I ran this but now run the SynoCommunity package). This lets me easily stream audio from my computer to my stereo (using TuneBlade). I also connect my USB printer to it so I can print wirelessly.

One thing I've often wanted to run on it was Asterisk. Asterisk is a program that manages telephone lines and services (especially VoIP lines). Synology officially provides an Asterisk package that even includes a web GUI (though Digium no longer recommends using it). The problem with this package, however, is that they don't include the chan_motif module, which is necessary for running Google Voice lines with it.

While ambitious me usually doesn't stick around too long when he shows up, he was around long enough to try to take the Asterisk source and Synology toolchains from the Synology Open Source Project and rebuild their package from source, this time including chan_motif. I remember getting stuck on building some dependencies for my platform and ended up stopping there.

Months later I decide to revisit this problem. Looking through SynoCommunity's repository for their spksrc tool, I was thinking that maybe this would allow me to more easily create my own Asterisk package. Further looking led me to find their basic instructions for using Debian Chroot (I liked these extended instructions, too). Oh, my. What is this?! It's running a second, parallel OS on your Diskstation. (I've run Optware on a PogoPlug v2 and on my Asus Router that had DD-WRT at the time (now Tomato) and I hated it. I can't remember why but I remember thinking it was such a pain in the neck. I have had zero desire to run it on my Diskstation from day one. That PogoPlug, by the way, lasted less than 18 months before I chucked it in the garbage. I don't miss it. Arch Linux. U-Boot. It all made me want to poke my eyeballs out. That was a long parenthetical.)

So?

Well Debian offers their own package of Asterisk that does include the chan_motif module! No rebuilding from source necessary. (Just a simple 'sudo apt-get install asterisk' is all you neeed.) Now we're cooking with induction.

Now I've seen how things like this go and I am telling you right now that I will not become the go-to guy for support when you want to get Google Voice on Asterisk going on your Diskstation. This is what worked for me in early 2016. May this page go into the rubbish bin of irrelevance and be deemed out-dated not far into the future. I don't care! I have a life to live here, people. (Okay. Okay. I think they get it. Lay off on the attitude a little. (Phrases like "lay off on" make me love English.))

If you want instructions for getting Google Voice going on your Asterisk installation, you can start with Digium's wiki but I would recommend the excellent book Asterisk: The Definitive Guide, 4th Edition which covers it in Chapter 18 (beg your library to get it or if you have a Safari subscription, it's there, too). It's a good book for all things Asterisk.


Wednesday, January 13, 2016

Printing Every Powerball Lottery Number

I think gambling is fascinating. I think it's tempting and I abstain from it. I think it's morally wrong. Value comes from work. You don't get something for nothing in life (there's always a loss to someone).

Tonight the Powerball will play for a world record \$1.5 billion. Billion! That's insane. The odds are also insane. Powerball is played by choosing 5 of 69 numbers and then choosing 1 of 26 "powerballs." How many ways can the Powerball numbers be chosen? That's easy:

\begin{equation}
\binom{69}{5} \binom{26}{1}
\end{equation}

which is:

\begin{equation}
\frac{69!}{5!(69-5)!} \cdot \frac{26!}{1!(26-1)!}
\end{equation}

which is 292,201,338. That's a very big number. I know 1.5 billion is 5 times bigger but it's still a big number. Well let's consider the title scenario of printing out every single Powerball combination possible. Just what would that look like?

When I made a quick test print of different font sizes from 1.5 to 5.5 in half-point increments, I thought 3.5-point Courier New made for a nice size. That's a little more than 1 mm in text height. Making some adjustments to line spacing, page margins, and using 15 columns allowed me to put 2970 Powerball combos on a sheet of paper. If we print double sided we get 5940 combos per sheet of paper (enough combos for 57 years of twice-weekly playing). That means we'll need 49193 sheets of paper or 99 reams of paper! That weighs in at 492 lb. After toner is applied it's sure to weigh more than a quarter of a ton (sorry planet earth). Between me and the paper, I'm pushing the limit of what my Toyota Yaris is rated to carry. (Despite my wife's objection to having 10 cases of useless paper sitting around our house, I think it would be cool to have a copy.)

Walter Hickey wrote an excellent analysis for Business Insider of the expected value of a lottery ticket (written in Sept. 2013). He takes into account the possibility of sharing the jackpot (because more people buy tickets as the jackpot goes up in value) and the loss in value due to taking a lump sum. He does not factor in taxes, though. The short answer is that after taxes, it's never worth playing the lottery. This is no surprise.

The jackpot didn't get to \$1.5 billion by magic. It gets that high because people lose! That \$1.5 billion represents hundreds of millions of \$2 tickets. More tickets than there are combos! The odds truly are terrible. People will often say, "You have to play to win." I would say that the odds are so bad that not playing is as good as winning \$2 (or more!) twice a week for the rest of your life.

Here's a fun simulator (play as much as you want!): http://graphics.latimes.com/powerball-simulator/

Monday, January 4, 2016

My Not-quite White Space Radio System

2016 will be Merrick's Year of Communications. My goal is to write a blog post once a week. What will I write about? Communications! My previous post mentioned this. Now it's time to talk about the hardware I'll be using.

Communications is a lot more fun when it's wireless. (Consider "tied down," "strapped to my desk," and "the old ball and chain." Not fun.) But as much fun as wireless is, how can I perform wireless transmissions and receptions from my home computer with full control over the transmitted signal? Well a software-defined radio (SDR) would work perfectly for that.

Let's see. I'll just do a quick check here... Oh, I only need to spend nearly $700? No! Just over $400! Oh, wait. Here's one for about $300. Those are all awesome SDRs if you want to transmit. So what's the problem? I'm cheap. I know computers cost that much and I'll be using two of those but permission to purchase a $300 radio hasn't been granted by my local approving authority (i.e. wife).

How cheap? Like $3-eBay-FM-transmitter-direct-from-China cheap. At least for the receivers I'll be using some of those cool RTL-SDR receivers (that often cost less than $10).

Now I'm not dumb and I know that I'm really not going to be doing any modulation of the RF signal by using an FM transmitter. I'm not likely to get more than 10 kHz of bandwidth in the audio spectrum to play around in. So from a cost/bandwidth perspective, these $3 radios don't really match the SDRs I linked to above by an order of magnitude or so. But they're $3! Feeding myself today will cost more than that.

I'd get more bandwidth by just connecting the two computers with audio cables instead of going through the FM transmitter. But, again, it's wireless. And audio cables aren't that interesting. By using the FM transmitter I can make my channel model more noisy interesting.

The FM transmitters are the same ones you'd use to play audio from your phone to your car stereo. They operate under "47 CFR (Code of Federal Regulations) Section 15.239, and the July 24, 1991 Public Notice (still in effect)." They are supposed to have no more range than approximately 200 ft (for $3, I'm hoping for 20 ft). But I can operate them with no restrictions to content (e.g. encrypted) or broadcast duration.

I call this a "not-quite white space radio system" in the title of this post. If I use my FM transmitter in an unoccupied channel on the radio, I'm effectively using a "white space." Unlike actual TV band white space allocations, however, the FM band isn't allocated for white space radio by the FCC (probably because no one wants that space and no one is asking the FCC to do so).

Now I just have to wait for the slow boat from China to make it's way to American shores.


Monday, December 28, 2015

Installing VirtualBox Guest Additions in Fedora 23

It's been a while since I've done anything with digital signal processing (DSP) and I never ended up taking a formal digital communications course in college. So over the next year I have some plans to try out GNU Radio with some very inexpensive radio hardware (on the order of dollars to tens of dollars) to try building some simple digital communications lessons.

I am primarily a Windows user and GNU Radio is primarily a Linux tool. (There are instructions for building GNU Radio to run in Windows but today I'm not going to go there.) We'll discuss my hardware plans later but for now I'll say that primarily we'll be using the audio interface of the computer to move signals around. USB may get involved, too, but it will be secondary. With that in mind, I figure it's no problem if we use a virtual machine. I've decided to run Fedora 23 in VirtualBox.

I'm kind of a novice when it comes to managing Linux machines but that doesn't mean I can't do it. I wanted VirutalBox's Guest Additions installed to my x64 Fedora image but it failed when I tried to install it. After a little Binging (that will never sound as good as Googling) and sorting through my command history, here are the commands I ran to get things into a state where I could install Guest Additions.

  sudo dnf update
  sudo dnf install gcc
  sudo dnf install kernel-devel.x86_64
  sudo dnf install dkms
  sudo dnf install kernel-headers
  sudo dnf install gcc-c++
  sudo dnf update kernel
  reboot
  # now install Guest Additions. Allow the "CD" to auto-run. Reboot again, I think.

With Guest Additions installed, the virtual machine now displays at the full resolution of my monitor, among other benefits.

Another problem: By default Fedora puts its logo in the bottom corner of your desktop. This eats up a surprising number of CPU cycles running in VirtualBox. Install gnome-tweak-tool to remove it.

  sudo dnf install gnome-tweak-tool
  gnome-tweak-tool 

Then disable the background logo in the tweak tool (answer originally found here).

Now we can install GNU Radio and run the GNU Radio Companion.

  sudo dnf install gnuradio
  grc

Right now I am having issues with audio buffer underruns. I'm thinking I may have to move on to a live image or actually try to build GNU Radio for Windows. *Sigh.* At least my virtual machine fills my monitor...

I fixed my buffer underrun problem. I made sure the virtual machine's audio controller was ICH AC97 in the VirtualBox settings. Then in Fedora I opened up a terminal and typed 'aplay -L' to list all of the available audio devices. One is listed as:

front:CARD=I82801AAICH,DEV=0
    Intel 82801AA-ICH, Intel 82801AA-ICH
    Front speakers

That first line is what I typed into my GRC audio sink as "Device Name" parameter. With the sample rate at 44.1kHz I get no underrun errors (a repeating aUaUaUaUaU in GRC's console output). There's some clicks at the start of playback but that's something we can work with. Hooray!

Thursday, December 24, 2015

Expected Value of a Roll in Farkle

We like to play Farkle in my family (well... I like to). It's a game played with 6 standard dice and the goal is to be the first to get more than 10,000 points. If multiple people have more than 10,000 points after N turns, then the person with the max points wins. We have Farkle Party and play by its scoring rules. A question I often ask myself is, "What should I do next?"

Maybe a little more on how the game is played would help. When it's your turn you roll all six dice. Each time you roll dice you must increase the number of points earned during your turn. If your roll has no dice that add to your score, you rolled a "Farkle" and you get 0 points for your turn and your turn is over. You may end your turn at any time to "bank" the points you've earned that turn. The 10,000 point end goal is for banked points. The leftover dice that don't add to your score after a roll may be re-rolled to continue your turn. A single 1 is worth 100 points. A single 5 is worth 50 points. There are more points awarded for more difficult combinations, such as three-or-more-of-a-kind, that vary depending on the rules you've set. The Wikipedia article linked to above covers many of those variations.

An example. It's your turn and you roll 1-1-2-3-4-6. You may take the two 1s for 200 points or you may just take one of the 1s for 100 points. The rest are worth nothing. You may then roll the remaining four or five dice, respectively, to earn more points. Again, with each roll you must increase your score or your turn ends with 0 points. So say I take 200 points and roll the remaining four and get 2-2-3-6. That's worth 0. I didn't increase my turn's score of 200 with that roll so I get 0 that turn and hand the dice to the next player.

This example was one where I would ask myself if it is better to take 100 points and roll 5 dice or take 200 points and roll 4. Which choice will maximize my expected score?

There's lots of factors that go into answering that question. Am I behind? Or do I hold a comfortable lead over my opponents?  I won't go into that strategy today. For now I will talk about the expected value of a roll.

Let's change the example we've been using. Say I take 200 points, roll four dice, and get 6-6-6-2. Three sixes is worth 600 points. I take those points and am left with one die and 800 points for my turn. What is the expected value of rolling one die? I have a 1/6 chance of getting 100, a 1/6 chance of getting 50, and a 4/6 chance of getting something else and losing my 800 points. So my expected value is $\frac{1}{6}100 + \frac{1}{6}50 - \frac{4}{6}800$ which is $-508.33$ points. I should stop, right? Well it's not exactly that. If I get a 1 or a 5 then I can start over and roll all six dice again to continue building my score. So now the expected value is
\begin{equation}
E[d_1] = \frac{1}{6}(100+E[d_6]) + \frac{1}{6}(50+E[d_6]) - \frac{4}{6}800
\end{equation}
where $E[d_i]$ is the expected value of rolling $i$ dice.

Over several lunch breaks I figured out the expected values for all six $E[d_i]$ in terms of each other. The hard part was calculating the number of ways you can roll different scoring combinations with $i$ dice. Hmm... six equations and six unknowns... that's a system of linear equations! We can solve that.

But wait. Don't we have seven unknowns? We assumed 800 was our current score when that's also an unknown. True. We don't know what our score is or will be when we get to roll $i$ dice. But for now let's calculate our expected values assuming our current score is 0. Then we can calculate them again over many possible values for our current score later.

If we solve this system of linear equations, we find the following expected values:

$E[d_1] = 280$
$E[d_2] = 260$
$E[d_3] = 304$
$E[d_4] = 390$
$E[d_5] = 520$
$E[d_6] = 700$

Interesting that rolling two dice gives the lowest expected value rather than rolling one. I won't go into detail but I'll just say that if we assume our current score is 50 or 150 or $C$ then all of these expected values go down by 50, 150, or $C$.

So if we have 800 points and one die to roll, our expected value for that roll is -520. That means I should stop, right? Or if I go back one roll in the example I can take 100 and have an expected value of 420 or I can take 200 and my expected value of 190. That means I should take 100 and roll five dice, right? I'll weasel out of this by saying it depends.

Like I said earlier, decisions don't just depend on expected value but on what the game situation is. Am I ahead by 1500 points? Behind by 500? How many turns (more or less) are left until someone breaks 10,000? But the expected value gives a gauge of your risk relative to your current score. Fun!

Tuesday, December 15, 2015

If I Could Modulate A City

I wish I could find it but long ago I was reading about some amateur radio operators performing tests in the 10 GHz band. The would drive to the tops of nearby mountains, point their equipment at each other and talk to each other to test their equipment. Despite the high frequency, they were only operating on an FM voice mode. (I don't remember if it was this site but it seems like a good candidate.) Part of me was disappointed but I also know that it's 10 freaking GHz on home-made equipment! They were doing point-to-point links, which is what you'd expect for that frequency.

Now I can't remember if the chicken or the egg came first, but recently I made the connection between high-frequency, long-distance (over the horizon), wireless communication and city light pollution.

I delved into learning photography for a while in my life. If you ever decide to take long-exposure pictures of the stars, everyone will recommend getting as far away from cities as possible. This is because the glow of the city lights can be seen in those long-exposure photos even though the city is beyond the horizon. (Just look at the glowing horizon in some of these pictures.)

Me: Wait. Light seen beyond the horizon?
Smart me: Yes.
Me: So you're saying light doesn't follow a straight path?
Smart me: That's not at all what I'm saying. Are you crazy?

The light gets beyond the horizon because of tropospheric scatter, which is the atmosphere making the light/RF energy scatter in different directions. Yes that link to the Wikipedia article on tropospheric scatter is much better than what I could explain. (People are doing this at rates of up to 20 Mbit/s?! And you need kWs of power? Awesome!) Even those 10 GHz guys know about this. But that's pretty cool, right? Communicating wirelessly beyond the horizon by making the atmosphere "glow."

If you were some sick, crazy person who happened to have the power to turn an entire city's power on and off at will, you could do it in a way that communicated information to your sick, crazy friend who lives just down the freeway a hundred miles or so.

You: So in the title you said "I." Are you that sick, crazy person?
Me: No. I did say "if."

Now that I've written about it I can stop thinking about it. I feel like Gary Clark Jr. right now when he sang, "Bright lights, big city going to my head / I don't care no / 'Cause you don't care, no"

Sing it Gary.