Transfer Files From Phone to PC Easily

I have a Pixel phone that for some reason always disconnects when I try to transfer files over USB to my laptop. it is almost full of pics and videos since I can’t really transfer them off. I finally filled the phone up and had to make space, so I figured out the method I’m going to use from now on to transfer files on and off my phone.

I set my phone up to be a simple little WebDAV server. This allows me to very easily map it as a network drive in windows and transfer files at will.

I downloaded HTTP File Server (+WebDAV ) on my phone. Then I opened it and clicked the “Start button to start the server. Obviously you have to give this app file permissions for it to work.

Then on my PC, I mapped the drive by right-clicking the “This PC” and selected “Map Network Drive:

 

Then type in the address from the HTTP Server app.

Once this is done you can access the file from the phone via whatever drive letter you selected when you mapped it. In the above image the drive is the Y:/

 

Using MPlabX to Program Arduino Boards in Straight C/C++

I wanted to use MplabX to replace the graphical front-end of the Arduino app and write my own bare metal C/C++ code, but use the arduino’s built-in USB programming function. The method below can be done in multiple different IDEs, but I show how to set up MplabX. It is important to note that this method expects the Arduino’s bootloader to be stay on the board.

1. Setting up the Path to the Arduino IDE’s Compiler:

These are different for every computer so you have to find it yourself. To do this, open the Arduino IDE , go to File–>Preferences and click “Show Verbose Output” for both Compile and Download.

Next, plug in a board and send an example project to your Arduino (such as Blink.ino). The command line will spill over with commands.

Scroll to the top of the command window in the ArduinoIDE and look for the line after it says “Compiling Sketch”:

Copy this entire line and paste it to a text file so we can edit it. (Double-click this line to highlight it to copy). Here’s mine:

"C:\\Users\\adamp\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Users\\adamp\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\adamp\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\eightanaloginputs" "C:\\Users\\adamp\\AppData\\Local\\Temp\\arduino_build_752190\\sketch\\Blink.ino.cpp" -o "C:\\Users\\adamp\\AppData\\Local\\Temp\\arduino_build_752190\\sketch\\Blink.ino.cpp.o"

 

The highlighted portion above is the file path to the C++ compiler Arduino uses. If we want to write our own bare-bones C or C++ code for the arduino, this is all we need.  Open MPlabX and select “Tools–>Preferences.  In the popup, select the “Embedded” tab.  On the left side, click the Add… button to add a new toolchain. A new popup will appear where you can paste the “Base Directory”. Paste in the highlighted part of your path only up to but NOT including the “avr-g++” part.  This will allow MPlabx to use the AVR-G++ compiler for Atmel chips.

2. Creating a MplabX project:

In MpLabx, start a new standalone project. I’m using an Arduino nano so I will choose the chip “Atmega328P” I recommend you select “Simulator” for the “Tool” entry here as it can be really handy for debugging code.

Then select the new entry “AVR-GCC” for the toolchain.

3. Set up the Programming Tool in MPlabX:

Now to take advantage of the built-in USB programming of an arduino board we need to set up the correct commands. In the Arduino IDE scroll down in the command window and find the last line of white text before the red text begins. The red text is the output from the programmer app (called avrdude.exe).  The line just before this is the command that calls avrdude with the appropriate settings.

 

C:\Users\adamp\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude -CC:\Users\adamp\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf -v -patmega328p -carduino -PCOM7 -b115200 -D -Uflash:w:C:\Users\adamp\AppData\Local\Temp\arduino_build_752190/Blink.ino.hex:i

Scroll to the rightmost of the command below to see the highlighted text in the line above. This is the path to the actual hex file the programmer is sending. This changes based on where you name the MPlabx project. You need to change this file path to the following to work with any MPlabX project:

-Uflash:w:${ImagePath}

Once you make this change, simply copy this entire command and paste it into the MplabX project properties.

In Mplabx, right-click the project you want to work on, select Properties, then select “Building” from the left sidebar in the popup window. Make sure to click “Execute this line after build” and paste the entire command from above in the blank.

***BIG NOTE: If you change the port number of the board you are programming (plugging in a different board, or using this same project on a different computer) you MUST change the COM port in this option in the project or it will fail to program and give a cryptic error.

4. Testing with a Blink project:

If you have set everything up correctly, then you can right-click your project and select New–>main.c

Replace the code in this file with the following:

