Wednesday, January 8, 2014

Control your Yamaha AV Receiver (RX-V673, RX-A720, RX-V773, RX-A820, RX-A1020 etc.) with a python script

Please check out

https://github.com/thomas-villagers/avsend

for a python script to control your Yamaha AV Receiver.
This is more or less the same stuff the Android and iOS apps are based on.
So feel free to make a nice GUI if you like.

Have fun!

Monday, August 5, 2013

Streams from "Structure and Interpretation of Computer Programs" with Gambit Scheme

Figuring out how to implement Streams in the sense of Structure and Interpretation of Computer Programs by Abelson, Sussman and Sussman (great book!) in Gambit Scheme was difficult.

Clearly, "cons-stream" has to be a Macro.But how to do that with Gambit and it's confusing (for me!) macro system(s) ??

I found some older discussions and most helpful of all was https://wiki.aalto.fi/download/attachments/70786353/course-support-gambitc.scm.

However, the definition of cons-stream works fine in the REPL, but when I put the code in a file "streams.scm" and load it via

(load "streams.scm")

I get an error:

1> (cons-stream 1 2)                                        
*** ERROR IN (console)@3.2 -- Unbound variable: cons-stream


The reason for this might be that Gambit needs to know about the macro before it is read by the reader. (?) "Including" the file with the macro definition did not work either. (confusing stuff for a Clojure programmer where Macros just work ... )

The solution is to put the Macro in a file ".gambcini".
Now, we are ready for stream-fun:

(cons-stream 1 2)  
(1 . #<promise #2>)


This works with Gambit v4.2.8 in the Debian Repositories and with Mac OSX.




Saturday, March 23, 2013

Magento: How to add Download Link for Data Sheet PDFs to Product Page

Let's add a download link for arbitrary files (in this case, data sheets in PDF) to your products on Magento.
(This is relatively easy, so no need to buy an extension ...  )

To do this, we create a new attribute (Admin Panel, go to "Catalog->Attributes->Manage Attributes").

Create a new Attribute:

Attribute Code: pdf_download
Scope: Global
Catalog Input Type for Store Owner: Textfield
Default: <empty>
Apply To: (arbitrary, I chose "All Product Types")

Visible on Product View Page on Front-end: Yes
Used in Product Listing: Yes

Under "Mange Label / Options" enter "Data Sheet".

Save the new attribute.

Open "Catalog->Attributes->Manage Attribute Sets" and choose a set (I chose "Default").
Drag your new Attribute "pdf_download" from the right column into the General group and save.

Open a Product from your Catalog and hopefully you'll find the new attribute "Data Sheet".

Now, create a folder "files" in your Web Server's document root (e.g. var/www/files or apache/htdocs/files). Copy your data sheet pdf there:

  var/www/files/datasheet.pdf

We can now specify the link to our data sheet in the "Data Sheet" text field in Magento's backend:

  Data Sheet: <a href="http://localhost/files/datasheet.pdf"> Data Sheet </a>

Refresh Magento's Index and Cache and navigate to your product on the front page. You should now see a table titled "Additional Information" with the link to our data sheet.

This approach has two problems, however: If we move our shop to a different server, we would need to adjust the attribute for all our products with a downloadable data sheet. Further, we always have to speficy the full link which is tedious.

We can do better than that.
Let's mess with some Magento internals and modify "attributes.phtml".
(don't forget to make a backup of this file in case something goes wrong!)
Search for attributes.phtml, in my case it was
app/design/frontend/base/default/template/catalog/product/view/attributes.phtml

Search for this block at the end of the file:

and change it to:
That's it (for now & testing purposes). You can now replace the text for the attribute

   Data Sheet: <a href="http://localhost/files/datasheet.pdf"> Data Sheet </a>

with the much simpler

Data Sheet:  datasheet.pdf

Please test if that works. Now, let's get rid of the hard coded url (http://localhost). To do that, first put the pdfs in a subfolder of magento, i.e.

  var/www/magento/files/datasheet.pdf

Now change the line
into
You're done.

Sunday, May 27, 2012

Clojure macro for LibGDX input polling

A very simple macro:
(defmacro when-pressed
  ([key form]
  `(when (.isKeyPressed Gdx/input (. com.badlogic.gdx.Input$Keys ~key))
     ~form))
  ([key form & rest]
     `(do
        (when-pressed ~key ~form)
        (when-pressed ~@rest))))
example usage:
 (when-pressed LEFT  (do-something)
               RIGHT (do-something-different)
               SPACE ( ... )
               ...)
expands to:
user=> (pprint (macroexpand-1 '(when-pressed LEFT (do-something))))
(clojure.core/when
 (.isKeyPressed Gdx/input (. com.badlogic.gdx.Input$Keys LEFT))
 (do-something))
nil
or
user=> (pprint (macroexpand-1 '(when-pressed LEFT (do-something)
                                             RIGHT (do-something-else))))
(do
 (when-pressed LEFT (do-something))
 (when-pressed RIGHT (do-something-else)))
nil

Saturday, May 12, 2012

Android: A two column ListView with an ImageView

Struggling with ListViews and SimpleAdapters on Android, I came up with the following simple example of a ListView with two columns: an image and some text. In res/layout, create a xml list_item.xml for the listview items:
<?xml version="1.0" encoding="utf-8"?>
<!-- list_item.xml -->
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingTop="4dip"
    android:paddingBottom="6dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="horizontal">
    <ImageView android:layout_width="100px"
       android:layout_height="50px"
       android:layout_gravity="left"
       android:id="@+id/LOGO_CELL"
       />
    <TextView android:id="@+id/TEXT_CELL"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:textColor="#000000"
       android:layout_gravity="center"
       android:layout_weight="1"/>

</LinearLayout>

Add a ListView to main.xml main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Some Text"
      />
  <ListView  android:id="@+id/LISTVIEW"
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
   >
  </ListView>
</LinearLayout>

Finally, the code:
(ns com.example.android.ListExample 
  (:gen-class
   :extends android.app.Activity
   :exposes-methods {onCreate superOnCreate}
  (:import (android.app Activity)
           (android.os Bundle))))

(defn -onCreate  [this bundle]
    (.superOnCreate this bundle )
    (.setContentView this com.example.android.R$layout/main)
    (let [ids (int-array [com.example.android.R$id/LOGO_CELL
                          com.example.android.R$id/TEXT_CELL])
          names (into-array ["logo" "text"])
          thelist (java.util.ArrayList.
                   [{"logo" com.example.android.R$drawable/logo1 "text" "Text 1"}
                    {"logo" com.example.android.R$drawable/logo2 "text" "Text 2"}]
                   aa (android.widget.SimpleAdapter. this thelist
                                                     com.example.android.R$layout/list_item names ids)]
      (.setAdapter (.findViewById this com.example.android.R$id/LISTVIEW) aa)))
          
This will find the two pngs "logo1.png" and "logo2.png" in the res/drawable folders.