Tuesday, July 14, 2015

Emacs cc-mode indenting

Use cc-mode to set emacs coding styles for different languages. The configuration basics shows how to use hooks in the .emacs file. I wanted to  customize indentation and you can interactively determine which parameters to change and test different values.
   The default GNU style indents C++ as shown below.
for (unsigned i = 0; i<10; i++)
  {
    std::cout << i << std::endl;
  }
I want to indent this way
for (unsigned i = 0; i<10; i++)
{
  std::cout << i << std::endl;
}
So I added the my-c++-mode-hook at the bottom of my /home/USER/.emacs file.  I've copied the entire file to have a record of my current settings.

;;;Use font lock mode
(global-font-lock-mode 1)

;;;keep the display simple
(scroll-bar-mode 0)
(menu-bar-mode 0)
(tool-bar-mode 0)

;;;Pretty colors
(set-background-color "darkslategrey")
(set-foreground-color "lightsteelblue")
(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
  '(inhibit-startup-screen t))

(custom-set-faces
;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)

;; change the substatement indenting to 0 so opening bracket on next line is below first letter of conditional statement
(defun my-c++-mode-hook()
  (c-set-offset 'substatement-open 0))
(add-hook 'c++-mode-hook 'my-c++-mode-hook)

Wednesday, January 14, 2015

Construct a triangle given angle a, altitude of a, and the perimeter

I read How To Solve It by George Polya and went overboard solving one of the example problems: Construct a triangle given the vertex angle, the height of the vertex, and the perimeter.  Here is my solution in PDF. Please leave comments to correct any mistakes.  Here are the source files as tarball.

I created the figures with eukleides, a language devoted to plane geometry.  There is not a lot of documentation but hopefully my figure source files (in the above tar ball) can help you get the gist of it.  Eukleides produces very nice figures.  Create an EPS from the EUK file, then convert that with epstopdf to include it in the latex file.
$> eukleides FIGURE.euk
$> eps2topdf FIGURE.eps
I created the document in LaTeX, specifically the pdflatex package.

Sunday, November 16, 2014

disable ssh root login

Install the ssh server and client.
ROOTPROMPT$ apt-get install openssh_client openssh_server
Then open the /etc/ssh/sshd_config file
ROOTPROMPT$ emacs /etc/ssh/sshd_config

and change the"PermitRootLogin" option to 'no'
...#SKIP to relevant section
# Authentication:
LoginGraceTime 120
PermitRootLogin no
StrictModes yes


More details at the ssh man page.

Replace Cracked Dash Cam Lens

   I bought a top dawg electronics dash cam but within a week of use I broke the camera lens.  This was my fault (my kid knocked it off my desk one evening), but unfortunately they don't repair these units.  They did offer to sell me a refurb for approximately half retail price.  However, after some research on dashcamtalk I decided to try to repair it myself.  The dashcamtalk thread is on a different unit, but pointed me in the right direction for a lens replacement.  I selected this 2.8mm 115 Degree Wide Angle lens after some research on aliexpress.  This lens is longer than the original and extends past the camera body when the image is in focus, so my research was not perfect, but it works and I purchased it for $5USD shipped.  When selecting a lens make sure the it is infrared capable if your camera has infrared (night vision) recording.  Follow these instructions at your own risk because you can easily destroy the camera.
    Detach the suction cup arm and remove the battery cover and battery.  Screws are accesable on battery side.  Remove the two screws near the camera lens end and the two beneath the batter cover.  Figure 1 shows the battery side of the case propped open.  Be careful not to break the red and black wires that attach the speaker to the board.  The four small red circles show the screws that attach the circuit board to the case.  Remove these.  The large red circle on the right of the image highlights the circuit board subassembly that has the CCD (imaging chip) and the lens.  This board is mounted perpendicular to the main board by a right angle header pin.  This image was taken during reassembly and doesn't show the large amount of hot glue used to help hold them together.  Carefully remove the hot glue with a razor and tweezers until you can unplug the CCD board as shown in Figure 2 and 3.
 
Figure 1: Open case and unscrew the circuit board
Figure 2: Bottom of ciruit board showing CCD connection
Figure 3: CCD subassembly detached

The lens was super glued to the CCD mount, but only a couple threads.  Use a razor and/or tweezers to remove all visible super glue between the lens and mount.  Then you have to apply some torque to break it free, but be careful not to break the mount.  Figure 4 shows the lens separated from the CCD.

Replace the lens and attach the CCD to the main circuit board, then replace the screws to hold the circuit board in place (Figure 1).  Don't screw the case together yet because you need to power up the unit and adjust the lens to achieve a focused image.  I just held the case together and inserted the battery to do this.  Once I found the focus I backed the lens out 1.5 turns, applied a couple drops of super glue to the threads, then screwed the lens back in 1.5 turns.  Then - rapidly before the glue dries - install the battery and power up to make the final fine adjustments.
   As stated this worked, but the new lens protrudes from the camera case as shown in Figure 1.  This is only a cosmetic issue, and I'm not sure how to spec a part to correct this since I'm not a camera hobbyist (as evidenced by my blurry pictures).  However, for $5USD I'm happy with the results.

Sunday, May 18, 2014

nomodeset to correct nouveau driver black screen after login

I have an 11 year old computer in my workshop running debian squeeze, and figured I'd upgrade to wheezy.  After replacing 'squeeze' with 'wheezy' in the /etc/apt/sources.list I rant apt-get update, apt-get upgrade, apt-get dist-upgrade.  The computer functioned fine after the upgrade.  Until I rebooted.  I could log in but not start the xserver.  My TTY was periodically flooded with some gibberish with the final line reading
[drm] nouveau 0000:01:04.0: DDC responded, but no EDID for DVI-I-1
 This machine has a GeForceFX 5500 that has only VGA output.  I was confused and google did not resolve the issue when I focused on that error line.  Unfortunately I did not save the error messages from the xserver failing to start but it was along the lines of  'number of displays does not match the number of screens'.

Out of ideas I grabbed the latest version of Debian Jessie and did a fresh install on all partitions except /home - to preserve my data.  The install seemed to go smoothly but this time after logging in I got just a blank screen not even a TTY.  A little more searching on this issue revealed the solution was to prevent the kernel from loading video drivers but just use the BIOS modes until after X is started.  The nouveau driver seems particularly problematic with modesetting.  To temporarily set the boot option (so I could log in without the blackout screen) highlight the kernel on the GRUB2 screen but press 'e' instead of 'Enter'.  You are then presented with the GRUB2 boot commands.  Find the line reading  
linux /boot/... quiet splash
Where ... will depend on your particular kernel and such.  Append the 'nomodeset' command to the end of this line.
linux /boot/... quiet splash nomodeset
Then CTRL-x to boot with the new GRUB2 commands.  You can then login, su to root, and change the /etc/default/grub file to add the nomodeset command permanently to your GRUB2 commands.  Find the line with the 'quiet' command 
GRUB_CMDLINE_LINUX_DEFAULT="quiet"
and add 'nomodeset' within the same parenthesis.
GRUB_CMDLINE_LINUX_DEFAULT="quiet nomodeset"
The machine should now boot as normal.

Some of the sites I learned about this issue and solution.
nomodeset to fix black screen tutorial (Linux Mint forums)
nomodeset (and other boot options) explained (Ubuntu forums)
GRUB2 nomodeset (Open Suse forums)

I still don't know whether the 'no EDID' problem in wheezy was related or could have been solved with the nomodeset solution.

Sunday, August 25, 2013

MPD and NCMPC setup

I decided on mpd (Music Player Daemon) and ncmpc (NCurses Music Player Client) to play my music.  The following commands install the necessary packages then use insserv to remove mpd from being started automatically by root since I want to configure and run mpd as user.  The cat command output shows that mpd was part of the system startup before the innserv command. After the insserv command the lack of output from that same cat command demonstrates mpd was removed from the startup file. The ps command checks for any instances of mpd, and the final command stops it if needed.
ROOTPROMPT$ apt-get install mpd ncmpc
ROOTPROMPT$ cat /etc/init.d/.depend.start | grep mpd
TARGETS = motd killprocs rpcbind nfs-common rsyslog sudo mpd atd exim4 rsync cron ssh dbus acpid bootlogs single rmnologin rc.local
rmnologin: sudo atd rsyslog rpcbind nfs-common exim4 motd bootlogs rsync cron ssh dbus acpid mpd
rc.local: atd rsyslog rpcbind nfs-common exim4 motd bootlogs rsync cron sudo ssh dbus acpid mpd
ROOTPROMPT$ insserv -r mpd
ROOTPROMPT$ cat /etc/init.d/.depend.start | grep mpd
ROOTPROMPT$ ps aux | grep mpd
root      5029  0.0  0.0 322696  7456 ?        Ssl  23:39   0:00 mpd
root      5033  0.0  0.0   7828   872 pts/1    S+   23:39   0:00 grep mpd
ROOTPROMPT$ /etc/init.d/mpd stop
If your system does not use dependency based boot sequence you will have to configure the mpd service using the update-rc.d command.

update: With systemd I had to run the following to prevent mpd startup on boot
ROOTPROMPT$ systemctl disable mpd
ROOTPROMPT$ systemctl disable mpd.service
ROOTPROMPT$ systemctl disable mpd.socket 
Found help on crunchbang mpd how to

Copy the example mpdconf file to your home directory.
$> gunzip usr/share/doc/mpd/examples/mpd.conf.gz > ~/.mpdconf
Edit this file to change the music_directory, playlist_directory, db_file, log_file, error_file, pid_file, and state_file. Also uncomment the mixer_type line for software volume control.  Below are the relevant exerpts from my config file showing the previous entry (commented out), the comment with my name to remind me I changed it, and my new entry. For easy viewing I've cut the comments, but I recommend reading them as you edit.
#music_directory  "/var/lib/mpd/music"
#ibyrd
music_directory     "/home/byrdi01/music"
...
#playlist_directory  "/var/lib/mpd/playlists"
#ibyrd
playlist_directory  "/home/byrdi01/.mpd/playlists"
...
#db_file   "/var/lib/mpd/tag_cache"
#ibyrd
db_file             "/home/byrdi01/.mpd/database"
...
#log_file   "/var/log/mpd/mpd.log"
#ibyrd
log_file            "/home/byrdi01/.mpd/log"
...
#pid_file   "/var/run/mpd/pid"
#ibyrd
pid_file            "/home/byrdi01/.mpd/pid"
...
#state_file   "/var/lib/mpd/state"
#ibyrd
state_file   "/home/byrdi01/.mpd/state"
...
#ibyrd.  uncomment the next line to enable software control of volume.
mixer_type   "software"

My music is in the "/home/byrdi01/music" directory. Change this as appropriate to tell mpd where your music is located. Create the .mpd/ and .mpd/playlists directories.
$> mkdir -p /home/byrdi01/.mpd/playlists
Now whenever you want to play music you need to start mpd and ncmpc
$> mp
$> ncmpc
The ncmpc client will take up the entire xterm. Press 1 in ncmpc to get the list of commands. 'q' quits ncmpc, but mpd continues, so either stop playing music before exiting ncmpc or run
$> mpd --kill
to kill the daemon.
The mpd wiki has a good mpd configuration page, and I used some information from the mpd config on Arch and the mpd config on Ubuntu pages.

Low volume with snd_hda_intel module

My new machine has six audio jacks on the back (shown in the figure) and two  on the front.  
I used alsamixer to unmute and set output to 0dB for the 'Headphone' (front green jack) and the 'Speaker' (rear green jack) channels, but the volume off the green jacks was barely audible at the maximum settings and with speakers set to maximum output.  Plugging the speakers into the the grey side speaker (#1) jack produced sound at reasonable volume. The following command gives the device [bus:slot.func] numbers, then the second command used this number to reveal which kernel modules were used by my card. (This snd_hda_intel howto was very helpful in sorting this out, despite the warning at the top that the information is outdated).
$> lspci | grep Audio
00:14.2 Audio device: Advanced Micro Devices [AMD] FCH Azalia Controller (rev 01)
$> lspci -v -s00:14.2
00:14.2 Audio device: Advanced Micro Devices [AMD] FCH Azalia Controller (rev 01)
        Subsystem: Hewlett-Packard Company Device 2ae0
        Flags: bus master, slow devsel, latency 32, IRQ 16
        Memory at feb40000 (64-bit, non-prefetchable) [size=16K]
        Capabilities: 
        Kernel driver in use: snd_hda_intel
A little Google and a lot of reading suggested the problem was with the snd_hda_intel module, and was fixed as follows (Note these instructions are only useful for cards relying on the snd_hda_intel module): First get the sound card model.
$> cat /proc/asound/card0/codec* | grep Codec
Codec: IDT 92HD73E1X5
My card model is 92HD73E1X5.  I checked HD Audio Models and found this entry was the closest match.
SSTAC92HD73*
 ===========
   ref           Reference board
   no-jd         BIOS setup but without jack-detection
   intel         Intel DG45* mobos
   dell-m6-amic  Dell desktops/laptops with analog mics
   dell-m6-dmic  Dell desktops/laptops with digital mics
   dell-m6       Dell desktops/laptops with both type of mics
   dell-eq       Dell desktops/laptops
   alienware     Alienware M17x
   auto          BIOS setup (default)
My computer is an HP Pavilion P7-1439 Desktop and HPs are not on this list.  I decided to try a few configurations, so as ROOT I added the following as the last line of my /etc/modprobe.d/alsa-base.conf file.
...
options snd-hda-intel model=[MODEL]
I iterated [MODEL] through the options available for the SSTAC92HD73 series. After each [MODEL] change I reloaded alsa
ROOT$> alsa force-reload
then opened alsamixer to unmute and increase volume to 0.0dB for all outputs.  I played music or ran the command
speaker-test -c 2 -t wav
while I moved the speaker plug to each jack, and tested headphone jack detection on the front jack by plugging/unplugging in headphones for each speaker plug location. I found that dell-m6 provided good volume levels from front green jack and rear black jack (with jack detection) for headphones and rear blue 'in' jack for speakers, but no volume from other jacks.  Not listing a model option in the config file produced speaker output from the 'side' (grey), 'rear' (black), and 'csub' (orange) jacks, but the volume from the front headphone jack was extremely low, although it did have jack detection.  Since I have a simple pair of old DELL PC speakers, using the dell-m6 model and plugging the speakers into the blue jack works for me.  However if you have surround sound you'll have to choose between that and audible headphone volume.

   A little investigating revealed this snd_hda_intel low volume bug, and posts 13-16 show a difference in the GPIO IO[0] data pin when the volume is correct and when it is not.  I did the following: Comment out the options snd-hda-intel model=dell-m6 in /etc/modprobe.d/alsa-base.conf so that the model loads as default, then
ROOT$> alsa force-reload
ROOT$> cat /proc/asound/card0/codec#3 > auto.txt 
Then uncomment that same line in /etc/modprobe.d/alsa-base.conf and
ROOT$> alsa force-reload
ROOT$> cat /proc/asound/card0/codec#3 > dellm6.txt 
So I could compare all differences. I've made the outputs for defaultdell-eq, and dell-m6 available.  My GPIO IO[0] data=0 with models that produce low headphone jack volume and GPIO IO[0] data=1 for models that produced normal headphone jack volume.  the nodes are different for the headphone outputs as well, but I'm not sure that's important.  Because I don't have surround sound speakers, I didn't persist further with the hd-verb to manually change the data pin.

  Just to verify my card does produce surround sound, I commented out the options snd-hda-intel model=dell-m6 in /etc/modprobe.d/alsa-base.conf to load the default model then ran
speaker-test -D surround71 -c 6 -t wav
while moving the speaker plug to the 'side' (grey), 'rear' (black), and 'csub' (orange) jacks.  "Front Left" and "Front Right" played from the respective speakers when plugged into the grey jack, "Rear Left" and "Rear Right" played from the respective speakers when plugged into the black jack, and "Front Center" and Rear Center" played from the respective speakers when plugged into the orange jack, so my surround sound works on this card.