#define F_CPU 16000000UL //You must define the clock frequency before you can use the delay function. This should match the crystal on your board.

#include <stdio.h>
#include <stdlib.h>
#include <util/delay.h> //must use this to use the _delay_ms() function
#include <avr/io.h> //Must use this to access port and pin numbers on our chip

/*
*
*/
int main(int argc, char** argv) {

       //this is where your setup() function goes
       DDRB |= (1 << PORTB5); //Make Arduino Pin13 (AVR PortD Pin5)) and output pin

      while (1) {//this is your "loop" function
           PORTB ^= (1 << PORTB5); //Toggle the value of this pin using an XOR function with itself
          _delay_ms(100); //Change this number to change the LED flashing frequency to know if it is working or not.
        }//end while 1

       return (EXIT_SUCCESS);
}//end main

Then click the build icon and it should compile, then you should see the familiar text from the Arduino IDE. The Avrdude programming information is the red text when you turn on Verbose mode.

Troubleshooting:

If you don’t see this text, a few things might be the cause:

  1. You forgot to click the “Execute after build” checkbox in the build properties
  2. You have the wrong COM port or U:flash setting in the line you want it to execute after building.
  3. Make sure you set your project up with the correct chip and correct toolchain (AVR-GCC)
  4. You have a compilation errors. This can be hard to see, but you can make it easier

If your Arduino isn’t doing what you want it to, then you likely need to debug your code. There’s a LOT to be said with this. The easiest method is to set up a UART library so you can print text out the serial port to your computer. Search github and AVR freaks for examples you can import.

Another method is to use the Simulator in MPlabx to see what bits are being set/changed in the memory location of the Atmega chip.

A third option allows you to run the code line by line on the actual chip. This would require a proper programmer (not just the arduino’s built-in USB programming).  Here’s an example of how to set that up.

Now you can learn to write your own libraries to better understand the magic of the Arduino project. Learn some of the tricks that are used to simplify the interface for programming on this site.

 

 

Teenage Engineering PO-80 or Gakken Record Factory 78RPM Hack

Recently, Teenage Engineering released the PO-80 Record Factory, which is a rebranding of the Japanese Gakken Record Maker kit.  It is a really neat record player that can also cut custom records.  One problem I had with it is that it only plays 33RPM and 45RPM, but we had a lot of 7″ 78s I wanted to play on it, so I fixed it.

Most cheap record players use the same 5-pin motor module which is a DC motor with a speed controller built in. This particular one uses the EG530SD-3F.  By wiring  3 of the pins in different ways, it will automatically play at constant speeds of 33RPM, 45, RPM or 78RPM. The 33 and 45 are basically the two built-in speeds, which is what this kit uses.  To add the functionality of 78RPM, you can add a resistor to the circuit.

There wasn’t a detailed datasheet explaining what’s inside the motor, but rather many listings for this motor had the following instructions.

Adjusting Method:

  • When the changeover switch reaches 33, use a small flat-blade screwdriver to adjust the resistance of the L hole of the motor, and the normal disc is 33 revolutions per minute.
  • When the changeover switch reaches 45, use a small flat-blade screwdriver to adjust the resistance of the H hole of the motor, and the normal disc is 45 revolutions per minute.
  • When the transfer switch reaches 78, use a small flat-blade screwdriver to adjust the resistance on the PCBS board. The normal disc is 78 revolutions per minute.

Luckily I found someone online who had taken one apart and described the controller circuit. This, along with the “official documentation” to get in the resistance range was all that was needed to make the simple hack.  The official info is as follows:

For this hack, I needed  a way to add in the resistance for when I wanted to play 78s, but I had to be able to completely remove the resistance to use the 33 or 45 speeds. For this I just found a JST connector, but any female wire connector would work. I soldered the wires of the JST connector to the motor based on the link above. I made sure my wires were long enough to reach out the side the connector for the record cutter extended to.

For the resistor, the only info I could find online were several schematics showing the use of a 300Ω potentiometer so you can adjust it, however, I only had a 5k so I threw a 1kΩ in parallel with it to get me somewhat within the range.  The actual resistance needed is around 160Ω but it is nice to have the knob for adjustment.  I intentionally left the resistor leads long so I could use them to plug into the JST connector. Notice that for this, you only need one side and the center lead from the potentiometer.

The final result looks pretty clean. It’ll look cleaner when I throw some heatshrink on the potentiometer. Again, this is only used for playing 78s so when playing the other speeds I disconnect the potentiometer and store it in the box the record player came in. Check the video out below.

COM Port Name Pop-up

When working with microcontrollers, especially helping students debug their work, I hate having to open device manager or a serial terminal to search for the serial port name of the device I most recently plugged in.  I wrote a small python script to hook into Window’s notification system to know when a new COM port has been plugged in, and then display its name in a popup window.

The first step in this was to find out when something is plugged in.  I tried all sorts of things with windows event viewer to see if I could detect this because I didn’t want to have yet another service running in the background on my PC eating resources all the time, and I wanted the reaction to be fast, so polling every 10 seconds or something would be too long to wait for a popup.  Alas, I couldn’t find any way to get USB port data to trigger an event in windows when you plug in a USB port.

Looking online for examples I only seemed to find C# or VS code.  I’m not installing anything more to develop this one project, and at first I wanted to see if there was a cross-platform solution since I was going to have to write it all from scratch.  Eventually I settled on a windows-only solution because I found an excellent site that is full of good code examples for win32 python interfacing from a user’s post on stackoverflow. these kinds of sites are few and far between, so I’m glad I came across it!  This code example connects into Windows Notification center and WMI (Windows Management Interface) to detect when USB items are plugged in. You can detect drives, networking, and serial ports based on the example in the link.

Once this info is gathered, the next step is to create a popup with the COM port name. For this I came across an example using the plyer library for python which is dead-simple.

When you run python code, by default, the command window is always visible, so I tried a few things to hide it. Firstly, you should be able to just use pythonw.exe instead of python to run the code and it should hide the window. For some reason, this didn’t always work. The next thing I tried was using the win32 library in python. This grabs the foreground window and hides it. Since the foreground window is likely this python script, it works fine. Running it sometimes takes a faction of a second for it to hide the window so sometimes you see it flash a command window.

The final step is to have this app start when you turn on the computer. Some folks said you could just put your python file into the windows startup folder, but this never worked for me. Instead, I keep the python script in my installations folder (where I keep all my useful apps) and I wrote a batch script to run the python code. I saved the batch script into my startup folder and rebooted.  It works great and doesn’t use many resources at all!

Get my python code and batch script here.  If you use it, let me know in a comment.

Now,  if you use linux or mac, I recommend you check out usb-ser-mon which has some of the same functionality of detecting plugged-in devices. Then you can use the same plyer code to generate a popup from what I understand.

Making Procedurally Generating Spalting Patterns

I did this a while back, but someone had an interest in this on hackaday so I tried to dig up my old code.

The goal is to generate wood grain patterns for laser cutting/CNC of plywood. Mainly, I wanted to have something similar to spalted maple laser etched onto the plain birch veneered plywood. The thing that inspired this project was this post of speaker boxes on imgur I came across. The design looked really cool, and while spalted maple is expensive, birch plywood is relatively cheap.

My first step was to procedurally create a random pattern.  Similar to the imgur post, I looked at creating camouflage. I found a great example code on openprocessing by ThingOnItsOwn that used perlin noise to create a camouflage pattern. I tweaked the values a bit experimentally, then stretched the entire design to make the final design look more like woodgrain.

The next step was to just capture the edges of these blob shapes from the first pattern. I came across this example from Richard Bourne It is forked from this example from R. Luke DuBois.   Honestly I was being lazy because I had written edge detection code in college as it is standard image processing, but I knew someone else had it already in processing. Instead of using for loops, this version manually calculates out the kernel.

This leaves me with a result that looks pretty realistic.

Camo:

Spalting:

This creates a PNG filetype which can be used to add texture to a 3D print in the slicer. The slicer will adjust the print to incorporate the texture in 3D giving it a woodgrain-like effect. You technically could use this as-is on a laser cutter to create spalting like my inspiration, however being raster data, it would take the laser a long time. It’d have to scan the laser (the thickness of a human hair) across the entire area of the panel you are applying the texture on. To make this faster, you can vectorize the PNG in inkscape or other software to outline the dark areas of the PNG.  This will cut fast as it is a vector (the laser would just have to draw the lines the same way your hand would. That would save a lot of time.

If anyone wants to add vectorizing to my code, please do! You can clone my github and put it on P5.js. I was going to add it, but I got lazy again. I even asked ChatGPT3 to help combine this code with something like potrace or imagetracerjs but it produced code that looked great, some even compiled after a few tweaks, but never worked.